Slack bridge: Bump slack-sdk to 3.11.2.

We also upgrade the RTM client API from v1 to v2. This is so that we no
longer require aiohttp. If we use v1, it would still require aiohttp.
This commit is contained in:
rht 2021-10-18 03:18:52 -04:00 committed by Tim Abbott
parent fec8cc50c4
commit 58e51c7ae5
2 changed files with 13 additions and 12 deletions

View file

@ -1,2 +1 @@
slack-sdk==3.5.1 slack-sdk==3.11.2
aiohttp

View file

@ -9,7 +9,7 @@ from typing import Any, Callable, Dict
import bridge_with_slack_config import bridge_with_slack_config
import slack_sdk import slack_sdk
from slack_sdk.rtm import RTMClient from slack_sdk.rtm_v2 import RTMClient
import zulip import zulip
@ -48,7 +48,7 @@ class SlackBridge:
# slack-specific # slack-specific
self.channel = self.slack_config["channel"] self.channel = self.slack_config["channel"]
self.slack_client = RTMClient(token=self.slack_config["token"], auto_reconnect=True) self.slack_client = rtm
# Spawn a non-websocket client for getting the users # Spawn a non-websocket client for getting the users
# list and for posting messages in Slack. # list and for posting messages in Slack.
self.slack_webclient = slack_sdk.WebClient(token=self.slack_config["token"]) self.slack_webclient = slack_sdk.WebClient(token=self.slack_config["token"])
@ -84,23 +84,22 @@ class SlackBridge:
def run_slack_listener(self) -> None: def run_slack_listener(self) -> None:
members = self.slack_webclient.users_list()["members"] members = self.slack_webclient.users_list()["members"]
# See also https://api.slack.com/changelog/2017-09-the-one-about-usernames # See also https://api.slack.com/changelog/2017-09-the-one-about-usernames
self.slack_id_to_name = { self.slack_id_to_name: Dict[str, str] = {
u["id"]: u["profile"].get("display_name", u["profile"]["real_name"]) for u in members u["id"]: u["profile"].get("display_name", u["profile"]["real_name"]) for u in members
} }
self.slack_name_to_id = {v: k for k, v in self.slack_id_to_name.items()} self.slack_name_to_id = {v: k for k, v in self.slack_id_to_name.items()}
@RTMClient.run_on(event="message") @rtm.on("message")
def slack_to_zulip(**payload: Any) -> None: def slack_to_zulip(client: RTMClient, event: Dict[str, Any]) -> None:
msg = payload["data"] if event["channel"] != self.channel:
if msg["channel"] != self.channel:
return return
user_id = msg["user"] user_id = event["user"]
user = self.slack_id_to_name[user_id] user = self.slack_id_to_name[user_id]
from_bot = user == self.slack_config["username"] from_bot = user == self.slack_config["username"]
if from_bot: if from_bot:
return return
self.replace_slack_id_with_name(msg) self.replace_slack_id_with_name(event)
content = ZULIP_MESSAGE_TEMPLATE.format(username=user, message=msg["text"]) content = ZULIP_MESSAGE_TEMPLATE.format(username=user, message=event["text"])
msg_data = dict( msg_data = dict(
type="stream", to=self.zulip_stream, subject=self.zulip_subject, content=content type="stream", to=self.zulip_stream, subject=self.zulip_subject, content=content
) )
@ -124,6 +123,9 @@ if __name__ == "__main__":
config = bridge_with_slack_config.config config = bridge_with_slack_config.config
# We have to define rtm outside of SlackBridge because the rtm variable is used as a method decorator.
rtm = RTMClient(token=config["slack"]["token"])
backoff = zulip.RandomExponentialBackoff(timeout_success_equivalent=300) backoff = zulip.RandomExponentialBackoff(timeout_success_equivalent=300)
while backoff.keep_going(): while backoff.keep_going():
try: try: