diff --git a/bot.py b/bot.py index a9dde3a..417e244 100644 --- a/bot.py +++ b/bot.py @@ -188,6 +188,9 @@ def __init__(self): "You might need to specify one or more environment variables." ) + # Probability to avoid overcrowding Telegram chats with dog pictures + self.sad_message_response_probability: float = 0.45 + # Fetches list of dog breeds from the Dogs API self.fetch_breeds() @@ -307,8 +310,14 @@ async def handle_text_messages(self, update, context): elif has_wolf_reference: await self.send_wolf_picture(update, context) elif is_sad_message: - sad_caption = "Don't be sad, have a cute dog!" - await self.send_dog_picture(update, context, mentioned_breed, sad_caption) + # Easter Egg: if the message is sad, send a dog picture + # with a comforting message + + # To avoid overloading the chat with dog pictures, only + # send a picture with a certain probability + if random.random() < self.sad_message_response_probability: + sad_caption = "Don't be sad, have a cute dog!" + await self.send_dog_picture(update, context, mentioned_breed, sad_caption) elif any([should_trigger_picture, is_personal_chat, mentions_a_breed]): await self.send_dog_picture(update, context, mentioned_breed) diff --git a/tests.py b/tests.py index 4fce52d..9adcc93 100644 --- a/tests.py +++ b/tests.py @@ -423,25 +423,34 @@ async def test_handle_text_messages_for_sad_message( message, the bot replies with a dog picture and a particular caption. """ - # instantiating mock bot - bot = get_mock_bot(monkeypatch) - update = get_mock_update(message=sad_message) - context = get_mock_context() - - # context is empty of sent photos - assert len(context.bot.photos) == 0 - - await bot.handle_text_messages(update, context) - - # one picture sent through context - assert len(context.bot.photos) == 1 - - # contains the chat_id, original message id, photo url and caption - chat_id, reply_to_message_id, photo_url, caption = context.bot.photos[0] - assert chat_id == update.message.chat_id - assert reply_to_message_id == update.message.message_id - assert photo_url == "https://dog.pics/dog.png" - assert caption == "Don't be sad, have a cute dog!" + for probability in [0.0, 1.0]: + # we're either testing with a 100% probability or a 0% probability + # so we can assert the outcome of the test + should_send: bool = probability == 1.0 + + # instantiating mock bot + bot = get_mock_bot(monkeypatch) + bot.sad_message_response_probability = probability + update = get_mock_update(message=sad_message) + context = get_mock_context() + + # context is empty of sent photos + assert len(context.bot.photos) == 0 + + await bot.handle_text_messages(update, context) + + if should_send: + # one picture sent through context + assert len(context.bot.photos) == 1 + + # contains the chat_id, original message id, photo url and caption + chat_id, reply_to_message_id, photo_url, caption = context.bot.photos[0] + assert chat_id == update.message.chat_id + assert reply_to_message_id == update.message.message_id + assert photo_url == "https://dog.pics/dog.png" + assert caption == "Don't be sad, have a cute dog!" + else: + assert len(context.bot.photos) == 0 @pytest.mark.parametrize(