private static async Task BuildEmail(string toAddress, Language language, IBinder binder, TemplateData templateData, string category, CancellationToken token) { var emailProvider = await binder.BindAsync <IAsyncCollector <SendGridMessage> >(new SendGridAttribute() { ApiKey = "SendgridKey", //From = "Spitball <no-reply @spitball.co>" }, token); var message = new SendGridMessage { Asm = new ASM { GroupId = UnsubscribeGroup.Update }, TemplateId = "d-91a839096c8547f9a028134744e78ecb" }; if (language.Info.Equals(Language.EnglishIndia.Info)) { message.TemplateId = "d-91a839096c8547f9a028134744e78ecb"; } message.AddFromResource(language.Info); templateData.To = toAddress; var personalization = new Personalization { TemplateData = templateData }; message.Personalizations = new List <Personalization>() { personalization }; message.AddCategory(category); message.TrackingSettings = new TrackingSettings { Ganalytics = new Ganalytics { UtmCampaign = category, UtmSource = "SendGrid", UtmMedium = "Email", Enable = true } }; message.AddTo(toAddress); await emailProvider.AddAsync(message, token); }
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); } }
private static async Task ProcessEmail(IAsyncCollector <SendGridMessage> emailProvider, ILogger log, BaseEmail topicMessage, CancellationToken token) { var message = new SendGridMessage(); var personalization = new Personalization(); if (topicMessage.TemplateId != null) { message.Asm = new ASM { GroupId = topicMessage.UnsubscribeGroup }; message.TemplateId = topicMessage.TemplateId; message.Subject = topicMessage.Subject; if (topicMessage.Campaign != null) { message.AddCategory(topicMessage.Campaign); message.TrackingSettings = new TrackingSettings { Ganalytics = new Ganalytics { UtmCampaign = topicMessage.Campaign, UtmSource = "SendGrid", UtmMedium = "Email", Enable = true } }; message.TrackingSettings.Ganalytics.Enable = true; } personalization.Substitutions = new Dictionary <string, string>(); foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(topicMessage)) { var p = prop.GetValue(topicMessage); personalization.Substitutions[$"-{prop.Name}-"] = p?.ToString() ?? string.Empty; //personalization.AddSubstitution($"-{prop.Name}-", p?.ToString() ?? string.Empty); } message.Personalizations = new List <Personalization>() { personalization }; } else { message.AddContent("text/html", topicMessage.ToString()); message.Subject = topicMessage.Subject; if (topicMessage.Bcc != null) { foreach (var bcc in topicMessage.Bcc) { message.AddBcc(bcc); } } log.LogWarning("error with template name" + topicMessage.TemplateId); } CultureInfo.DefaultThreadCurrentCulture = topicMessage.Info; message.AddFromResource(topicMessage.Info); message.AddTo(topicMessage.To); await emailProvider.AddAsync(message, token); }
public async Task DoOperationAsync(RequestTutorMessage message2, IBinder binder, CancellationToken token) { var query = new RequestTutorEmailQuery(message2.LeadId); var result = await _queryBus.QueryAsync(query, token); foreach (var obj in result) { CultureInfo.DefaultThreadCurrentCulture = obj.TutorLanguage.ChangeCultureBaseOnCountry(obj.TutorCountry); var body = ResourceWrapper.GetString("unread_message_request_email_body"); var code = _dataProtectionService.ProtectData(obj.TutorId.ToString(), DateTimeOffset.UtcNow.AddDays(5)); var identifierChat = ShortId.Generate(true, false); var url = _urlBuilder.BuildChatEndpoint(code, new { utm_source = "request-tutor-email" }); var commandChat = new CreateShortUrlCommand(identifierChat, url.PathAndQuery, DateTime.UtcNow.AddDays(5)); await _commandBus.DispatchAsync(commandChat, token); var request = string.Empty; //TODO - whatsapp link if (!string.IsNullOrEmpty(obj.Request)) { request = ResourceWrapper.GetString("unread_message_request_email_body_lead_request") .InjectSingleValue("Request", obj.Request); } var whatsAppLink = new UriBuilder($"https://wa.me/{obj.StudentPhoneNumber.Replace("+", string.Empty)}") .AddQuery(new { text = ResourceWrapper.GetString("unread_message_request_email_body_whatsapp_text").InjectSingleValue("CourseName", obj.CourseName), }); var identifierWhatsApp = ShortId.Generate(true, false); var commandWhatsApp = new CreateShortUrlCommand(identifierWhatsApp, whatsAppLink.ToString(), DateTime.UtcNow.AddDays(30)); await _commandBus.DispatchAsync(commandWhatsApp, token); var urlShortWhatsApp = _urlBuilder.BuildShortUrlEndpoint(identifierWhatsApp, new { eventCategory = "After tutor Submit", eventAction = "Whatsapp email", eventLabel = $"Tutor{obj.TutorId}, Student {obj.StudentId}" }); body = body.InjectSingleValue("Request", request); body = body.InjectSingleValue("WhatsappLink", urlShortWhatsApp); var htmlBodyDirection = CultureInfo.CurrentCulture.TextInfo.IsRightToLeft ? "rtl" : "ltr"; body = body.Inject(new { Request = request, WhatsappLink = whatsAppLink.ToString(), UserFirstName = obj.TutorFirstName, link = _urlBuilder.BuildShortUrlEndpoint(identifierChat /*, obj.TutorCountry*/), obj.StudentName, obj.CourseName, FirstMessageStudentName = obj.StudentName }); var message = new SendGridMessage() { Subject = ResourceWrapper.GetString("unread_message_request_email_subject").InjectSingleValue("FirstMessageStudentName", obj.StudentName) .Inject(obj), HtmlContent = $"<html><body dir=\"{htmlBodyDirection}\">{body.Replace(Environment.NewLine, "<br><br>")}</body></html>", }; message.AddFromResource(CultureInfo.DefaultThreadCurrentCulture); message.AddTo(obj.TutorEmail); var emailProvider = await binder.BindAsync <IAsyncCollector <SendGridMessage> >(new SendGridAttribute() { ApiKey = "SendgridKey" }, token); await emailProvider.AddAsync(message, token); } }