Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More updates to the message checker #604

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions bennettbot/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,29 +236,33 @@ 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"]
messages = self.user_slack_client.search_messages(
query=(
# Search for messages with the keyword but without the expected reaction
f"{keyword} -has::{reaction}: "
# Wrap the keyword in double quotes so we don't return "tech support" as
# well as "tech-support"
f'"{keyword}" -has::{reaction}: '
# exclude messages in the channel itself
f"-in:#{channel} "
# exclude messages from the bot
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
12 changes: 8 additions & 4 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 @@ -614,9 +618,9 @@ def test_message_checker_matched_messages(keyword, support_channel, reaction):
assert requests_by_path["/api/search.messages"] == [
{
"query": [
f"{keyword} -has::{reaction}: -in:#{support_channel} "
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