An IWebHookHandler can post a response to a Slack Slash request by updating the System.Net.Http.HttpResponseMessage on the WebHookHandlerContext with a response containing a SlackSlashResponse. Please see https://api.slack.com/docs/attachments#message_formatting for additional details.
示例#1
0
        public void SlackSlashResponse_Roundtrips()
        {
            // Arrange
            JObject         data = EmbeddedResource.ReadAsJObject("Microsoft.AspNet.WebHooks.Messages.SlashResponse.json");
            SlackAttachment att1 = new SlackAttachment
            {
                Fallback   = "Required plain-text summary of the attachment.",
                Color      = "#36a64f",
                Pretext    = "Optional text that appears above the attachment block",
                AuthorName = "Bobby Tables",
                AuthorLink = new Uri("http://flickr.com/bobby/"),
                AuthorIcon = new Uri("http://flickr.com/icons/bobby.jpg"),
                Title      = "Slack API Documentation",
                TitleLink  = new Uri("https://api.slack.com/"),
                Text       = "Optional text that appears within the attachment",
                ImageLink  = new Uri("http://my-website.com/path/to/image.jpg"),
                ThumbLink  = new Uri("http://example.com/path/to/thumb.png"),
            };

            att1.Fields.Add(new SlackField("Priority", "High")
            {
                Short = true
            });
            att1.Fields.Add(new SlackField("Importance", "Low")
            {
                Short = false
            });

            SlackAttachment att2 = new SlackAttachment
            {
                Fallback  = "New ticket from Andrea Lee - Ticket #1943: Can't rest my password - https://groove.hq/path/to/ticket/1943",
                Color     = "#7CD197",
                Pretext   = "New ticket from Andrea Lee",
                Title     = "Ticket #1943: Can't reset my password",
                TitleLink = new Uri("https://groove.hq/path/to/ticket/1943"),
                Text      = "Help! I tried to reset my password but nothing happened!",
            };

            SlackSlashResponse expected = new SlackSlashResponse("It's 80 degrees right now.")
            {
                ResponseType = "in_channel"
            };

            expected.Attachments.Add(att1);
            expected.Attachments.Add(att2);

            // Act
            SlackSlashResponse actual = data.ToObject <SlackSlashResponse>();

            // Assert
            string expectedJson = JsonConvert.SerializeObject(expected);
            string actualJson   = JsonConvert.SerializeObject(actual);

            Assert.Equal(expectedJson, actualJson);
        }
        public void SlackAttachment_InitializesAttachments()
        {
            // Arrange
            var expected = new SlackAttachment[]
            {
                new SlackAttachment("t1", "f1"),
                new SlackAttachment("t2", "f2"),
                new SlackAttachment("t3", "f3"),
            };

            // Act
            SlackSlashResponse rsp = new SlackSlashResponse("It's 80 degrees right now.", expected) { ResponseType = "in_channel" };

            // Assert
            Assert.Equal(expected, rsp.Attachments);
        }
示例#3
0
        public void SlackAttachment_InitializesAttachments()
        {
            // Arrange
            var expected = new SlackAttachment[]
            {
                new SlackAttachment("t1", "f1"),
                new SlackAttachment("t2", "f2"),
                new SlackAttachment("t3", "f3"),
            };

            // Act
            SlackSlashResponse rsp = new SlackSlashResponse("It's 80 degrees right now.", expected)
            {
                ResponseType = "in_channel"
            };

            // Assert
            Assert.Equal(expected, rsp.Attachments);
        }
        public void SlackSlashResponse_Roundtrips()
        {
            // Arrange
            JObject data = EmbeddedResource.ReadAsJObject("Microsoft.AspNet.WebHooks.Messages.SlashResponse.json");
            SlackAttachment att1 = new SlackAttachment
            {
                Fallback = "Required plain-text summary of the attachment.",
                Color = "#36a64f",
                Pretext = "Optional text that appears above the attachment block",
                AuthorName = "Bobby Tables",
                AuthorLink = new Uri("http://flickr.com/bobby/"),
                AuthorIcon = new Uri("http://flickr.com/icons/bobby.jpg"),
                Title = "Slack API Documentation",
                TitleLink = new Uri("https://api.slack.com/"),
                Text = "Optional text that appears within the attachment",
                ImageLink = new Uri("http://my-website.com/path/to/image.jpg"),
                ThumbLink = new Uri("http://example.com/path/to/thumb.png"),
            };
            att1.Fields.Add(new SlackField("Priority", "High") { Short = true });
            att1.Fields.Add(new SlackField("Importance", "Low") { Short = false });

            SlackAttachment att2 = new SlackAttachment
            {
                Fallback = "New ticket from Andrea Lee - Ticket #1943: Can't rest my password - https://groove.hq/path/to/ticket/1943",
                Color = "#7CD197",
                Pretext = "New ticket from Andrea Lee",
                Title = "Ticket #1943: Can't reset my password",
                TitleLink = new Uri("https://groove.hq/path/to/ticket/1943"),
                Text = "Help! I tried to reset my password but nothing happened!",
            };

            SlackSlashResponse expected = new SlackSlashResponse("It's 80 degrees right now.") { ResponseType = "in_channel" };
            expected.Attachments.Add(att1);
            expected.Attachments.Add(att2);

            // Act
            SlackSlashResponse actual = data.ToObject<SlackSlashResponse>();

            // Assert
            string expectedJson = JsonConvert.SerializeObject(expected);
            string actualJson = JsonConvert.SerializeObject(actual);
            Assert.Equal(expectedJson, actualJson);
        }
示例#5
0
        public void ResponseType_Roundtrips()
        {
            SlackSlashResponse response = new SlackSlashResponse("Some text");

            PropertyAssert.Roundtrips(response, a => a.ResponseType, PropertySetter.NullRoundtrips, roundtripValue: "你好世界");
        }
示例#6
0
        public void Text_Roundtrips()
        {
            SlackSlashResponse response = new SlackSlashResponse("Some text");

            PropertyAssert.Roundtrips(response, a => a.Text, PropertySetter.NullThrows, defaultValue: "Some text", roundtripValue: "你好世界");
        }
 public void ResponseType_Roundtrips()
 {
     SlackSlashResponse response = new SlackSlashResponse("Some text");
     PropertyAssert.Roundtrips(response, a => a.ResponseType, PropertySetter.NullRoundtrips, roundtripValue: "你好世界");
 }
 public void Text_Roundtrips()
 {
     SlackSlashResponse response = new SlackSlashResponse("Some text");
     PropertyAssert.Roundtrips(response, a => a.Text, PropertySetter.NullThrows, defaultValue: "Some text", roundtripValue: "你好世界");
 }
示例#9
0
        public override Task ExecuteAsync(string generator, WebHookHandlerContext context)
        {
            // For more information about Slack WebHook payloads, please see 
            // 'https://api.slack.com/outgoing-webhooks'
            NameValueCollection command = context.GetDataOrDefault<NameValueCollection>();

            // We can trace to see what is going on.
            Trace.WriteLine(command.ToString());

            // Switch over the IDs we used when configuring this WebHook 
            switch (context.Id)
            {
                case "trigger":
                    // Parse the trigger text of the form 'action parameters'.
                    var triggerCommand = SlackCommand.ParseActionWithValue(command["subtext"]);

                    // Information can be returned using a SlackResponse
                    string reply1 = string.Format(
                        "Received trigger '{0}' with action '{1}' and value '{2}'",
                        command["trigger_word"],
                        triggerCommand.Key,
                        triggerCommand.Value);
                    var triggerReply = new SlackResponse(reply1);
                    context.Response = context.Request.CreateResponse(triggerReply);
                    break;

                case "slash":
                    // Parse the slash text of the form 'action p1=v1; p2=v2; ...'.
                    var slashCommand = SlackCommand.ParseActionWithParameters(command["text"]);

                    string reply2 = string.Format(
                        "Received slash command '{0}' with action '{1}' and value '{2}'",
                        command["command"],
                        slashCommand.Key,
                        slashCommand.Value.ToString());

                    // Information can be returned using a SlackSlashResponse with attachments 
                    var slashReply = new SlackSlashResponse(reply2);

                    // Slash replies can be augmented with attachments containing data, images, and more 
                    var att = new SlackAttachment("Attachment Text", "Fallback description")
                    {
                        Color = "#439FE0",
                        Pretext = "Hello from ASP.NET WebHooks!",
                        Title = "Attachment title",
                    };

                    // Slash attachments can contain tabular data as well
                    att.Fields.Add(new SlackField("Field1", "1234"));
                    att.Fields.Add(new SlackField("Field2", "5678"));

                    // A reply can contain multiple attachments
                    slashReply.Attachments.Add(att);

                    // Return slash command response
                    context.Response = context.Request.CreateResponse(slashReply);
                    break;
            }

            return Task.FromResult(true);
        }