Esempio n. 1
0
        public static async Task SendUpdateToSlack(SlackSubmission submission, bool accept, ILogger log = null)
        {
            var message = accept ? $":white_check_mark: Approved by <@{submission.User.Id}|{submission.User.Name}>" : $":x: Rejected by <@{submission.User.Id}|{submission.User.Name}>";

            var emailAtt = submission.OriginalMessage.Attachments[0];
            var colour   = accept ? "good" : "danger";

            var jsonData = JsonSerializer.Serialize(new
            {
                attachments = new object[]
                {
                    emailAtt,
                    new
                    {
                        fallback = message,
                        color    = colour,
                        text     = message,
                        ts       = (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds
                    }
                },
                replace_original = "true",
                response_type    = "in_channel"
            });

            var httpClient = new HttpClient();
            await httpClient.PostAsync(submission.ResponseUrl, new StringContent(jsonData));
        }
Esempio n. 2
0
        public static async Task SendEmailAsync(SlackSubmission submission, ILogger log = null)
        {
            const string sender = "*****@*****.**";

            var authorName = submission.OriginalMessage.Attachments[0].AuthorName;

            authorName = HttpUtility.HtmlDecode(authorName);
            log?.LogInformation("Author: {Author}", authorName);

            // authorName is of the form "Name <email>" so pull out the relevant information.
            const string pattern = @"(?<name>.+) \<(?<email>.+)\>";
            var          m       = Regex.Match(authorName, pattern);
            string       emailAuthorName;
            string       emailAuthorEmail;

            if (m.Success)
            {
                emailAuthorName  = m.Groups["name"].Value;
                emailAuthorEmail = m.Groups["email"].Value;
            }
            else
            {
                return;
            }

            // Pull out the recipients.
            var recipients = submission.OriginalMessage.Attachments[0].Fields[0].Value;

            recipients = HttpUtility.HtmlDecode(recipients);
            log?.LogInformation("Recipients: {Recipients}", recipients);
            var emailRecipients = recipients
                                  .Split(',', StringSplitOptions.RemoveEmptyEntries)
                                  .Select(r => r.Split('|')[1].Replace(">", string.Empty))
                                  .Select(e => new EmailAddress(e))
                                  .ToList();

            var message = new MimeMessage();

            message.From.Add(new MailboxAddress($"{emailAuthorName} via Yes In New West", sender));
            foreach (var emailRecipient in emailRecipients)
            {
                message.To.Add(new MailboxAddress(emailRecipient.Name, emailRecipient.Email));
            }
            message.Cc.Add(new MailboxAddress(emailAuthorName, emailAuthorEmail));
            message.ReplyTo.Add(new MailboxAddress(emailAuthorName, emailAuthorEmail));

            message.Subject = submission.OriginalMessage.Attachments[0].Title;
            message.Body    = new TextPart("plain")
            {
                Text = $"{submission.OriginalMessage.Attachments[0].Text}\n\nThis email was sent by {emailAuthorName} ({emailAuthorEmail}) through the Yes In New West letter sender."
            };

            using var client = new SmtpClient();
            await client.ConnectAsync("smtp.sendgrid.net", 465, true);

            await client.AuthenticateAsync("apikey", Environment.GetEnvironmentVariable("SENDGRID_API_KEY"));

            await client.SendAsync(message);

            await client.DisconnectAsync(true);
        }