Example #1
0
        public async Task <SlackResult> HandleOptionsRequest(HttpRequest request, SlackEndpointConfiguration config)
        {
            if (request.Method != "POST")
            {
                return(new EmptyResult(HttpStatusCode.MethodNotAllowed));
            }

            await ReplaceRequestStreamWithMemoryStream(request).ConfigureAwait(false);

            var optionsRequest = await DeserializePayload <OptionsRequestBase>(request).ConfigureAwait(false);

            if (optionsRequest != null && VerifyRequest(await ReadString(request).ConfigureAwait(false), request.Headers, optionsRequest.Token, config))
            {
                switch (optionsRequest)
                {
                case OptionsRequest legacyOptionsRequest:
                    return(await HandleLegacyOptionsRequest(legacyOptionsRequest).ConfigureAwait(false));

                case BlockOptionsRequest blockOptionsRequest:
                    return(await HandleBlockOptionsRequest(blockOptionsRequest).ConfigureAwait(false));
                }
            }

            return(new StringResult(HttpStatusCode.BadRequest, "Invalid token or unrecognized content"));
        }
Example #2
0
        public async Task <SlackResult> HandleEventRequest(HttpRequest request, SlackEndpointConfiguration config)
        {
            if (request.Method != "POST")
            {
                return(new EmptyResult(HttpStatusCode.MethodNotAllowed));
            }

            if (request.ContentType != "application/json")
            {
                return(new EmptyResult(HttpStatusCode.UnsupportedMediaType));
            }

            var requestBody = await ReadString(request).ConfigureAwait(false);

            var eventRequest = DeserializeEventRequest(requestBody);

            if (!VerifyRequest(requestBody, request.Headers, eventRequest.Token, config))
            {
                return(new StringResult(HttpStatusCode.BadRequest, "Invalid signature/token"));
            }

            switch (eventRequest)
            {
            case UrlVerification urlVerification:
                return(new StringResult(HttpStatusCode.OK, urlVerification.Challenge));

            case EventCallback eventCallback:
                return(new EmptyResult(HttpStatusCode.OK)
                       .OnCompleted(() => _eventHandler.Handle(eventCallback)));

            default:
                return(new StringResult(HttpStatusCode.BadRequest, "Unrecognized content"));
            }
        }
Example #3
0
        public static IApplicationBuilder UseSlackNet(this IApplicationBuilder app, Action <SlackEndpointConfiguration> configure = null)
        {
            var config = new SlackEndpointConfiguration();

            configure?.Invoke(config);
            return(app.UseMiddleware <SlackEventsMiddleware>(config));
        }
 public SlackRequestMiddleware(
     RequestDelegate next,
     SlackEndpointConfiguration configuration,
     ISlackRequestHandler requestHandler)
 {
     _next           = next;
     _configuration  = configuration;
     _requestHandler = requestHandler;
 }
 public SlackRequestMiddleware(
     RequestDelegate next,
     SlackEndpointConfiguration configuration,
     ISlackRequestHandler requestHandler,
     SlackJsonSettings jsonSettings)
 {
     _next           = next;
     _configuration  = configuration;
     _requestHandler = requestHandler;
     _jsonSettings   = jsonSettings;
 }
Example #6
0
        /// <summary>
        /// Adds the Slack request-handling middleware to ASP.NET.
        /// By default, the following routes are configured:
        /// <br /><c>/slack/event</c> - Event subscriptions
        /// <br /><c>/slack/action</c> - Interactive component requests
        /// <br /><c>/slack/options</c> - Options loading (for message menus)
        /// <br /><c>/slack/command</c> - Slash command requests
        /// </summary>
        public static IApplicationBuilder UseSlackNet(this IApplicationBuilder app, Action <SlackEndpointConfiguration> configure = null)
        {
            var config = new SlackEndpointConfiguration();

            configure?.Invoke(config);

            if (config.SocketMode)
            {
                app.ApplicationServices.GetRequiredService <ISlackSocketModeClient>().Connect();
                return(app);
            }
            else
            {
                return(app.UseMiddleware <SlackRequestMiddleware>(config));
            }
        }
Example #7
0
        public async Task <SlackResult> HandleActionRequest(HttpRequest request, SlackEndpointConfiguration config)
        {
            if (request.Method != "POST")
            {
                return(new EmptyResult(HttpStatusCode.MethodNotAllowed));
            }

            await ReplaceRequestStreamWithMemoryStream(request).ConfigureAwait(false);

            var interactionRequest = await DeserializePayload <InteractionRequest>(request).ConfigureAwait(false);

            if (interactionRequest != null && VerifyRequest(await ReadString(request).ConfigureAwait(false), request.Headers, interactionRequest.Token, config))
            {
                switch (interactionRequest)
                {
                case BlockActionRequest blockActions:
                    return(await HandleBlockActions(blockActions).ConfigureAwait(false));

                case InteractiveMessage interactiveMessage:
                    return(await HandleInteractiveMessage(interactiveMessage).ConfigureAwait(false));

                case DialogSubmission dialogSubmission:
                    return(await HandleDialogSubmission(dialogSubmission).ConfigureAwait(false));

                case DialogCancellation dialogCancellation:
                    return(await HandleDialogCancellation(dialogCancellation).ConfigureAwait(false));

                case MessageShortcut messageShortcut:
                    return(await HandleMessageShortcut(messageShortcut).ConfigureAwait(false));

                case GlobalShortcut globalShortcut:
                    return(await HandleGlobalShortcut(globalShortcut).ConfigureAwait(false));

                case ViewSubmission viewSubmission:
                    return(await HandleViewSubmission(viewSubmission).ConfigureAwait(false));

                case ViewClosed viewClosed:
                    return(await HandleViewClosed(viewClosed).ConfigureAwait(false));

                case WorkflowStepEdit workflowStepEdit:
                    return(await HandleWorkflowStepEdit(workflowStepEdit).ConfigureAwait(false));
                }
            }

            return(new StringResult(HttpStatusCode.BadRequest, "Invalid token or unrecognized content"));
        }
 public SlackEventsMiddleware(
     RequestDelegate next,
     SlackEndpointConfiguration configuration,
     ISlackEvents slackEvents,
     ISlackActions slackActions,
     ISlackOptions slackOptions,
     IDialogSubmissionHandler dialogSubmissionHandler,
     SlackJsonSettings jsonSettings)
 {
     _next                    = next;
     _configuration           = configuration;
     _slackEvents             = slackEvents;
     _slackActions            = slackActions;
     _slackOptions            = slackOptions;
     _dialogSubmissionHandler = dialogSubmissionHandler;
     _jsonSettings            = jsonSettings;
 }
Example #9
0
 private static bool VerifyRequest(string requestBody, IHeaderDictionary headers, string token, SlackEndpointConfiguration config) =>
 !string.IsNullOrEmpty(config.SigningSecret)
         ? IsValidSignature(requestBody, headers, config.SigningSecret)
         : IsValidToken(token, config.VerificationToken);
Example #10
0
        public async Task <SlackResult> HandleSlashCommandRequest(HttpRequest request, SlackEndpointConfiguration config)
        {
            if (request.Method != "POST")
            {
                return(new EmptyResult(HttpStatusCode.MethodNotAllowed));
            }

            await ReplaceRequestStreamWithMemoryStream(request).ConfigureAwait(false);

            var command = await DeserializeForm <SlashCommand>(request).ConfigureAwait(false);

            if (!VerifyRequest(await ReadString(request).ConfigureAwait(false), request.Headers, command.Token, config))
            {
                return(new StringResult(HttpStatusCode.BadRequest, "Invalid signature/token"));
            }

            return(await RespondAsync <SlashCommandResponse>(
                       r => _slashCommandHandler.Handle(command, r),
                       response => response == null
                       ?(SlackResult) new EmptyResult(HttpStatusCode.OK)
                       : new JsonResult(_jsonSettings, HttpStatusCode.OK, new SlashCommandMessageResponse(response)),
                       () => new EmptyResult(HttpStatusCode.OK)).ConfigureAwait(false));
        }
Example #11
0
 public Task <SlackResult> HandleEventRequest(HttpRequest request, SlackEndpointConfiguration config) =>
 InRequestContext(request,
                  async requestContext =>