Skip to content

Commit

Permalink
use member join event instead of message event
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderGi authored and devxpy committed Sep 27, 2023
1 parent f13bc88 commit 313300e
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions routers/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,12 @@ def _handle_slack_event(event: dict, background_tasks: BackgroundTasks):
if event["type"] != "event_callback":
return
message = event["event"]
if message["type"] != "message":
return
try:
match message.get("subtype", "any"):
case "channel_join":
match message.get("type", "any"):
case "member_joined_channel":
bi = BotIntegration.objects.get(
slack_channel_id=message["channel"],
slack_team_id=event["team_id"],
slack_team_id=event["team"],
)
if not bi.slack_create_personal_channels:
return
Expand All @@ -198,27 +196,31 @@ def _handle_slack_event(event: dict, background_tasks: BackgroundTasks):
raise
else:
create_personal_channel(bi, user)

case "any" | "slack_audio" | "file_share":
files = message.get("files", [])
if not files:
message.get("messsage", {}).get("files", [])
if not files:
attachments = message.get("attachments", [])
files = [
file
for attachment in attachments
for file in attachment.get("files", [])
]
bot = SlackBot(
message_ts=message["ts"],
team_id=message.get("team", event["team_id"]),
channel_id=message["channel"],
user_id=message["user"],
text=message.get("text", ""),
files=files,
)
background_tasks.add_task(_on_msg, bot)
case "message":
if message.get("subtype", "any") in [
"any",
"slack_audio",
"file_share",
]:
files = message.get("files", [])
if not files:
message.get("message", {}).get("files", [])
if not files:
attachments = message.get("attachments", [])
files = [
file
for attachment in attachments
for file in attachment.get("files", [])
]
bot = SlackBot(
message_ts=message["ts"],
team_id=message.get("team", event["team_id"]),
channel_id=message["channel"],
user_id=message["user"],
text=message.get("text", ""),
files=files,
)
background_tasks.add_task(_on_msg, bot)

except BotIntegration.DoesNotExist as e:
print(f"Error: contacted from an unknown channel - {e!r}")
Expand Down

0 comments on commit 313300e

Please sign in to comment.