Beispiel #1
0
        private static string GetText(bool isTutor, int count, bool email)
        {
            var suffix = email ? "_email" : "";

            if (isTutor && count == 1)
            {
                return(ResourceWrapper.GetString($"unread_message_first_message_tutor{suffix}"));
            }

            return(string.Format(
                       ResourceWrapper.GetString($"unread_message{suffix}"),
                       isTutor ?
                       ResourceWrapper.GetString("unread_message_student") :
                       ResourceWrapper.GetString("unread_message_tutor")));
        }
Beispiel #2
0
        public static IActionResult RunTwilioResult(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "twilio")]
            HttpRequest req, ILogger log)
        {
            string name    = req.Query["code"];
            string culture = req.Query["culture"];

            //var culture = CultureInfo.CurrentUICulture.ChangeCultureBaseOnCountry(result.Country);
            CultureInfo.DefaultThreadCurrentCulture = new CultureInfo(culture);
            var twiml    = new VoiceResponse();
            var sentence = string.Format(ResourceWrapper.GetString("call_text"), string.Join(". ", name.ToCharArray()));

            twiml.Say(sentence, loop: 3, voice: "alice");
            return(new ContentResult()
            {
                Content = twiml.ToString(),
                ContentType = "application/xml"
            });
        }
Beispiel #3
0
        public static async Task SmsUnreadAsync([TimerTrigger("0 */10 * * * *")] TimerInfo myTimer,
                                                [Blob("spitball/chat/unread.txt")] CloudBlockBlob blob,
                                                [TwilioSms(AccountSidSetting = "TwilioSid", AuthTokenSetting = "TwilioToken", From = "+1 203-347-4577")] IAsyncCollector <CreateMessageOptions> options,
                                                [SendGrid(ApiKey = "SendgridKey")] IAsyncCollector <SendGridMessage> emailProvider,
                                                [Inject] IQueryBus queryBus,
                                                [Inject] ICommandBus commandBus,
                                                [Inject] IDataProtectionProvider dataProtectProvider,
                                                [Inject] IUrlBuilder urlBuilder,
                                                ILogger log,
                                                CancellationToken token)
        {
            byte[] version = null;
            if (await blob.ExistsAsync())
            {
                version = new byte[8];
                await blob.DownloadToByteArrayAsync(version, 0);
            }

            var query         = new UserUnreadMessageQuery(version);
            var result        = (await queryBus.QueryAsync(query, token)).ToList();
            var dataProtector = dataProtectProvider.CreateProtector("Spitball")
                                .ToTimeLimitedDataProtector();

            foreach (var unreadMessageDto in result.Distinct(UnreadMessageDto.UserIdComparer))
            {
                try
                {
                    if (unreadMessageDto.ChatMessagesCount == 0)
                    {
                        continue;
                    }

                    if (unreadMessageDto.Country is null)
                    {
                        continue;
                    }
                    log.LogInformation($"processing {unreadMessageDto}");
                    CultureInfo.DefaultThreadCurrentCulture = unreadMessageDto.CultureInfo.ChangeCultureBaseOnCountry(unreadMessageDto.Country);

                    var code       = dataProtector.Protect(unreadMessageDto.UserId.ToString(), DateTimeOffset.UtcNow.AddDays(5));
                    var identifier = ShortId.Generate(true, false);
                    var url        = urlBuilder.BuildChatEndpoint(code, new { utm_source = "SMS-auto" });
                    var command    = new CreateShortUrlCommand(identifier, url.PathAndQuery, DateTime.UtcNow.AddDays(5));
                    await commandBus.DispatchAsync(command, token);

                    var urlShort = urlBuilder.BuildShortUrlEndpoint(identifier, unreadMessageDto.Country);


                    if (!unreadMessageDto.Country.Equals(Country.India.Name, StringComparison.OrdinalIgnoreCase))
                    {
                        var text           = GetText(unreadMessageDto.IsTutor, unreadMessageDto.ChatMessagesCount, false);
                        var generatedText  = text.Inject(new { link = urlShort });
                        var messageOptions = new CreateMessageOptions(new PhoneNumber(unreadMessageDto.PhoneNumber))
                        {
                            Body = generatedText
                        };
                        if (unreadMessageDto.PhoneNumber.StartsWith("+972"))
                        {
                            messageOptions.From = "Spitball";
                        }
                        try
                        {
                            await options.AddAsync(messageOptions, token);

                            await options.FlushAsync(token);
                        }
                        catch (Exception ex)
                        {
                            log.LogError(ex, $"Cant send sms to {unreadMessageDto}");
                        }
                    }

                    var htmlBodyDirection = CultureInfo.CurrentCulture.TextInfo.IsRightToLeft ? "rtl" : "ltr";

                    var message = new SendGridMessage();
                    message.AddTo(unreadMessageDto.Email);

                    var body = GetText(unreadMessageDto.IsTutor, unreadMessageDto.ChatMessagesCount, true).Inject(new { link = urlShort });


                    message.AddContent("text/html", $"<html>" +
                                       $"<body dir=\"{htmlBodyDirection}\">{body}</body></html>");
                    message.SetSubject(ResourceWrapper.GetString("you_have_unread_message"));
                    message.AddFromResource(CultureInfo.DefaultThreadCurrentCulture);
                    await emailProvider.AddAsync(message, token);

                    await emailProvider.FlushAsync(token);
                }
                catch (Exception ex)
                {
                    log.LogError(ex, $"Cant send sms/email to {unreadMessageDto}");
                }
            }
            if (result.Count > 0)
            {
                version = result.OrderByDescending(o => o.VersionAsLong).First().Version;
                await blob.UploadFromByteArrayAsync(version, 0, 8);
            }
        }