Skip to content

Commit

Permalink
Merge pull request #993 from PrefectHQ/marvin-message
Browse files Browse the repository at this point in the history
added check for new user join to chat_endpoint
  • Loading branch information
zzstoatzz authored Nov 18, 2024
2 parents 050bf1a + 4cf236d commit 7173e37
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions cookbook/slackbot/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,37 @@ 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(
body=f"Attempted to respond in DM channel: {channel_name}",
subject="Slackbot DM Warning",
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
# get the welcome message from the variable store
message_var = await Variable.get("marvin_welcome_message")
message_template = message_var["text"]
# format the message with the user's 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=rendered_message,
channel_id=user_id, # type: ignore
)
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))
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(
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}
case _:
Expand Down

0 comments on commit 7173e37

Please sign in to comment.