1- from typing import Optional , Union , Dict , Sequence
1+ from typing import Optional , Union , Dict , Sequence , Callable
22
33from slack_sdk import WebClient
44from slack_sdk .models .attachments import Attachment
@@ -15,18 +15,21 @@ class Say:
1515 channel : Optional [str ]
1616 thread_ts : Optional [str ]
1717 metadata : Optional [Union [Dict , Metadata ]]
18+ build_metadata : Optional [Callable [[], Optional [Union [Dict , Metadata ]]]]
1819
1920 def __init__ (
2021 self ,
2122 client : Optional [WebClient ],
2223 channel : Optional [str ],
2324 thread_ts : Optional [str ] = None ,
2425 metadata : Optional [Union [Dict , Metadata ]] = None ,
26+ build_metadata : Optional [Callable [[], Optional [Union [Dict , Metadata ]]]] = None ,
2527 ):
2628 self .client = client
2729 self .channel = channel
2830 self .thread_ts = thread_ts
2931 self .metadata = metadata
32+ self .build_metadata = build_metadata
3033
3134 def __call__ (
3235 self ,
@@ -52,6 +55,8 @@ def __call__(
5255 text_or_whole_response : Union [str , dict ] = text
5356 if isinstance (text_or_whole_response , str ):
5457 text = text_or_whole_response
58+ if metadata is None :
59+ metadata = self .build_metadata () if self .build_metadata is not None else self .metadata
5560 return self .client .chat_postMessage ( # type: ignore[union-attr]
5661 channel = channel or self .channel , # type: ignore[arg-type]
5762 text = text ,
@@ -68,7 +73,7 @@ def __call__(
6873 mrkdwn = mrkdwn ,
6974 link_names = link_names ,
7075 parse = parse ,
71- metadata = metadata or self . metadata ,
76+ metadata = metadata ,
7277 ** kwargs ,
7378 )
7479 elif isinstance (text_or_whole_response , dict ):
@@ -78,7 +83,8 @@ def __call__(
7883 if "thread_ts" not in message :
7984 message ["thread_ts" ] = thread_ts or self .thread_ts
8085 if "metadata" not in message :
81- message ["metadata" ] = metadata or self .metadata
86+ metadata = self .build_metadata () if self .build_metadata is not None else self .metadata
87+ message ["metadata" ] = metadata
8288 return self .client .chat_postMessage (** message ) # type: ignore[union-attr]
8389 else :
8490 raise ValueError (f"The arg is unexpected type ({ type (text_or_whole_response )} )" )
0 commit comments