1- from typing import Optional , List , Union , Any
1+ from typing import Optional , List , Union , Any , Dict
22
33from slack_sdk .models .attachments import Attachment
44from slack_sdk .models .blocks import Block , Option , OptionGroup
5+ from slack_sdk .models .views import View
56
67from slack_bolt .error import BoltError
78from slack_bolt .response import BoltResponse
8- from slack_bolt .util .utils import convert_to_dict_list
9+ from slack_bolt .util .utils import convert_to_dict_list , _to_dict
910
1011
1112def _set_response (
1213 self : Any ,
1314 text_or_whole_response : Union [str , dict ] = "" ,
1415 blocks : Optional [List [Union [dict , Block ]]] = None ,
1516 attachments : Optional [List [Union [dict , Attachment ]]] = None ,
17+ response_type : Optional [str ] = None , # in_channel / ephemeral
18+ # block_suggestion / dialog_suggestion
1619 options : Optional [List [Union [dict , Option ]]] = None ,
1720 option_groups : Optional [List [Union [dict , OptionGroup ]]] = None ,
21+ # view_submission
22+ response_action : Optional [str ] = None ,
23+ errors : Optional [Dict [str , str ]] = None ,
24+ view : Optional [Union [dict , View ]] = None ,
1825) -> BoltResponse :
1926 if isinstance (text_or_whole_response , str ):
2027 text : str = text_or_whole_response
28+ body = {"text" : text }
29+ if response_type :
30+ body ["response_type" ] = response_type
2131 if attachments and len (attachments ) > 0 :
22- self .response = BoltResponse (
23- status = 200 ,
24- body = {"text" : text , "attachments" : convert_to_dict_list (attachments ),},
32+ body .update (
33+ {"text" : text , "attachments" : convert_to_dict_list (attachments )}
2534 )
35+ self .response = BoltResponse (status = 200 , body = body )
2636 elif blocks and len (blocks ) > 0 :
27- self .response = BoltResponse (
28- status = 200 , body = {"text" : text , "blocks" : convert_to_dict_list (blocks ),}
29- )
37+ body .update ({"text" : text , "blocks" : convert_to_dict_list (blocks )})
38+ self .response = BoltResponse (status = 200 , body = body )
3039 elif options and len (options ) > 0 :
31- self .response = BoltResponse (
32- status = 200 , body = {"options" : convert_to_dict_list (options ),}
33- )
40+ body = {"options" : convert_to_dict_list (options )}
41+ self .response = BoltResponse (status = 200 , body = body )
3442 elif option_groups and len (option_groups ) > 0 :
35- self .response = BoltResponse (
36- status = 200 , body = {"option_groups" : convert_to_dict_list (option_groups ),}
37- )
43+ body = {"option_groups" : convert_to_dict_list (option_groups )}
44+ self .response = BoltResponse (status = 200 , body = body )
45+ elif response_action :
46+ # These patterns are in response to view_submission requests
47+ if response_action == "errors" :
48+ if errors :
49+ self .response = BoltResponse (
50+ status = 200 ,
51+ body = {
52+ "response_action" : response_action ,
53+ "errors" : _to_dict (errors ),
54+ },
55+ )
56+ else :
57+ raise ValueError (
58+ f"errors field is required for response_action: errors"
59+ )
60+ else :
61+ body = {"response_action" : response_action }
62+ if view :
63+ body ["view" ] = _to_dict (view )
64+ self .response = BoltResponse (status = 200 , body = body )
3865 else :
39- self .response = BoltResponse (status = 200 , body = text )
66+ if len (body ) == 1 and "text" in body :
67+ self .response = BoltResponse (status = 200 , body = body ["text" ])
68+ else :
69+ self .response = BoltResponse (status = 200 , body = body )
4070 return self .response
4171 elif isinstance (text_or_whole_response , dict ):
4272 body = text_or_whole_response
@@ -48,6 +78,14 @@ def _set_response(
4878 body ["options" ] = convert_to_dict_list (body ["options" ])
4979 if "option_groups" in body :
5080 body ["option_groups" ] = convert_to_dict_list (body ["option_groups" ])
81+ if "response_type" in body :
82+ body ["response_type" ] = body ["response_type" ]
83+ if "response_action" in body :
84+ body ["response_action" ] = body ["response_action" ]
85+ if "errors" in body :
86+ body ["errors" ] = _to_dict (body ["errors" ])
87+ if "view" in body :
88+ body ["view" ] = _to_dict (body ["view" ])
5189
5290 self .response = BoltResponse (status = 200 , body = body )
5391 return self .response
0 commit comments