1+ using System . Collections . Generic ;
12using System . IO ;
23using System . Linq ;
34using System . Net ;
45using System . Threading . Tasks ;
56using Microsoft . AspNetCore . Http ;
67using Newtonsoft . Json ;
78using SlackNet . Events ;
9+ using SlackNet . Interaction ;
810
911namespace SlackNet . AspNetCore
1012{
@@ -15,21 +17,24 @@ class SlackEventsMiddleware
1517 private readonly ISlackEvents _slackEvents ;
1618 private readonly ISlackActions _slackActions ;
1719 private readonly ISlackOptions _slackOptions ;
20+ private readonly IDialogSubmissionHandler _dialogSubmissionHandler ;
1821 private readonly SlackJsonSettings _jsonSettings ;
1922
2023 public SlackEventsMiddleware (
21- RequestDelegate next ,
22- SlackEndpointConfiguration configuration ,
23- ISlackEvents slackEvents ,
24- ISlackActions slackActions ,
24+ RequestDelegate next ,
25+ SlackEndpointConfiguration configuration ,
26+ ISlackEvents slackEvents ,
27+ ISlackActions slackActions ,
2528 ISlackOptions slackOptions ,
29+ IDialogSubmissionHandler dialogSubmissionHandler ,
2630 SlackJsonSettings jsonSettings )
2731 {
2832 _next = next ;
2933 _configuration = configuration ;
3034 _slackEvents = slackEvents ;
3135 _slackActions = slackActions ;
3236 _slackOptions = slackOptions ;
37+ _dialogSubmissionHandler = dialogSubmissionHandler ;
3338 _jsonSettings = jsonSettings ;
3439 }
3540
@@ -60,8 +65,9 @@ private async Task<HttpResponse> HandleSlackEvent(HttpContext context)
6065
6166 if ( body is EventCallback eventCallback && IsValidToken ( eventCallback . Token ) )
6267 {
68+ var response = context . Respond ( HttpStatusCode . OK ) . ConfigureAwait ( false ) ;
6369 _slackEvents . Handle ( eventCallback ) ;
64- return await context . Respond ( HttpStatusCode . OK ) . ConfigureAwait ( false ) ;
70+ return await response ;
6571 }
6672
6773 return await context . Respond ( HttpStatusCode . BadRequest , body : "Invalid token or unrecognized content" ) . ConfigureAwait ( false ) ;
@@ -72,22 +78,43 @@ private async Task<HttpResponse> HandleSlackAction(HttpContext context)
7278 if ( context . Request . Method != "POST" )
7379 return await context . Respond ( HttpStatusCode . MethodNotAllowed ) . ConfigureAwait ( false ) ;
7480
75- var interactiveMessage = await DeserializePayload < InteractiveMessage > ( context ) . ConfigureAwait ( false ) ;
81+ var interactionRequest = await DeserializePayload < InteractionRequest > ( context ) . ConfigureAwait ( false ) ;
7682
77- if ( interactiveMessage != null && IsValidToken ( interactiveMessage . Token ) )
83+ if ( interactionRequest != null && IsValidToken ( interactionRequest . Token ) )
7884 {
79- var response = await _slackActions . Handle ( interactiveMessage ) . ConfigureAwait ( false ) ;
80-
81- var responseJson = response == null ? null
82- : interactiveMessage . IsAppUnfurl ? Serialize ( new AttachmentUpdateResponse ( response ) )
83- : Serialize ( new MessageUpdateResponse ( response ) ) ;
84-
85- return await context . Respond ( HttpStatusCode . OK , "application/json" , responseJson ) . ConfigureAwait ( false ) ;
85+ switch ( interactionRequest )
86+ {
87+ case InteractiveMessage interactiveMessage :
88+ return await HandleInteractiveMessage ( context , interactiveMessage ) . ConfigureAwait ( false ) ;
89+ case DialogSubmission dialogSubmission :
90+ return await HandleDialogSubmission ( context , dialogSubmission ) . ConfigureAwait ( false ) ;
91+ }
8692 }
8793
8894 return await context . Respond ( HttpStatusCode . BadRequest , body : "Invalid token or unrecognized content" ) . ConfigureAwait ( false ) ;
8995 }
9096
97+ private async Task < HttpResponse > HandleInteractiveMessage ( HttpContext context , InteractiveMessage interactiveMessage )
98+ {
99+ var response = await _slackActions . Handle ( interactiveMessage ) . ConfigureAwait ( false ) ;
100+
101+ var responseJson = response == null ? null
102+ : interactiveMessage . IsAppUnfurl ? Serialize ( new AttachmentUpdateResponse ( response ) )
103+ : Serialize ( new MessageUpdateResponse ( response ) ) ;
104+
105+ return await context . Respond ( HttpStatusCode . OK , "application/json" , responseJson ) . ConfigureAwait ( false ) ;
106+ }
107+
108+ private async Task < HttpResponse > HandleDialogSubmission ( HttpContext context , DialogSubmission dialog )
109+ {
110+ var errors = ( await _dialogSubmissionHandler . Handle ( dialog ) . ConfigureAwait ( false ) ) ? . ToList ( )
111+ ?? new List < DialogError > ( ) ;
112+
113+ return errors . Any ( )
114+ ? await context . Respond ( HttpStatusCode . OK , "application/json" , Serialize ( new DialogErrorResponse { Errors = errors } ) ) . ConfigureAwait ( false )
115+ : await context . Respond ( HttpStatusCode . OK ) . ConfigureAwait ( false ) ;
116+ }
117+
91118 private async Task < HttpResponse > HandleSlackOptions ( HttpContext context )
92119 {
93120 if ( context . Request . Method != "POST" )
0 commit comments