Skip to content

Commit

Permalink
Utilise asynchronous functions
Browse files Browse the repository at this point in the history
  • Loading branch information
yihong1120 committed Jul 17, 2024
1 parent 10d90fd commit 61eaa12
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
13 changes: 10 additions & 3 deletions src/telegram_notifier.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import os
import asyncio
import os
from io import BytesIO

import numpy as np
Expand All @@ -26,9 +26,16 @@ async def send_notification(
buffer = BytesIO()
image_pil.save(buffer, format='PNG')
buffer.seek(0)
await self.bot.send_photo(chat_id=chat_id, photo=buffer, caption=message)
await self.bot.send_photo(
chat_id=chat_id,
photo=buffer,
caption=message,
)
else:
await self.bot.send_message(chat_id=chat_id, text=message)
await self.bot.send_message(
chat_id=chat_id,
text=message,
)
return 200


Expand Down
8 changes: 6 additions & 2 deletions tests/telegram_notifier_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import unittest
from io import BytesIO
from unittest.mock import patch, AsyncMock
from unittest.mock import AsyncMock
from unittest.mock import patch

import numpy as np

Expand All @@ -25,7 +26,10 @@ async def test_send_notification_no_image(self, mock_send_message):
mock_send_message.return_value = 'Message sent'
chat_id = 'test_chat_id'
message = 'Hello, Telegram!'
response = await self.telegram_notifier.send_notification(chat_id, message)
response = await self.telegram_notifier.send_notification(
chat_id,
message,
)
self.assertEqual(response, 'Message sent')
mock_send_message.assert_called_once_with(
chat_id=chat_id, text=message,
Expand Down

0 comments on commit 61eaa12

Please sign in to comment.