slack-bridge: Use WebClient to post message in Slack.

This commit is contained in:
rht 2020-07-25 01:08:12 -04:00 committed by Tim Abbott
parent 47a4f258d7
commit e992f14522

View file

@ -45,6 +45,9 @@ class SlackBridge:
# slack-specific # slack-specific
self.channel = self.slack_config["channel"] self.channel = self.slack_config["channel"]
self.slack_client = slack.RTMClient(token=self.slack_config["token"], auto_reconnect=True) self.slack_client = slack.RTMClient(token=self.slack_config["token"], auto_reconnect=True)
# Spawn a non-websocket client for getting the users
# list and for posting messages in Slack.
self.slack_webclient = slack.WebClient(token=self.slack_config["token"])
def wrap_slack_mention_with_bracket(self, zulip_msg: Dict[str, Any]) -> None: def wrap_slack_mention_with_bracket(self, zulip_msg: Dict[str, Any]) -> None:
words = zulip_msg["content"].split(' ') words = zulip_msg["content"].split(' ')
@ -66,16 +69,14 @@ class SlackBridge:
self.wrap_slack_mention_with_bracket(msg) self.wrap_slack_mention_with_bracket(msg)
slack_text = SLACK_MESSAGE_TEMPLATE.format(username=msg["sender_full_name"], slack_text = SLACK_MESSAGE_TEMPLATE.format(username=msg["sender_full_name"],
message=msg["content"]) message=msg["content"])
self.slack_client.rtm_send_message( self.slack_webclient.chat_postMessage(
self.channel, channel=self.channel,
slack_text, text=slack_text,
) )
return _zulip_to_slack return _zulip_to_slack
def run_slack_listener(self) -> None: def run_slack_listener(self) -> None:
# spawn a non-websocket client for getting the users list members = self.slack_webclient.users_list()['members']
_wc = slack.WebClient(token=self.slack_config["token"])
members = _wc.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 = {u["id"]: u["profile"].get("display_name", u["profile"]["real_name"]) for u in members} self.slack_id_to_name = {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()}