1010import com .slack .api .bolt .request .builtin .BlockActionRequest ;
1111import com .slack .api .bolt .request .builtin .EventRequest ;
1212import com .slack .api .bolt .request .builtin .ViewSubmissionRequest ;
13+ import com .slack .api .bolt .request .builtin .EventRequest ;
1314import com .slack .api .bolt .response .Response ;
1415import com .slack .api .model .event .FunctionExecutedEvent ;
1516import com .slack .api .util .json .GsonFactory ;
2627import java .util .List ;
2728import java .util .Map ;
2829import java .util .concurrent .atomic .AtomicBoolean ;
30+ import java .util .regex .Pattern ;
2931
3032import static org .junit .Assert .assertEquals ;
3133import static org .junit .Assert .assertTrue ;
@@ -356,17 +358,13 @@ public void all_function_events() throws Exception {
356358 called .set (req .getEvent ().getFunction ().getCallbackId ().equals ("hello" )
357359 && req .getEvent ().getInputs ().get ("user_id" ).asString ().equals ("U03E94MK0" )
358360 && req .getEvent ().getInputs ().get ("amount" ).asInteger ().equals (1 )
359- && req . getEvent (). getBotAccessToken (). equals ( "xwfp-this-is-valid" )
360- );
361+ && ctx . isAttachingFunctionTokenEnabled ( )
362+ && ctx . getFunctionBotAccessToken (). equals ( "xwfp-valid" ) );
361363 called .set (ctx .client ().functionsCompleteSuccess (r -> r
362- // TODO: remove this token passing by enhancing bolt internals
363- .token (req .getEvent ().getBotAccessToken ())
364364 .functionExecutionId (req .getEvent ().getFunctionExecutionId ())
365365 .outputs (new HashMap <>())
366366 ).getError ().equals ("" ));
367367 called .set (ctx .client ().functionsCompleteError (r -> r
368- // TODO: remove this token passing by enhancing bolt internals
369- .token (req .getEvent ().getBotAccessToken ())
370368 .functionExecutionId (req .getEvent ().getFunctionExecutionId ())
371369 .error ("something wrong" )
372370 ).getError ().equals ("" ));
@@ -378,6 +376,50 @@ public void all_function_events() throws Exception {
378376 assertTrue (called .get ());
379377 }
380378
379+ @ Test
380+ public void static_callback_id () throws Exception {
381+ App app = buildApp ();
382+ AtomicBoolean called = new AtomicBoolean (false );
383+ app .function ("hello" , (req , ctx ) -> {
384+ called .set (req .getEvent ().getFunction ().getCallbackId ().equals ("hello" )
385+ && req .getEvent ().getInputs ().get ("user_id" ).asString ().equals ("U03E94MK0" )
386+ && req .getEvent ().getInputs ().get ("amount" ).asInteger ().equals (1 )
387+ && ctx .isAttachingFunctionTokenEnabled ()
388+ && ctx .getFunctionBotAccessToken ().equals ("xwfp-valid" ));
389+ called .set (ctx .client ().functionsCompleteSuccess (r -> r
390+ .functionExecutionId (req .getEvent ().getFunctionExecutionId ())
391+ .outputs (new HashMap <>())
392+ ).getError ().equals ("" ));
393+ return ctx .ack ();
394+ });
395+
396+ Response response = app .run (buildEventRequest ());
397+ assertEquals (200L , response .getStatusCode ().longValue ());
398+ assertTrue (called .get ());
399+ }
400+
401+ @ Test
402+ public void regexp_callback_id () throws Exception {
403+ App app = buildApp ();
404+ AtomicBoolean called = new AtomicBoolean (false );
405+ app .function (Pattern .compile ("^he.+" ), (req , ctx ) -> {
406+ called .set (req .getEvent ().getFunction ().getCallbackId ().equals ("hello" )
407+ && req .getEvent ().getInputs ().get ("user_id" ).asString ().equals ("U03E94MK0" )
408+ && req .getEvent ().getInputs ().get ("amount" ).asInteger ().equals (1 )
409+ && ctx .isAttachingFunctionTokenEnabled ()
410+ && ctx .getFunctionBotAccessToken ().equals ("xwfp-valid" ));
411+ called .set (ctx .client ().functionsCompleteSuccess (r -> r
412+ .functionExecutionId (req .getEvent ().getFunctionExecutionId ())
413+ .outputs (new HashMap <>())
414+ ).getError ().equals ("" ));
415+ return ctx .ack ();
416+ });
417+
418+ Response response = app .run (buildEventRequest ());
419+ assertEquals (200L , response .getStatusCode ().longValue ());
420+ assertTrue (called .get ());
421+ }
422+
381423 @ Test
382424 public void button_clicks () throws Exception {
383425 App app = buildApp ();
@@ -389,8 +431,6 @@ public void button_clicks() throws Exception {
389431 && req .getPayload ().getBotAccessToken ().equals ("xwfp-this-is-valid" )
390432 );
391433 called .set (ctx .client ().functionsCompleteSuccess (r -> r
392- // TODO: remove this token passing by enhancing bolt internals
393- .token (req .getPayload ().getBotAccessToken ())
394434 .functionExecutionId (req .getPayload ().getFunctionData ().getExecutionId ())
395435 .outputs (new HashMap <>())
396436 ).getError ().equals ("" ));
@@ -413,8 +453,6 @@ public void view_submissions() throws Exception {
413453 && req .getPayload ().getBotAccessToken ().equals ("xwfp-this-is-valid" )
414454 );
415455 called .set (ctx .client ().functionsCompleteSuccess (r -> r
416- // TODO: remove this token passing by enhancing bolt internals
417- .token (req .getPayload ().getBotAccessToken ())
418456 .functionExecutionId (req .getPayload ().getFunctionData ().getExecutionId ())
419457 .outputs (new HashMap <>())
420458 ).getError ().equals ("" ));
@@ -461,4 +499,5 @@ ViewSubmissionRequest buildViewSubmissionRequest() {
461499 setRequestHeaders (body , rawHeaders , timestamp );
462500 return new ViewSubmissionRequest (body , viewSubmissionPayload , new RequestHeaders (rawHeaders ));
463501 }
502+
464503}
0 commit comments