From 7236ab8034abb61b6f331f79c2b6cc2738130e78 Mon Sep 17 00:00:00 2001 From: Jacob Henry Date: Sat, 2 Mar 2019 22:38:04 -0500 Subject: [PATCH] Began block work --- main.py | 20 ++++++++++++++++++++ slack_util.py | 27 +++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index a25b861..1bc78a6 100644 --- a/main.py +++ b/main.py @@ -56,6 +56,25 @@ def main() -> None: event_handling = wrap.handle_events() passive_handling = wrap.run_passives() both = asyncio.gather(event_handling, passive_handling) + + wrap.send_message("test", "#botzone", blocks=[ + { + "type": "actions", + "block_id": "test_block_id", + "elements": [ + { + "type": "button", + "action_id": "test_action_id", + "text": { + "type": "plain_text", + "text": "Send payload", + "emoji": False + } + } + ] + } + ]) + event_loop.run_until_complete(both) @@ -90,6 +109,7 @@ async def help_callback(event: slack_util.Event, match: Match) -> None: # Do not let my efforts fall to waste. Its a pitious legacy but its something, at least, to maybe tide the # unending flow of work for poor Niko. + # run main if __name__ == '__main__': main() diff --git a/slack_util.py b/slack_util.py index 853f39f..f67494e 100644 --- a/slack_util.py +++ b/slack_util.py @@ -283,7 +283,6 @@ class ClientWrapper(object): # Handle each action separately if "actions" in payload: for action in payload["actions"]: - # Start building the event ev = Event() @@ -405,7 +404,8 @@ class ClientWrapper(object): else: return self.send_message(text, event.conversation.conversation_id) - def send_message(self, text: str, channel_id: str, thread: str = None, broadcast: bool = False) -> dict: + def _send_core(self, api_method: str, text: str, channel_id: str, thread: str, broadcast: bool, + blocks: List[dict]) -> dict: """ Copy of the internal send message function of slack, with some helpful options. Returns the JSON response. @@ -415,9 +415,32 @@ class ClientWrapper(object): kwargs["thread_ts"] = thread if broadcast: kwargs["reply_broadcast"] = True + if blocks: + kwargs["blocks"] = blocks return self.api_call("chat.postMessage", **kwargs) + def send_message(self, + text: str, + channel_id: str, + thread: str = None, + broadcast: bool = False, + blocks: List[dict] = None) -> dict: + """ + Wraps _send_core for normal messages + """ + return self._send_core("chat.postMessage", text, channel_id, thread, broadcast, blocks) + + def send_ephemeral(self, + text: str, + channel_id: str, + thread: str = None, + blocks: List[dict] = None) -> dict: + """ + Wraps _send_core for ephemeral messages + """ + return self._send_core("chat.postEphemeral", text, channel_id, thread, False, blocks) + # Update slack data def update_channels(self):