From 8669c72fc6a559b2d1fb9baa74ab7b26a4b5c743 Mon Sep 17 00:00:00 2001 From: Bianca Hoch Date: Fri, 15 Nov 2024 16:40:17 -0500 Subject: [PATCH 1/4] added check for new user join to chat_endpoint --- cookbook/slackbot/start.py | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/cookbook/slackbot/start.py b/cookbook/slackbot/start.py index 7283c8b63..3f03fe67b 100644 --- a/cookbook/slackbot/start.py +++ b/cookbook/slackbot/start.py @@ -154,20 +154,35 @@ async def chat_endpoint(request: Request): raise HTTPException(400, "Invalid event type") match payload.type: case "event_callback": - channel_name = await get_channel_name(payload.event.channel) - if channel_name.startswith("D"): - # This is a DM channel, we should not respond - logger.warning(f"Attempted to respond in DM channel: {channel_name}") - slack_webhook = await SlackWebhook.load("marvin-bot-pager") - await slack_webhook.notify( + print(payload.event) + #check if the event is a new user joining the workspace. If so, send a welcome message. + if payload.event.type == "team_join": + user_id = payload.event.user + #get the welcome message from the variable store + message_var = await Variable.get("marvin_welcome_message") + message = (message_var["text"]) + #format the message with the user's id + f_string = message.format(user_id=user_id) + #post the message to the user's DM channel + await task(post_slack_message)( + message=(f_string), + channel_id=user_id, # type: ignore + ) + else: + channel_name = await get_channel_name(payload.event.channel) + if channel_name.startswith("D"): + # This is a DM channel, we should not respond + logger.warning(f"Attempted to respond in DM channel: {channel_name}") + slack_webhook = await SlackWebhook.load("marvin-bot-pager") + await slack_webhook.notify( body=f"Attempted to respond in DM channel: {channel_name}", subject="Slackbot DM Warning", - ) - return Completed(message="Skipped DM channel", name="SKIPPED") - options = dict( + ) + return Completed(message="Skipped DM channel", name="SKIPPED") + options = dict( flow_run_name=f"respond in {channel_name}/{payload.event.thread_ts}" ) - asyncio.create_task(handle_message.with_options(**options)(payload)) + asyncio.create_task(handle_message.with_options(**options)(payload)) case "url_verification": return {"challenge": payload.challenge} case _: From 527b901f5ff58b7579f23c93ab53372ca3b23c89 Mon Sep 17 00:00:00 2001 From: zzstoatzz Date: Fri, 15 Nov 2024 22:26:49 -0600 Subject: [PATCH 2/4] reformat --- cookbook/slackbot/start.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/cookbook/slackbot/start.py b/cookbook/slackbot/start.py index 3f03fe67b..888c5a352 100644 --- a/cookbook/slackbot/start.py +++ b/cookbook/slackbot/start.py @@ -155,15 +155,15 @@ async def chat_endpoint(request: Request): match payload.type: case "event_callback": print(payload.event) - #check if the event is a new user joining the workspace. If so, send a welcome message. + # check if the event is a new user joining the workspace. If so, send a welcome message. if payload.event.type == "team_join": user_id = payload.event.user - #get the welcome message from the variable store + # get the welcome message from the variable store message_var = await Variable.get("marvin_welcome_message") - message = (message_var["text"]) - #format the message with the user's id + message = message_var["text"] + # format the message with the user's id f_string = message.format(user_id=user_id) - #post the message to the user's DM channel + # post the message to the user's DM channel await task(post_slack_message)( message=(f_string), channel_id=user_id, # type: ignore @@ -172,16 +172,18 @@ async def chat_endpoint(request: Request): channel_name = await get_channel_name(payload.event.channel) if channel_name.startswith("D"): # This is a DM channel, we should not respond - logger.warning(f"Attempted to respond in DM channel: {channel_name}") + logger.warning( + f"Attempted to respond in DM channel: {channel_name}" + ) slack_webhook = await SlackWebhook.load("marvin-bot-pager") await slack_webhook.notify( - body=f"Attempted to respond in DM channel: {channel_name}", - subject="Slackbot DM Warning", + body=f"Attempted to respond in DM channel: {channel_name}", + subject="Slackbot DM Warning", ) return Completed(message="Skipped DM channel", name="SKIPPED") options = dict( - flow_run_name=f"respond in {channel_name}/{payload.event.thread_ts}" - ) + flow_run_name=f"respond in {channel_name}/{payload.event.thread_ts}" + ) asyncio.create_task(handle_message.with_options(**options)(payload)) case "url_verification": return {"challenge": payload.challenge} From 6c426c63a9c88222dc6a21e7e3d2c5825ce4c42c Mon Sep 17 00:00:00 2001 From: Bianca <105657837+biancaines@users.noreply.github.com> Date: Mon, 18 Nov 2024 09:16:46 -0500 Subject: [PATCH 3/4] Update cookbook/slackbot/start.py Co-authored-by: nate nowack --- cookbook/slackbot/start.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cookbook/slackbot/start.py b/cookbook/slackbot/start.py index 888c5a352..11e4c727f 100644 --- a/cookbook/slackbot/start.py +++ b/cookbook/slackbot/start.py @@ -154,7 +154,7 @@ async def chat_endpoint(request: Request): raise HTTPException(400, "Invalid event type") match payload.type: case "event_callback": - print(payload.event) + assert payload.event is not None, "No event found!" # check if the event is a new user joining the workspace. If so, send a welcome message. if payload.event.type == "team_join": user_id = payload.event.user From 4cf236d1a0e4160da427b15526937fd102c980c7 Mon Sep 17 00:00:00 2001 From: Bianca <105657837+biancaines@users.noreply.github.com> Date: Mon, 18 Nov 2024 09:17:29 -0500 Subject: [PATCH 4/4] Update cookbook/slackbot/start.py Co-authored-by: nate nowack --- cookbook/slackbot/start.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cookbook/slackbot/start.py b/cookbook/slackbot/start.py index 11e4c727f..56147d569 100644 --- a/cookbook/slackbot/start.py +++ b/cookbook/slackbot/start.py @@ -160,12 +160,12 @@ async def chat_endpoint(request: Request): user_id = payload.event.user # get the welcome message from the variable store message_var = await Variable.get("marvin_welcome_message") - message = message_var["text"] + message_template = message_var["text"] # format the message with the user's id - f_string = message.format(user_id=user_id) + rendered_message = message_template.format(user_id=user_id) # post the message to the user's DM channel await task(post_slack_message)( - message=(f_string), + message=rendered_message, channel_id=user_id, # type: ignore ) else: