Skip to content

Commit

Permalink
Add handler for bennett-admins messages
Browse files Browse the repository at this point in the history
  • Loading branch information
inglesp committed Apr 11, 2024
1 parent dc17c64 commit 92ff4d9
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions DEPLOY.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ See also comments in `ebmbot/settings.py`.

The following slack environment variables need to be set:
- `SLACK_LOGS_CHANNEL`: channel where scheduled job notifications will be posted
- `SLACK_BENNETT_ADMINS_CHANNEL`: channel where bennett-admins requests will be reposted
- `SLACK_TECH_SUPPORT_CHANNEL`: channel where tech-support requests will be reposted
- `SLACK_APP_TOKEN`: app-level token generated above (starts `xapp-`); found on the app's Basic Information page
- `SLACK_BOT_TOKEN`: bot token generated above (starts `xoxb-`); found on the app's Oauth and Permissions page
Expand Down
1 change: 1 addition & 0 deletions dotenv-sample
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
LOGS_DIR=changeme
WRITEABLE_DIR=.
SLACK_LOGS_CHANNEL=changeme
SLACK_BENNETT_ADMINS_CHANNEL=changeme
SLACK_TECH_SUPPORT_CHANNEL=changeme
SLACK_BOT_TOKEN=changeme
SLACK_APP_TOKEN=changeme
Expand Down
16 changes: 16 additions & 0 deletions ebmbot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ def register_listeners(app, config, channels, bot_user_id, internal_user_ids):
passed in for tests.
"""

bennett_admins_channel_id = channels[settings.SLACK_BENNETT_ADMINS_CHANNEL]
# Match "bennett-admins" as a word (treating hyphens as word characters), except if
# it's preceded by a slash to avoid matching it in URLs
bennett_admins_regex = re.compile(
r".*(^|[^\w\-/])bennett-admins($|[^\w\-]).*", flags=re.I
)

tech_support_channel_id = channels[settings.SLACK_TECH_SUPPORT_CHANNEL]
# Match "tech-support" as a word (treating hyphens as word characters), except if
# it's preceded by a slash to avoid matching it in URLs
Expand Down Expand Up @@ -214,6 +221,15 @@ def matcher(event):

return matcher

@app.event(
{"type": "message"},
matchers=[build_matcher(bennett_admins_regex, bennett_admins_channel_id)],
)
def repost_to_bennett_admins(event, say, ack):
repost_support_request_to_channel(
event, say, ack, "bennett-admins", bennett_admins_channel_id
)

@app.event(
{"type": "message"},
matchers=[build_matcher(tech_support_regex, tech_support_channel_id)],
Expand Down
1 change: 1 addition & 0 deletions ebmbot/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
# in slack, where the log dir is a mounted volume
HOST_LOGS_DIR = env.path("HOST_LOGS_DIR", LOGS_DIR)
SLACK_LOGS_CHANNEL = env.str("SLACK_LOGS_CHANNEL")
SLACK_BENNETT_ADMINS_CHANNEL = env.str("SLACK_BENNETT_ADMINS_CHANNEL")
SLACK_TECH_SUPPORT_CHANNEL = env.str("SLACK_TECH_SUPPORT_CHANNEL")
SLACK_BOT_TOKEN = env.str("SLACK_BOT_TOKEN")
SLACK_APP_TOKEN = env.str("SLACK_APP_TOKEN")
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ env = [
"LOGS_DIR=tests/logs",
"HOST_LOGS_DIR=tests/logs",
"SLACK_LOGS_CHANNEL=logs",
"SLACK_BENNETT_ADMINS_CHANNEL=bennettadmins",
"SLACK_TECH_SUPPORT_CHANNEL=techsupport",
"SLACK_SIGNING_SECRET=secret",
"SLACK_BOT_TOKEN=xoxb-token",
Expand Down
1 change: 1 addition & 0 deletions tests/mock_web_api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ def set_common_headers(self):
{
"ok": True,
"channels": [
{"name": "bennettadmins", "id": "C0000", "is_archived": False},
{"name": "techsupport", "id": "C0001", "is_archived": False},
{"name": "channel", "id": "C0002", "is_archived": False},
{"name": "channel1", "id": "C0003", "is_archived": False},
Expand Down
9 changes: 5 additions & 4 deletions tests/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ def register_handler(mock_app):

def test_joined_channels(mock_app):
recorder = mock_app.recorder
# conversations.members called for each channel (3) (to check if bot is already a
# member) and conversations.join 2 times to join the channels it's not already in
assert recorder.mock_received_requests["/conversations.members"] == 3
assert recorder.mock_received_requests["/conversations.join"] == 2
# conversations.members called for each channel (4) (to check if bot is already a
# member) and conversations.join 3 times to join the channels it's not already in
assert recorder.mock_received_requests["/conversations.members"] == 4
assert recorder.mock_received_requests["/conversations.join"] == 3


def test_schedule_job(mock_app):
Expand Down Expand Up @@ -317,6 +317,7 @@ def _tech_support_test_params():
("This message should match the `tech-support` listener", "C0002", {}, "C0001"),
("tech-support - this message should match", "C0002", {}, "C0001"),
("This message should match - tech-support", "C0002", {}, "C0001"),
("This message should match the bennett-admins listener", "C0002", {}, "C0000"),
]


Expand Down

0 comments on commit 92ff4d9

Please sign in to comment.