diff --git a/src/remember_remember_bot/commands.py b/src/remember_remember_bot/commands.py index 6f8b256..c80b098 100644 --- a/src/remember_remember_bot/commands.py +++ b/src/remember_remember_bot/commands.py @@ -3,6 +3,7 @@ from remember_remember_bot.util import ( chat_is_active, get_file_path, get_lines_from_file, + today_lines, ) @@ -17,7 +18,9 @@ def remind_chat(chat: deltachat.Chat): if chat_is_active(messages): file_path = get_file_path(messages) lines = get_lines_from_file(file_path) - print(lines) + message_text = "\n".join(today_lines(lines)) + print(message_text) + chat.send_text(message_text) def activate_chat(msg: deltachat.Message): diff --git a/src/remember_remember_bot/util.py b/src/remember_remember_bot/util.py index 12dc949..6e4bdda 100644 --- a/src/remember_remember_bot/util.py +++ b/src/remember_remember_bot/util.py @@ -59,3 +59,25 @@ def get_lines_from_file(file_path: str) -> [str]: except (FileNotFoundError, TypeError): return [f"File not found: {file_path}"] return [line.strip("\n") for line in lines] + + +def today_lines(lines: [str]): + """Out of several lines from the file, only return those with the same date as today. + + :param lines: a list of lines with a date in the beginning. + :return: a list of lines, all from today. + """ + result = [] + for line in lines: + try: + day = int(line.split(".")[0]) + month = int(line.split(".")[1]) + except ValueError: + print(f"wrong format: {line}") + continue + if ( + day == datetime.datetime.now().day + and month == datetime.datetime.now().month + ): + result.append(line) + return result diff --git a/tests/test_util.py b/tests/test_util.py index 649e400..50cfe5b 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -3,8 +3,10 @@ from remember_remember_bot.util import ( update_day, chat_is_active, get_file_path, + today_lines, ) import pytest +import datetime def test_update_day(): @@ -53,3 +55,19 @@ def test_get_file_path(messages: [], tmp_file_path: str): "%file_path%", tmp_file_path ) assert get_file_path(messages) == tmp_file_path + + +def test_today_lines(): + day = datetime.datetime.now().day + month = datetime.datetime.now().month + year = datetime.datetime.now().year + lines = [ + f"{day}.{month}.{year}\tToday I had a great day!", + f"{day}.{month}.2020\tI didn't go outside all day :) no appointments", + "03.13.2024\tNice how they invented a 13th month extra for the Satanists!", + ] + result = today_lines(lines) + print(result) + assert len(result) == 2 + assert result[0] == lines[0] + assert result[1] == lines[1]