Skip to content

Commit 3ec3c34

Browse files
committed
slack bridge: Add legacy mode for Slack Bridge.
Slack Bridge now uses the Slack Webhook integration to get messages accross from Slack instead of the legacy RTM API based connection our Slack Bridge use. This commit adds a "--legacy" argument to the script, it acts as a toggle to run the RTM API based connection to get messages accross to Zulip. It is used to ensure backwards compitability for users who want to maintain any ongoing Slack mirror.
1 parent dea60b1 commit 3ec3c34

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

zulip/integrations/bridge_with_slack/bridge_with_slack_config.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
"channel_mapping": {
1414
# Slack channel; must be channel ID
1515
"C5Z5N7R8A": {
16-
# Zulip stream
17-
"stream": "test here",
16+
# Zulip channel
17+
"channel": "test here",
1818
# Zulip topic
1919
"topic": "<- slack-bridge",
2020
},

zulip/integrations/bridge_with_slack/run-slack-bridge

+26-9
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import traceback
88
from typing import Any, Callable, Dict, Optional, Tuple
99

1010
import bridge_with_slack_config
11-
import slack_sdk
1211
from slack_sdk.rtm_v2 import RTMClient
12+
from slack_sdk.web.client import WebClient
1313

1414
import zulip
1515

@@ -49,7 +49,7 @@ class SlackBridge:
4949

5050
self.slack_to_zulip_map: Dict[str, Dict[str, str]] = config["channel_mapping"]
5151
self.zulip_to_slack_map: Dict[StreamTopicT, str] = {
52-
(z["stream"], z["topic"]): s for s, z in config["channel_mapping"].items()
52+
(z["channel"], z["topic"]): s for s, z in config["channel_mapping"].items()
5353
}
5454

5555
# zulip-specific
@@ -69,7 +69,7 @@ class SlackBridge:
6969
self.slack_client = rtm
7070
# Spawn a non-websocket client for getting the users
7171
# list and for posting messages in Slack.
72-
self.slack_webclient = slack_sdk.WebClient(token=self.slack_config["token"])
72+
self.slack_webclient = WebClient(token=self.slack_config["token"])
7373

7474
def wrap_slack_mention_with_bracket(self, zulip_msg: Dict[str, Any]) -> None:
7575
words = zulip_msg["content"].split(" ")
@@ -123,7 +123,7 @@ class SlackBridge:
123123
zulip_endpoint = self.slack_to_zulip_map[event["channel"]]
124124
msg_data = dict(
125125
type="stream",
126-
to=zulip_endpoint["stream"],
126+
to=zulip_endpoint["channel"],
127127
subject=zulip_endpoint["topic"],
128128
content=content,
129129
)
@@ -141,6 +141,10 @@ if __name__ == "__main__":
141141

142142
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
143143
parser = argparse.ArgumentParser(usage=usage)
144+
parser.add_argument(
145+
"--legacy", action="store_true", help="Run the bridge using the legacy Slack RTM API"
146+
)
147+
args = parser.parse_args()
144148

145149
config: Dict[str, Any] = bridge_with_slack_config.config
146150
if "channel_mapping" not in config:
@@ -154,6 +158,7 @@ if __name__ == "__main__":
154158
print("MAKE SURE THE BOT IS SUBSCRIBED TO THE RELEVANT ZULIP STREAM(S) & SLACK CHANNEL(S)!")
155159

156160
# We have to define rtm outside of SlackBridge because the rtm variable is used as a method decorator.
161+
# the RTM API is a legacy Slack SDK, we keep using them only to provide backwards compitability.
157162
rtm = RTMClient(token=config["slack"]["token"])
158163

159164
backoff = zulip.RandomExponentialBackoff(timeout_success_equivalent=300)
@@ -164,14 +169,26 @@ if __name__ == "__main__":
164169
zp = threading.Thread(
165170
target=sb.zulip_client.call_on_each_message, args=(sb.zulip_to_slack(),)
166171
)
167-
sp = threading.Thread(target=sb.run_slack_listener, args=())
168172
print("Starting message handler on Zulip client")
169173
zp.start()
170-
print("Starting message handler on Slack client")
171-
sp.start()
172174

173-
zp.join()
174-
sp.join()
175+
if args.legacy:
176+
sp = threading.Thread(target=sb.run_slack_listener, args=())
177+
print(
178+
"Warning! Running on legacy Slack SDK\n"
179+
"Starting message handler on Slack client"
180+
)
181+
sp.start()
182+
sp.join()
183+
zp.join()
184+
else:
185+
print(
186+
"Warning! if you haven't moved to the new Slack app,\n"
187+
"please run the script with the --legacy argument.\n"
188+
"Make sure your Slack Webhook integration is running\n"
189+
"to receive messages from Slack."
190+
)
191+
zp.join()
175192
except Exception:
176193
traceback.print_exc()
177194
backoff.fail()

0 commit comments

Comments
 (0)