Skip to content

Commit

Permalink
Search for missed support messages from today and yesterday
Browse files Browse the repository at this point in the history
Slack only allows us to search from a date, not a datetime (times
can be used too, but they apply to times on every day).

Sometimes, it seems slack's search just doesn't return messages
for a while. We now make it check for the whole of yesterday
AND today.

We also don't need to give it a "before" search term.
  • Loading branch information
rebkwok committed Oct 2, 2024
1 parent 0f669ce commit 99607a6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
14 changes: 8 additions & 6 deletions bennettbot/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,16 @@ def do_check(self, run_fn=lambda: True, delay=10): # pragma: no branch
# https://api.slack.com/apis/rate-limits#tier_t2
# A 10s delay should be safe for our 2 calls per loop
while run_fn():
# check for messages from today and yesterday; sometimes it seems to
# take a while for slack to return messages in search results, so
# make sure that messages sent late in the day still get picked up
today = datetime.today()
yesterday = (today - timedelta(days=1)).strftime("%Y-%m-%d")
tomorrow = (today + timedelta(days=1)).strftime("%Y-%m-%d")
check_from = (today - timedelta(days=2)).strftime("%Y-%m-%d")
for keyword in self.config:
self.check_messages(keyword, tomorrow, yesterday)
self.check_messages(keyword, check_from)
time.sleep(delay)

def check_messages(self, keyword, before, after):
def check_messages(self, keyword, after):
logger.debug("Checking %s messages", keyword)
reaction = self.config[keyword]["reaction"]
channel = self.config[keyword]["channel"]
Expand All @@ -259,8 +261,8 @@ def check_messages(self, keyword, before, after):
f"-from:@{settings.SLACK_APP_USERNAME} "
# exclude DMs as the auto-responders don't respond to these anyway
f"-is:dm "
# only include messages from today
f"before:{before} after:{after}"
# only include messages from today and yesterday
f"after:{after}"
)
)["messages"]["matches"]
for message in messages:
Expand Down
10 changes: 7 additions & 3 deletions tests/test_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,8 @@ def test_message_checker_config():
}


def test_message_checker_run():
def test_message_checker_run(freezer):
freezer.move_to("2024-10-08 23:30")
httpretty_register(
{
"search.messages": [
Expand All @@ -557,6 +558,9 @@ def test_message_checker_run():
# search.messages is called twice for each run of the checker
# no matches, so no reactions or messages reposted.
assert len(httpretty.latest_requests()) == 4
requests_by_path = get_mock_received_requests()
last_search_query = requests_by_path["/api/search.messages"][-1]["query"][0]
assert "after:2024-10-06" in last_search_query


@pytest.mark.parametrize(
Expand Down Expand Up @@ -604,7 +608,7 @@ def test_message_checker_matched_messages(keyword, support_channel, reaction):

checker = MessageChecker(slack_web_client("bot"), slack_web_client("user"))

checker.check_messages(keyword, "2024-03-04", "2024-03-02")
checker.check_messages(keyword, "2024-03-02")
# search.messages is called once
# other 3 endpoints called once each for 2 matched messages requiring
# reaction and reposting.
Expand All @@ -616,7 +620,7 @@ def test_message_checker_matched_messages(keyword, support_channel, reaction):
"query": [
f'"{keyword}" -has::{reaction}: -in:#{support_channel} '
f"-from:@{settings.SLACK_APP_USERNAME} -is:dm "
"before:2024-03-04 after:2024-03-02"
"after:2024-03-02"
]
}
]
Expand Down

0 comments on commit 99607a6

Please sign in to comment.