Skip to content

Commit

Permalink
feat(slack): Add check function for no-bot channel.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayush0Chaudhary committed Dec 24, 2022
1 parent 5dd965c commit d6becb4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bot/slack/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
42 changes: 41 additions & 1 deletion bot/slack/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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]:
"""
Expand Down
1 change: 1 addition & 0 deletions samples/bot_manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ oauth_config:
- chat:write.customize
- commands
- files:write
- chat:write:public
settings:
org_deploy_enabled: false
socket_mode_enabled: false
Expand Down

0 comments on commit d6becb4

Please sign in to comment.