示例#1
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)));
        }
        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)));
        }
示例#3
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));
        }