The SlackAttachment is used to describe the contents of an SlackSlashResponse.
Beispiel #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);
        }
Beispiel #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);
        }
Beispiel #5
0
        public void Fallback_Roundtrips()
        {
            SlackAttachment att = new SlackAttachment("Some text", "MyFallback");

            PropertyAssert.Roundtrips(att, a => a.Fallback, PropertySetter.NullThrows, defaultValue: "MyFallback", roundtripValue: "你好世界");
        }
 public void Fallback_Roundtrips()
 {
     SlackAttachment att = new SlackAttachment("Some text", "MyFallback");
     PropertyAssert.Roundtrips(att, a => a.Fallback, PropertySetter.NullThrows, defaultValue: "MyFallback", roundtripValue: "你好世界");
 }
Beispiel #7
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);
        }