diff --git a/bot/slack/bot.py b/bot/slack/bot.py index 9d2bc35..1f99d71 100644 --- a/bot/slack/bot.py +++ b/bot/slack/bot.py @@ -29,4 +29,4 @@ def __init__( secret: str, ): Messenger.__init__(self, token) - Runner.__init__(self, logger, base_url, secret) + Runner.__init__(self, logger, base_url, secret, token) diff --git a/bot/slack/runner.py b/bot/slack/runner.py index f24b74b..3e6665a 100644 --- a/bot/slack/runner.py +++ b/bot/slack/runner.py @@ -9,6 +9,9 @@ from typing import Any from bottle import MultiDict, WSGIHeaderDict +from sentry_sdk import capture_message +from slack.errors import SlackApiError +from slack.web.client import WebClient from ..models.github import EventType, convert_keywords_to_events from ..utils.json import JSON @@ -29,11 +32,13 @@ def __init__( logger: Logger, base_url: str, secret: str, + token: str, ): SlackBotBase.__init__(self) self.logger = logger self.base_url = base_url self.secret = secret.encode("utf-8") + self.client = WebClient(token=token) def verify( self, @@ -120,7 +125,21 @@ def run_subscribe_command( :param current_channel: Name of the current channel. :param args: `list` of events to subscribe to. """ - + in_channel = self.check_bot_in_channel(current_channel=current_channel) + if not in_channel: + return { + "response_type": + "ephemeral", + "blocks": [{ + "type": "section", + "text": { + "type": + "mrkdwn", + "text": + "I can not subscribed this repo to the channel,\n beacuse I am not in this channel, \nIf you need me to send you updates, invite me in this channel first." + } + }] + } repository = args[0] if repository.find('/') == -1: return self.send_wrong_syntax_message() @@ -289,6 +308,27 @@ def run_list_command( "blocks": blocks, } + def check_bot_in_channel( + self, + current_channel: str, + ) -> bool: + subscriptions = self.storage.get_subscriptions(channel=current_channel) + print(subscriptions) + if len(subscriptions) != 0: + return True + try: + is_private = self.client.conversations_info( + channel=current_channel)["channel"]["is_private"] + if is_private: + if len(subscriptions) == 0: + return False + else: + return True + except SlackApiError as E: + capture_message( + f"SlackApiError {E} Failed to fetch conversation info for {current_channel}" + ) + @staticmethod def run_help_command(args: list[str]) -> dict[str, Any]: """ diff --git a/samples/bot_manifest.yml b/samples/bot_manifest.yml index cdc684d..098ad2c 100644 --- a/samples/bot_manifest.yml +++ b/samples/bot_manifest.yml @@ -33,6 +33,7 @@ oauth_config: - chat:write.customize - commands - files:write + - chat:write:public settings: org_deploy_enabled: false socket_mode_enabled: false