Ejemplo n.º 1
0
        protected async Task <bool> Respond(SlackRequest request, SlackResponse response)
        {
            var httpResponse = await HttpClient.SendAsync(new HttpRequestMessage {
                RequestUri = new Uri(request.ResponseUrl),
                Method     = HttpMethod.Post,
                Content    = new StringContent(SerializeJson(response))
            });

            return(httpResponse.StatusCode == HttpStatusCode.OK);
        }
Ejemplo n.º 2
0
        protected async Task <bool> Respond(SlackRequest request, SlackResponse response)
        {
            var httpResponse = await HttpClient.SendAsync(new HttpRequestMessage {
                RequestUri = new Uri(request.ResponseUrl),
                Method     = HttpMethod.Post,
                Content    = new StringContent(JsonConvert.SerializeObject(response), Encoding.UTF8, "application/json")
            });

            return(httpResponse.StatusCode == HttpStatusCode.OK);
        }
Ejemplo n.º 3
0
        //--- Methods ---
        public async Task FunctionHandler(SlackRequest request, ILambdaContext context)
        {
            LambdaLogger.Log($"*** INFO: request received\n{JsonConvert.SerializeObject(request)}\n");
            using (var consoleOutWriter = new StringWriter())
                using (var consoleErrorWriter = new StringWriter()) {
                    var consoleOutOriginal   = Console.Out;
                    var consoleErrorOriginal = Console.Error;
                    try {
                        // redirect the console output and error streams so we can emit them later to slack
                        Console.SetOut(consoleOutWriter);
                        Console.SetError(consoleErrorWriter);

                        // validate the slack token (assuming one was configured)
                        if (!(_slackVerificationToken?.Equals(request.Token) ?? true))
                        {
                            throw new SlackVerificationTokenMismatchException();
                        }

                        // handle slack request
                        await HandleMessageAsync(request);
                    } catch (Exception e) {
                        LambdaLogger.Log($"*** EXCEPTION: {e.ToString()}\n");
                        Console.Error.WriteLine(e);
                    } finally {
                        Console.SetOut(consoleOutOriginal);
                        Console.SetError(consoleErrorOriginal);
                    }

                    // send console output to slack as an in_channel response
                    var output = consoleOutWriter.ToString();
                    if (output.Length > 0)
                    {
                        await RespondInChannel(request, output);
                    }

                    // send console error to slack as an ephemeral response (only visible to the requesting user)
                    var error = consoleErrorWriter.ToString();
                    if (error.Length > 0)
                    {
                        await RespondEphemeral(request, error);
                    }
                }
        }
Ejemplo n.º 4
0
 //--- Abstract Methods ---
 protected abstract Task ProcessSlackRequestAsync(SlackRequest request);
Ejemplo n.º 5
0
 protected Task <bool> RespondEphemeral(SlackRequest request, string text, params SlackResponseAttachment[] attachments)
 => Respond(request, SlackResponse.Ephemeral(text, attachments));
Ejemplo n.º 6
0
 protected Task <bool> RespondInChannel(SlackRequest request, string text, params SlackResponseAttachment[] attachments)
 => Respond(request, SlackResponse.InChannel(text, attachments));
Ejemplo n.º 7
0
 //--- Abstract Methods ---
 protected abstract Task HandleMessageAsync(SlackRequest request);