Skip to content

Commit

Permalink
Handle edited tech support messages
Browse files Browse the repository at this point in the history
Check edited messages for tech-support keyword, in case an initial
request for tech-support was mistyped.
  • Loading branch information
rebkwok committed Jan 12, 2024
1 parent 05c5e58 commit a054c43
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 6 deletions.
27 changes: 22 additions & 5 deletions ebmbot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ def register_listeners(app, config, channels, bot_user_id):
tech_support_regex = re.compile(
r".*(^|[^\w\-/])tech-support($|[^\w\-]).*", flags=re.I
)
# Only match messages posted outside of the tech support channel itself
# and messages that are not posted by a bot (to avoid reposting reminders etc)
tech_support_matchers = [
lambda message: message["channel"] != tech_support_channel_id,
lambda message: "bot_id" not in message,
]

@app.event(
"app_mention",
Expand Down Expand Up @@ -172,18 +178,29 @@ def _listener(event, say):
include_apology = text != "help"
handle_help(event, say, config["help"], config["description"], include_apology)

@app.event(
{"type": "message", "subtype": "message_changed", "text": tech_support_regex},
matchers=tech_support_matchers,
)
def repost_edited_message_to_tech_support(event, say, ack):
message_to_handle = {
**event["previous_message"],
"channel": event["channel"],
"channel_type": event["channel_type"],
}
return _repost_to_tech_support(message_to_handle, say, ack)

@app.message(
tech_support_regex,
# Only match messages posted outside of the tech support channel itself
# and messages that are not posted by a bot (to avoid reposting reminders etc)
matchers=[
lambda message: message["channel"] != tech_support_channel_id,
lambda message: "bot_id" not in message,
],
matchers=tech_support_matchers,
)
def repost_to_tech_support(message, say, ack):
ack()
return _repost_to_tech_support(message, say, ack)

def _repost_to_tech_support(message, say, ack):
ack()
# Don't repost messages in DMs with the bot
if message["channel_type"] in ["channel", "group"]:
# Respond with SOS reaction
Expand Down
48 changes: 47 additions & 1 deletion tests/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,40 @@ def test_tech_support_listener(mock_app, text, channel, event_kwargs, repost_exp
assert ("channel", "C0001") in post_message.items()


def test_tech_support_edited_message(mock_app):
# the triggered tech support handler will first fetch the url for the message
# and then post it to the techsupport channel
# Before the dispatched message, neither of these paths have been called
recorder = mock_app.recorder
tech_support_call_paths = ["/chat.getPermalink", "/chat.postMessage"]
for path in tech_support_call_paths:
assert path not in recorder.mock_received_requests

handle_message(
mock_app,
"get tec-support",
channel="C0002",
reaction_count=0,
event_type="message",
)

# tech-support keyword typo, no tech support calls
for path in tech_support_call_paths:
assert path not in recorder.mock_received_requests

# Editing the same message to include tech-support does repost
handle_message(
mock_app,
"get tech-support",
channel="C0002",
reaction_count=1,
event_type="message_changed",
)

for path in tech_support_call_paths:
assert recorder.mock_received_requests[path] == 1


@patch("ebmbot.bot.get_tech_support_dates")
def test_tech_support_out_of_office_listener(tech_support_dates, mock_app):
start = (datetime.today() - timedelta(1)).date()
Expand Down Expand Up @@ -454,7 +488,7 @@ def test_no_listener_found(mock_app):
# A message must either start with "<@U1234>" (i.e. a user @'d the bot) OR must contain
# the tech-support pattern
text = "This message should not match any listener"
# We use an error handler to deal with unhandled messages, so the resonse status
# We use an error handler to deal with unhandled messages, so the response status
# is 200
resp = handle_message(
mock_app,
Expand Down Expand Up @@ -566,6 +600,18 @@ def get_mock_request(event_type, event_kwargs):
event_kwargs = event_kwargs or {}
event_kwargs.update({"message": {"text": event_kwargs.get("text", "")}})

if event_type == "message_changed":
event_kwargs.update(
{
"type": "message",
"subtype": "message_changed",
"previous_message": {
"ts": "1596183880.004200",
"text": event_kwargs["text"],
},
}
)

body = {
"token": "verification_token",
"team_id": "T111",
Expand Down

0 comments on commit a054c43

Please sign in to comment.