Skip to content

Commit

Permalink
To be squashed
Browse files Browse the repository at this point in the history
  • Loading branch information
rht committed Sep 29, 2022
1 parent 0b79507 commit 1ee14e5
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions zulip/integrations/bridge_with_slack/run-slack-bridge
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import os
import sys
import threading
import traceback
from typing import Any, Callable, Dict, Tuple
from typing import Any, Callable, Dict, Union

import bridge_with_slack_config
import slack_sdk
Expand All @@ -18,23 +18,25 @@ ZULIP_MESSAGE_TEMPLATE = "**{username}**: {message}"
SLACK_MESSAGE_TEMPLATE = "<{username}> {message}"


def check_zulip_message_validity(
def get_zulip_message_slack_channel(
msg: Dict[str, Any], zulip_to_slack_map: Dict[str, Any], bot_email: str
) -> Tuple[bool, str]:
) -> Union[str, None]:
is_a_stream = msg["type"] == "stream"
if not is_a_stream:
return False, ""
return None

stream_name = msg["display_recipient"]
topic_name = msg["subject"]
stream_topic = stream_name + "-" + topic_name
stream_topic = f"{stream_name}-{topic_name}"
if stream_topic not in zulip_to_slack_map:
return False, ""
return None

# We do this to identify the messages generated from Slack -> Zulip
# and we make sure we don't forward it again to the Slack.
not_from_zulip_bot = msg["sender_email"] != bot_email
return not_from_zulip_bot, zulip_to_slack_map[stream_topic]
from_zulip_bot = msg["sender_email"] == bot_email
if from_zulip_bot:
return None
return zulip_to_slack_map[stream_topic]


class SlackBridge:
Expand All @@ -43,9 +45,9 @@ class SlackBridge:
self.zulip_config = config["zulip"]
self.slack_config = config["slack"]

self.slack_to_zulip_map = dict(config["pairs"])
self.slack_to_zulip_map: Dict[str, Dict[str, str]] = config["pairs"]
self.zulip_to_slack_map = {
z["stream"] + "-" + z["topic"]: s for s, z in config["pairs"].items()
f'{z["stream"]}-{z["topic"]}': s for s, z in config["pairs"].items()
}

# zulip-specific
Expand Down Expand Up @@ -77,10 +79,10 @@ class SlackBridge:

def zulip_to_slack(self) -> Callable[[Dict[str, Any]], None]:
def _zulip_to_slack(msg: Dict[str, Any]) -> None:
message_valid, slack_channel = check_zulip_message_validity(
slack_channel = get_zulip_message_slack_channel(
msg, self.zulip_to_slack_map, self.zulip_config["email"]
)
if message_valid:
if slack_channel is not None:
self.wrap_slack_mention_with_bracket(msg)
slack_text = SLACK_MESSAGE_TEMPLATE.format(
username=msg["sender_full_name"], message=msg["content"]
Expand Down

0 comments on commit 1ee14e5

Please sign in to comment.