From ea6923e700f6340a46886ae7ecfd253c4ad64669 Mon Sep 17 00:00:00 2001 From: PieterCK Date: Tue, 11 Jun 2024 21:39:24 +0700 Subject: [PATCH] slack bridge: Add logic to prevent looping messages. When using Slack Webhook integration to get messages from Slack to Zulip, we don't want to send back messages from the Slack integration bot. This prevents that by filtering out any messages from the Slack Webhook bots when sending messages from Zulip to Slack.. Fixes #825. --- .../bridge_with_slack/bridge_with_slack_config.py | 1 + zulip/integrations/bridge_with_slack/run-slack-bridge | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/zulip/integrations/bridge_with_slack/bridge_with_slack_config.py b/zulip/integrations/bridge_with_slack/bridge_with_slack_config.py index b6e759471..cba44fd6e 100644 --- a/zulip/integrations/bridge_with_slack/bridge_with_slack_config.py +++ b/zulip/integrations/bridge_with_slack/bridge_with_slack_config.py @@ -3,6 +3,7 @@ "email": "zulip-bot@email.com", "api_key": "put api key here", "site": "https://chat.zulip.org", + "integration_bot_email": "slack-bot@zulip.com", }, "slack": { "username": "slack_username", diff --git a/zulip/integrations/bridge_with_slack/run-slack-bridge b/zulip/integrations/bridge_with_slack/run-slack-bridge index ff90ca807..502be2b32 100755 --- a/zulip/integrations/bridge_with_slack/run-slack-bridge +++ b/zulip/integrations/bridge_with_slack/run-slack-bridge @@ -84,12 +84,18 @@ class SlackBridge: if w.startswith("@"): zulip_msg["content"] = zulip_msg["content"].replace(w, "<" + w + ">") + def is_message_from_slack(self, msg: Dict[str, Any]) -> bool: + # Check whether or not this message is from Slack to prevent + # them from being tossed back to Zulip. + return msg["sender_email"] == self.zulip_config.get("integration_bot_email") + def zulip_to_slack(self) -> Callable[[Dict[str, Any]], None]: def _zulip_to_slack(msg: Dict[str, Any]) -> None: slack_channel = get_slack_channel_for_zulip_message( msg, self.zulip_to_slack_map, self.zulip_config["email"] ) - if slack_channel is not None: + + if slack_channel is not None and not self.is_message_from_slack(msg): self.wrap_slack_mention_with_bracket(msg) slack_text = SLACK_MESSAGE_TEMPLATE.format( username=msg["sender_full_name"], message=msg["content"]