Skip to content

bridges: Check if bot is subscribed to relevant stream. #571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions zulip/integrations/bridge_between_zulips/run-interrealm-bridge
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ import interrealm_bridge_config

from typing import Any, Callable, Dict

def check_subscription_or_die(zulip_client, stream):
# type: (zulip.Client, str) -> None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The annotation needs to be changed to Python 3 version. Also applied to the other check_subscription_or_die.

resp = zulip_client.list_subscriptions()
if resp["result"] != "success":
print("ERROR: %s" % (resp["msg"],))
sys.exit(1)
subs = [s["name"] for s in resp["subscriptions"]]
if stream not in subs:
print("The bot is not yet subscribed to stream '%s'. Please subscribe the bot to the stream first." % (stream,))
sys.exit(1)

def create_pipe_event(to_client: zulip.Client, from_bot: Dict[str, Any],
to_bot: Dict[str, Any], stream_wide: bool
Expand Down Expand Up @@ -84,6 +94,9 @@ if __name__ == "__main__":
site=bot1["site"])
client2 = zulip.Client(email=bot2["email"], api_key=bot2["api_key"],
site=bot2["site"])
# Check whether zulip bots are subscribed to their respective streams or not
check_subscription_or_die(client1, bot1["stream"])
check_subscription_or_die(client2, bot2["stream"])
# A bidirectional tunnel
pipe_event1 = create_pipe_event(client2, bot1, bot2, args.stream)
p1 = mp.Process(target=client1.call_on_each_event, args=(pipe_event1, ["message"]))
Expand Down
13 changes: 13 additions & 0 deletions zulip/integrations/bridge_with_matrix/matrix_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ def die(signal: int, frame: FrameType) -> None:
# We actually want to exit, so run os._exit (so as not to be caught and restarted)
os._exit(1)

def check_subscription_or_die(zulip_client, stream):
# type: (zulip.Client, str) -> None
resp = zulip_client.list_subscriptions()
if resp["result"] != "success":
print("ERROR: %s" % (resp["msg"],))
sys.exit(1)
subs = [s["name"] for s in resp["subscriptions"]]
if stream not in subs:
print("The bot is not yet subscribed to stream '%s'. Please subscribe the bot to the stream first." % (stream,))
sys.exit(1)

def matrix_to_zulip(
zulip_client: zulip.Client,
zulip_config: Dict[str, Any],
Expand Down Expand Up @@ -286,6 +297,8 @@ def main() -> None:
site=zulip_config["site"])
matrix_client = MatrixClient(matrix_config["host"])

# Check whether zulip bot is subscribed to stream or not
check_subscription_or_die(zulip_client, zulip_config["stream"])
# Login to Matrix
matrix_login(matrix_client, matrix_config)
# Join a room in Matrix
Expand Down