2018-12-13 10:47:17 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import argparse
|
2021-05-20 09:23:59 -04:00
|
|
|
import os
|
|
|
|
import sys
|
2021-05-20 09:21:14 -04:00
|
|
|
import threading
|
2021-05-20 09:23:59 -04:00
|
|
|
import traceback
|
2021-11-28 00:56:56 -05:00
|
|
|
from typing import Any, Callable, Dict, Optional, Tuple
|
2021-05-20 09:23:59 -04:00
|
|
|
|
2021-05-28 05:00:04 -04:00
|
|
|
import bridge_with_slack_config
|
2021-05-20 09:20:07 -04:00
|
|
|
import slack_sdk
|
2021-10-18 03:18:52 -04:00
|
|
|
from slack_sdk.rtm_v2 import RTMClient
|
2018-12-13 10:47:17 -05:00
|
|
|
|
2021-05-28 05:00:04 -04:00
|
|
|
import zulip
|
2018-12-13 10:47:17 -05:00
|
|
|
|
|
|
|
# change these templates to change the format of displayed message
|
|
|
|
ZULIP_MESSAGE_TEMPLATE = "**{username}**: {message}"
|
|
|
|
SLACK_MESSAGE_TEMPLATE = "<{username}> {message}"
|
|
|
|
|
2021-11-28 00:56:56 -05:00
|
|
|
StreamTopicT = Tuple[str, str]
|
2021-05-28 05:03:46 -04:00
|
|
|
|
2021-11-28 00:56:56 -05:00
|
|
|
|
|
|
|
def get_slack_channel_for_zulip_message(
|
|
|
|
msg: Dict[str, Any], zulip_to_slack_map: Dict[StreamTopicT, Any], bot_email: str
|
|
|
|
) -> Optional[str]:
|
2018-12-13 10:47:17 -05:00
|
|
|
is_a_stream = msg["type"] == "stream"
|
2021-11-28 00:56:56 -05:00
|
|
|
if not is_a_stream:
|
|
|
|
return None
|
|
|
|
|
|
|
|
stream_name = msg["display_recipient"]
|
|
|
|
topic_name = msg["subject"]
|
|
|
|
stream_topic: StreamTopicT = (stream_name, topic_name)
|
|
|
|
if stream_topic not in zulip_to_slack_map:
|
|
|
|
return None
|
2018-12-13 10:47:17 -05:00
|
|
|
|
2021-11-28 00:56:56 -05:00
|
|
|
# We do this to identify the messages generated from Slack -> Zulip
|
|
|
|
# and we make sure we don't forward it again to the Slack.
|
|
|
|
from_zulip_bot = msg["sender_email"] == bot_email
|
|
|
|
if from_zulip_bot:
|
|
|
|
return None
|
|
|
|
return zulip_to_slack_map[stream_topic]
|
2018-12-13 10:47:17 -05:00
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
|
2018-12-13 10:47:17 -05:00
|
|
|
class SlackBridge:
|
|
|
|
def __init__(self, config: Dict[str, Any]) -> None:
|
|
|
|
self.config = config
|
|
|
|
self.zulip_config = config["zulip"]
|
|
|
|
self.slack_config = config["slack"]
|
|
|
|
|
2021-11-28 00:56:56 -05:00
|
|
|
self.slack_to_zulip_map: Dict[str, Dict[str, str]] = config["channel_mapping"]
|
|
|
|
self.zulip_to_slack_map: Dict[StreamTopicT, str] = {
|
|
|
|
(z["stream"], z["topic"]): s for s, z in config["channel_mapping"].items()
|
|
|
|
}
|
|
|
|
|
2018-12-13 10:47:17 -05:00
|
|
|
# zulip-specific
|
|
|
|
self.zulip_client = zulip.Client(
|
|
|
|
email=self.zulip_config["email"],
|
|
|
|
api_key=self.zulip_config["api_key"],
|
2021-05-28 05:03:46 -04:00
|
|
|
site=self.zulip_config["site"],
|
|
|
|
)
|
2018-12-13 10:47:17 -05:00
|
|
|
|
|
|
|
# slack-specific
|
|
|
|
self.channel = self.slack_config["channel"]
|
2021-10-18 03:18:52 -04:00
|
|
|
self.slack_client = rtm
|
2020-07-25 01:08:12 -04:00
|
|
|
# Spawn a non-websocket client for getting the users
|
|
|
|
# list and for posting messages in Slack.
|
2021-05-20 09:20:07 -04:00
|
|
|
self.slack_webclient = slack_sdk.WebClient(token=self.slack_config["token"])
|
2018-12-13 10:47:17 -05:00
|
|
|
|
|
|
|
def wrap_slack_mention_with_bracket(self, zulip_msg: Dict[str, Any]) -> None:
|
2021-05-28 05:05:11 -04:00
|
|
|
words = zulip_msg["content"].split(" ")
|
2018-12-13 10:47:17 -05:00
|
|
|
for w in words:
|
2021-05-28 05:05:11 -04:00
|
|
|
if w.startswith("@"):
|
|
|
|
zulip_msg["content"] = zulip_msg["content"].replace(w, "<" + w + ">")
|
2018-12-13 10:47:17 -05:00
|
|
|
|
|
|
|
def replace_slack_id_with_name(self, msg: Dict[str, Any]) -> None:
|
2021-05-28 05:05:11 -04:00
|
|
|
words = msg["text"].split(" ")
|
2018-12-13 10:47:17 -05:00
|
|
|
for w in words:
|
2021-05-28 05:05:11 -04:00
|
|
|
if w.startswith("<@") and w.endswith(">"):
|
2018-12-13 10:47:17 -05:00
|
|
|
_id = w[2:-1]
|
2021-05-28 05:05:11 -04:00
|
|
|
msg["text"] = msg["text"].replace(_id, self.slack_id_to_name[_id])
|
2018-12-13 10:47:17 -05:00
|
|
|
|
|
|
|
def zulip_to_slack(self) -> Callable[[Dict[str, Any]], None]:
|
|
|
|
def _zulip_to_slack(msg: Dict[str, Any]) -> None:
|
2021-11-28 00:56:56 -05:00
|
|
|
slack_channel = get_slack_channel_for_zulip_message(
|
|
|
|
msg, self.zulip_to_slack_map, self.zulip_config["email"]
|
|
|
|
)
|
|
|
|
if slack_channel is not None:
|
2018-12-13 10:47:17 -05:00
|
|
|
self.wrap_slack_mention_with_bracket(msg)
|
2021-05-28 05:03:46 -04:00
|
|
|
slack_text = SLACK_MESSAGE_TEMPLATE.format(
|
|
|
|
username=msg["sender_full_name"], message=msg["content"]
|
|
|
|
)
|
2020-07-25 01:08:12 -04:00
|
|
|
self.slack_webclient.chat_postMessage(
|
2021-11-28 00:56:56 -05:00
|
|
|
channel=slack_channel,
|
2020-07-25 01:08:12 -04:00
|
|
|
text=slack_text,
|
2018-12-13 10:47:17 -05:00
|
|
|
)
|
2021-05-28 05:03:46 -04:00
|
|
|
|
2018-12-13 10:47:17 -05:00
|
|
|
return _zulip_to_slack
|
|
|
|
|
|
|
|
def run_slack_listener(self) -> None:
|
2021-05-28 05:05:11 -04:00
|
|
|
members = self.slack_webclient.users_list()["members"]
|
2018-12-13 10:47:17 -05:00
|
|
|
# See also https://api.slack.com/changelog/2017-09-the-one-about-usernames
|
2021-10-18 03:18:52 -04:00
|
|
|
self.slack_id_to_name: Dict[str, str] = {
|
2021-05-28 05:03:46 -04:00
|
|
|
u["id"]: u["profile"].get("display_name", u["profile"]["real_name"]) for u in members
|
|
|
|
}
|
2018-12-13 10:47:17 -05:00
|
|
|
self.slack_name_to_id = {v: k for k, v in self.slack_id_to_name.items()}
|
|
|
|
|
2021-10-18 03:18:52 -04:00
|
|
|
@rtm.on("message")
|
|
|
|
def slack_to_zulip(client: RTMClient, event: Dict[str, Any]) -> None:
|
2021-11-28 00:56:56 -05:00
|
|
|
if event["channel"] not in self.slack_to_zulip_map:
|
2018-12-13 10:47:17 -05:00
|
|
|
return
|
2021-10-18 03:18:52 -04:00
|
|
|
user_id = event["user"]
|
2018-12-13 10:47:17 -05:00
|
|
|
user = self.slack_id_to_name[user_id]
|
2021-05-28 05:05:11 -04:00
|
|
|
from_bot = user == self.slack_config["username"]
|
2018-12-13 10:47:17 -05:00
|
|
|
if from_bot:
|
|
|
|
return
|
2021-10-18 03:18:52 -04:00
|
|
|
self.replace_slack_id_with_name(event)
|
|
|
|
content = ZULIP_MESSAGE_TEMPLATE.format(username=user, message=event["text"])
|
2021-11-28 00:56:56 -05:00
|
|
|
zulip_endpoint = self.slack_to_zulip_map[event["channel"]]
|
2018-12-13 10:47:17 -05:00
|
|
|
msg_data = dict(
|
2021-11-28 00:56:56 -05:00
|
|
|
type="stream",
|
|
|
|
to=zulip_endpoint["stream"],
|
|
|
|
subject=zulip_endpoint["topic"],
|
|
|
|
content=content,
|
2021-05-28 05:03:46 -04:00
|
|
|
)
|
2018-12-13 10:47:17 -05:00
|
|
|
self.zulip_client.send_message(msg_data)
|
|
|
|
|
|
|
|
self.slack_client.start()
|
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
|
2018-12-13 10:47:17 -05:00
|
|
|
if __name__ == "__main__":
|
|
|
|
usage = """run-slack-bridge
|
|
|
|
|
|
|
|
Relay each message received at a specified subject in a specified stream from
|
|
|
|
the first realm to a channel in a Slack workspace.
|
|
|
|
"""
|
|
|
|
|
2021-05-28 05:05:11 -04:00
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
|
2018-12-13 10:47:17 -05:00
|
|
|
parser = argparse.ArgumentParser(usage=usage)
|
|
|
|
|
2021-11-28 00:56:56 -05:00
|
|
|
config: Dict[str, Any] = bridge_with_slack_config.config
|
|
|
|
if "channel_mapping" not in config:
|
|
|
|
print(
|
|
|
|
'The key "channel_mapping" is not found in bridge_with_slack_config.py.\n'
|
|
|
|
"Your config file may be outdated."
|
|
|
|
)
|
|
|
|
exit(1)
|
|
|
|
|
2018-12-13 10:47:17 -05:00
|
|
|
print("Starting slack mirroring bot")
|
|
|
|
print("MAKE SURE THE BOT IS SUBSCRIBED TO THE RELEVANT ZULIP STREAM")
|
|
|
|
|
2021-10-18 03:18:52 -04:00
|
|
|
# We have to define rtm outside of SlackBridge because the rtm variable is used as a method decorator.
|
|
|
|
rtm = RTMClient(token=config["slack"]["token"])
|
|
|
|
|
2018-12-13 10:47:17 -05:00
|
|
|
backoff = zulip.RandomExponentialBackoff(timeout_success_equivalent=300)
|
|
|
|
while backoff.keep_going():
|
|
|
|
try:
|
|
|
|
sb = SlackBridge(config)
|
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
zp = threading.Thread(
|
|
|
|
target=sb.zulip_client.call_on_each_message, args=(sb.zulip_to_slack(),)
|
|
|
|
)
|
2021-05-20 09:21:14 -04:00
|
|
|
sp = threading.Thread(target=sb.run_slack_listener, args=())
|
2018-12-13 10:47:17 -05:00
|
|
|
print("Starting message handler on Zulip client")
|
|
|
|
zp.start()
|
|
|
|
print("Starting message handler on Slack client")
|
|
|
|
sp.start()
|
|
|
|
|
|
|
|
zp.join()
|
|
|
|
sp.join()
|
|
|
|
except Exception:
|
|
|
|
traceback.print_exc()
|
|
|
|
backoff.fail()
|