예제 #1
0
        private static async Task PostMessage(SlackPayload payload, Uri uri)
        {
            var payloadJson = JsonSerializer.Serialize(payload);

            using var client = new HttpClient();
            await client.PostAsync(uri.ToString(), new StringContent(payloadJson));
        }
예제 #2
0
        public static async Task PostMessageToSlack(EmailSubmission submission, Guid id, Uri uri)
        {
            var payload = new SlackPayload()
            {
                Attachments = new List <SlackAttachment>()
                {
                    new SlackAttachment()
                    {
                        Pretext    = "New submission",
                        Title      = submission.Subject,
                        AuthorName = $"{submission.Name} <{submission.Email}>",
                        Text       = submission.Content,
                    },
                    new SlackAttachment()
                    {
                        Fallback   = "Your client does not support approving/rejecting messages",
                        CallbackId = "submit",
                        Actions    = new List <SlackAction>()
                        {
                            new SlackAction()
                            {
                                Name    = "approve",
                                Text    = "Approve",
                                Style   = "primary",
                                Type    = "button",
                                Value   = id.ToString(),
                                Confirm = new SlackConfirm()
                                {
                                    Text        = "Are you sure you want to approve this message?",
                                    OkText      = "Yes",
                                    DismissText = "Not right now"
                                }
                            },
                            new SlackAction()
                            {
                                Name    = "reject",
                                Text    = "Reject",
                                Type    = "button",
                                Confirm = new SlackConfirm()
                                {
                                    Text        = "Are you sure you want to reject this message?",
                                    OkText      = "Yes",
                                    DismissText = "Not right now"
                                }
                            }
                        }
                    }
                }
            };

            if (submission.Recipients?.Count > 0)
            {
                payload.Attachments[0].Fields = new List <SlackField>()
                {
                    new SlackField()
                    {
                        Title = "Recipients",
                        Value = string.Join(", ", submission.Recipients),
                        Short = false,
                    }
                };
            }
            await PostMessage(payload, uri);
        }