internal static SlackCommand AsSlackCommand(this IFormCollection form)
        {
            if (form == null)
            {
                return(null);
            }

            var command = new SlackCommand
            {
                token           = form[nameof(SlackCommand.token)].ToString(),
                team_id         = form[nameof(SlackCommand.team_id)].ToString(),
                team_domain     = form[nameof(SlackCommand.team_domain)].ToString(),
                enterprise_id   = form[nameof(SlackCommand.enterprise_id)].ToString(),
                enterprise_name = form[nameof(SlackCommand.enterprise_name)].ToString(),
                channel_id      = form[nameof(SlackCommand.channel_id)].ToString(),
                channel_name    = form[nameof(SlackCommand.channel_name)].ToString(),
                user_id         = form[nameof(SlackCommand.user_id)].ToString(),
                user_name       = form[nameof(SlackCommand.user_name)].ToString(),
                command         = form[nameof(SlackCommand.command)].ToString(),
                text            = form[nameof(SlackCommand.text)].ToString(),
                response_url    = form[nameof(SlackCommand.response_url)].ToString(),
                trigger_id      = form[nameof(SlackCommand.trigger_id)].ToString(),
            };

            return(command);
        }
Exemplo n.º 2
0
 public Task <AppResponse> ProcessAsync(SlackCommand command)
 {
     return(Task.FromResult(new AppResponse {
         ImmediateMessage = new SlackMessage {
             text = "DUMMY"
         }
     }));
 }
Exemplo n.º 3
0
        public IActionResult SlackForCommand(string @event, IFormCollection data)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _logger.LogInformation(
                0,
                "{ControllerName} / '{ReceiverId}' received {Count} properties with event '{EventName}').",
                nameof(SlackController),
                "command",
                data.Count,
                @event);

            string channel = data[SlackConstants.ChannelRequestFieldName];
            string command = data[SlackConstants.CommandRequestFieldName];
            string trigger = data[SlackConstants.TriggerRequestFieldName];

            _logger.LogInformation(
                1,
                "Data contains channel '{ChannelName}', command '{Command}', and trigger '{Trigger}'.",
                channel,
                command,
                trigger);

            string text    = data[SlackConstants.TextRequestFieldName];
            string subtext = data[SlackConstants.SubtextRequestFieldName];

            _logger.LogInformation(
                2,
                "Data contains text '{Text}' and subtext '{Subtext}'.",
                text,
                subtext);

            // Create the response.
            var slashCommand = SlackCommand.ParseActionWithParameters(command);
            var reply        = string.Format(
                "Received slash command '{0}' with action '{1}' and value '{2}'",
                command,
                slashCommand.Key,
                slashCommand.Value.ToString());

            // Slash responses can be augmented with attachments containing data, images, and more.
            var attachment = 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
            attachment.Fields.Add(new SlackField("Field1", "1234"));
            attachment.Fields.Add(new SlackField("Field2", "5678"));

            return(new JsonResult(new SlackSlashResponse(reply, attachment)));
        }
        public Task <AppResponse> ProcessAsync(SlackCommand command)
        {
            var response = new AppResponse();

            response.ResponseUrlMessages.Add(new SlackMessage {
                text = "DUMMY"
            });
            return(Task.FromResult(response));
        }
Exemplo n.º 5
0
        private static string CookCommand(SlackCommand command)
        {
            dynamic responseJson = null;
            var     slackClient  = new SlackClient();

            responseJson = command.Text.StartsWith("order", ignoreCase: true, culture: CultureInfo.CurrentCulture) ?
                           slackClient.PostMenu() :
                           slackClient.SendPostMessage(command);
            return(responseJson);
        }
Exemplo n.º 6
0
 internal static FormCollection AsForm(this SlackCommand command) => new FormCollection(new Dictionary <string, StringValues>
 {
     { nameof(SlackCommand.token), command.token },
     { nameof(SlackCommand.team_id), command.team_id },
     { nameof(SlackCommand.team_domain), command.team_domain },
     { nameof(SlackCommand.enterprise_id), command.enterprise_id },
     { nameof(SlackCommand.enterprise_name), command.enterprise_name },
     { nameof(SlackCommand.channel_id), command.channel_id },
     { nameof(SlackCommand.channel_name), command.channel_name },
     { nameof(SlackCommand.user_id), command.user_id },
     { nameof(SlackCommand.user_name), command.user_name },
     { nameof(SlackCommand.command), command.command },
     { nameof(SlackCommand.text), command.text },
     { nameof(SlackCommand.response_url), command.response_url },
     { nameof(SlackCommand.trigger_id), command.trigger_id },
 });
Exemplo n.º 7
0
        public IActionResult SlackForTrigger(string @event, NameValueCollection data)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _logger.LogInformation(
                0,
                "{ControllerName} / '{ReceiverId}' received {Count} properties with event '{EventName}').",
                nameof(SlackController),
                "trigger",
                data.Count,
                @event);

            var channel = data[SlackConstants.ChannelRequestFieldName];
            var command = data[SlackConstants.CommandRequestFieldName];
            var trigger = data[SlackConstants.TriggerRequestFieldName];

            _logger.LogInformation(
                1,
                "Data contains channel '{ChannelName}', command '{Command}', and trigger '{Trigger}'.",
                channel,
                command,
                trigger);

            var text    = data[SlackConstants.TextRequestFieldName];
            var subtext = data[SlackConstants.SubtextRequestFieldName];

            _logger.LogInformation(
                2,
                "Data contains text '{Text}' and subtext '{Subtext}'.",
                text,
                subtext);

            // Create the response.
            var triggerCommand = SlackCommand.ParseActionWithValue(subtext);

            // Information can be returned using a SlackResponse.
            var reply = string.Format(
                "Received trigger '{0}' with action '{1}' and value '{2}'",
                trigger,
                triggerCommand.Key,
                triggerCommand.Value);

            return(new JsonResult(new SlackResponse(reply)));
        }
Exemplo n.º 8
0
        public string SendPostMessage(SlackCommand command)
        {
            var payload = new
            {
                Text        = "New comic book alert!",
                Attachments = new []
                {
                    new Attachment()
                    {
                        Fallback =
                            "Would you recommend it to customers?",
                        Color = "#7CD197",
                        // Title = "Would you recommend it to customers?",
                        AttachmentType = "default",
                        Actions        = new []
                        {
                            new Action
                            {
                                Name  = "recommend",
                                Text  = "Recommend",
                                Type  = "button",
                                Value = "recommend"
                            },
                            new Action
                            {
                                Name  = "no",
                                Text  = "No",
                                Type  = "button",
                                Value = "No"
                            }
                        }
                    }
                },
                command.ChannelName
            };


            var settings = new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            };

            return(JsonConvert.SerializeObject(payload, settings));
        }
Exemplo n.º 9
0
        internal static HttpContext GetIncomingCommandContext(SlackCommand command = null)
        {
            var context = new DefaultHttpContext();

            context.Request.Method = "POST";
            context.Request.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

            // https://stackoverflow.com/questions/45959605/inspect-defaulthttpcontext-body-in-unit-test-situation
            // Must set the Response.Body to a new MemoryStream because DefaultHttpContext's default is Stream.Null
            // that ignores all reads and writes.
            context.Response.Body = new MemoryStream();

            if (command != null)
            {
                context.Request.Form = command.AsForm();
            }

            return(context);
        }
Exemplo n.º 10
0
        public IActionResult SlackForCommand(string @event, string subtext, IFormCollection data)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _logger.LogInformation(
                0,
                $"{nameof(SlackController)} / 'command' received {{Count}} properties with event '{{EventName}}'.",
                data.Count,
                @event);

            string channel = data[SlackConstants.ChannelRequestFieldName];
            string command = data[SlackConstants.CommandRequestFieldName];
            string trigger = data[SlackConstants.TriggerRequestFieldName];

            _logger.LogInformation(
                1,
                "Data contains channel '{ChannelName}', command '{Command}', and trigger '{Trigger}'.",
                channel,
                command,
                trigger);

            string text = data[SlackConstants.TextRequestFieldName];

            _logger.LogInformation(
                2,
                "Data contains text '{Text}' and subtext '{Subtext}'.",
                text,
                subtext);

            var slashCommand = SlackCommand.ParseActionWithValue(command);

            // Ignore an error parsing the remainder of the command except to keep that action value together.
            var actionValue     = slashCommand.Value;
            var actionValueName = "value";
            var parameters      = SlackCommand.TryParseParameters(slashCommand.Value, out var error);

            if (error == null)
            {
                actionValue     = SlackCommand.GetNormalizedParameterString(parameters);
                actionValueName = "parameters";
            }

            // Create the response.
            var reply = $"Received slash command '{command}' with action '{slashCommand.Key}' and {actionValueName} " +
                        $"'{actionValue}'.";

            // Slash responses can be augmented with attachments containing data, images, and more.
            var attachment = 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
            attachment.Fields.Add(new SlackField("Field1", "1234"));
            attachment.Fields.Add(new SlackField("Field2", "5678"));

            return(new JsonResult(new SlackSlashResponse(reply, attachment)));
        }
Exemplo n.º 11
0
 public Task <AppResponse> ProcessAsync(SlackCommand command) => Task.FromResult((AppResponse)null);
Exemplo n.º 12
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));
        }
Exemplo n.º 13
0
        public string CreateMenu(SlackCommand command)
        {
            var channel = command.ChannelId;
            var payload = new
            {
                Text         = "Would you like to order your coffees in?",
                ResponseType = "in_channel",
                Attachments  = new []
                {
                    new Attachment()
                    {
                        Text     = "TChoose a game to play",
                        Fallback =
                            "If you could read this message, you'd be choosing something fun to do right now.",
                        Color          = "#3AA3E3",
                        AttachmentType = "default",
                        CallbackId     = "coffee_selection",
                        Actions        = new []
                        {
                            new Action
                            {
                                Name  = "confirm",
                                Text  = "Confirm",
                                Type  = "button",
                                Value = "confirm"
                            },
                            new Action
                            {
                                Name = "cancel",
                                Text = "Cancel",
                                Type = "button",
                            },
                            new Action
                            {
                                Name    = "coffees_list",
                                Text    = "Choose you order",
                                Type    = "select",
                                Options = new Option[]
                                {
                                    new Option()
                                    {
                                        Text  = "Flat White",
                                        Value = "Flat White"
                                    },
                                    new Option()
                                    {
                                        Text  = "Long Black",
                                        Value = "Long Black"
                                    },
                                    new Option()
                                    {
                                        Text  = "Coffee Latte",
                                        Value = "Coffee Latte"
                                    },
                                    new Option()
                                    {
                                        Text  = "Hot Chocolate",
                                        Value = "Hot Chocolate"
                                    }
                                }
                            }
                        }
                    }
                },
                channel
            };

            var settings = new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            };

            return(JsonConvert.SerializeObject(payload, settings));
        }
Exemplo n.º 14
0
 public Task <AppResponse> ProcessAsync(SlackCommand command) => Task.FromResult(new AppResponse());