@@ -8,8 +8,8 @@ import traceback
8
8
from typing import Any , Callable , Dict , Optional , Tuple
9
9
10
10
import bridge_with_slack_config
11
- import slack_sdk
12
11
from slack_sdk .rtm_v2 import RTMClient
12
+ from slack_sdk .web .client import WebClient
13
13
14
14
import zulip
15
15
@@ -49,7 +49,7 @@ class SlackBridge:
49
49
50
50
self .slack_to_zulip_map : Dict [str , Dict [str , str ]] = config ["channel_mapping" ]
51
51
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 ()
53
53
}
54
54
55
55
# zulip-specific
@@ -69,7 +69,7 @@ class SlackBridge:
69
69
self .slack_client = rtm
70
70
# Spawn a non-websocket client for getting the users
71
71
# 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" ])
73
73
74
74
def wrap_slack_mention_with_bracket (self , zulip_msg : Dict [str , Any ]) -> None :
75
75
words = zulip_msg ["content" ].split (" " )
@@ -123,7 +123,7 @@ class SlackBridge:
123
123
zulip_endpoint = self .slack_to_zulip_map [event ["channel" ]]
124
124
msg_data = dict (
125
125
type = "stream" ,
126
- to = zulip_endpoint ["stream " ],
126
+ to = zulip_endpoint ["channel " ],
127
127
subject = zulip_endpoint ["topic" ],
128
128
content = content ,
129
129
)
@@ -141,6 +141,10 @@ if __name__ == "__main__":
141
141
142
142
sys .path .append (os .path .join (os .path .dirname (__file__ ), ".." ))
143
143
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 ()
144
148
145
149
config : Dict [str , Any ] = bridge_with_slack_config .config
146
150
if "channel_mapping" not in config :
@@ -154,6 +158,7 @@ if __name__ == "__main__":
154
158
print ("MAKE SURE THE BOT IS SUBSCRIBED TO THE RELEVANT ZULIP STREAM(S) & SLACK CHANNEL(S)!" )
155
159
156
160
# 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.
157
162
rtm = RTMClient (token = config ["slack" ]["token" ])
158
163
159
164
backoff = zulip .RandomExponentialBackoff (timeout_success_equivalent = 300 )
@@ -164,14 +169,26 @@ if __name__ == "__main__":
164
169
zp = threading .Thread (
165
170
target = sb .zulip_client .call_on_each_message , args = (sb .zulip_to_slack (),)
166
171
)
167
- sp = threading .Thread (target = sb .run_slack_listener , args = ())
168
172
print ("Starting message handler on Zulip client" )
169
173
zp .start ()
170
- print ("Starting message handler on Slack client" )
171
- sp .start ()
172
174
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 ()
175
192
except Exception :
176
193
traceback .print_exc ()
177
194
backoff .fail ()
0 commit comments