Skip to content

Commit

Permalink
Merge pull request #117 from aviate-labs/115-bugfix/migrate-to-python…
Browse files Browse the repository at this point in the history
…-telegram-bot

115 bugfix/migrate to python telegram bot
  • Loading branch information
mourginakis authored Nov 16, 2023
2 parents f05fbe8 + 4cd2997 commit 88b0ff3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
19 changes: 13 additions & 6 deletions node_monitor/bot_telegram.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
import requests
import textwrap
from typing import List


class TelegramBot:
def __init__(self, telegram_token: str) -> None:
self.telegram_token = telegram_token


def send_message(
self, chat_id: str, message: str
) -> None | requests.exceptions.HTTPError:
"""Send a message to a single Telegram chat."""
max_message_length = 4096
message_parts = textwrap.wrap(message, width=max_message_length)

try:
request = requests.get(
f"https://api.telegram.org/bot{self.telegram_token}"
f"/sendMessage?chat_id={chat_id}&text={message}"
)
request.raise_for_status()
for part in message_parts:
payload = {"chat_id": chat_id, "text": part}
response = requests.post(
f"https://api.telegram.org/bot{self.telegram_token}/sendMessage",
data=payload
)
response.raise_for_status()
except requests.exceptions.HTTPError as e:
# print(f"Got an error: {e}")
return e
return None

Expand All @@ -33,3 +39,4 @@ def send_messages(
if this_err is not None:
err = this_err
return err

18 changes: 11 additions & 7 deletions tests/test_bot_telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@
import node_monitor.load_config as c
from node_monitor.bot_telegram import TelegramBot

@patch("requests.get")
def test_send_message(mock_get):
@patch("requests.post")
def test_send_message(mock_post):
telegram_bot = TelegramBot(c.TOKEN_TELEGRAM)
chat_id = "1234567890"
chat_id = "1234567890"
message = "Test message"
mock_response = mock_get.return_value
payload = {
"chat_id": chat_id,
"text": message
}
mock_response = mock_post.return_value
mock_response.raise_for_status.return_value = None

telegram_bot.send_message(chat_id, message)

mock_get.assert_called_once_with(
f"https://api.telegram.org/bot{telegram_bot.telegram_token}/sendMessage?chat_id={chat_id}&text={message}"
mock_post.assert_called_once_with(
f"https://api.telegram.org/bot{telegram_bot.telegram_token}/sendMessage",
data=payload
)
mock_response.raise_for_status.assert_called_once()

Expand All @@ -30,4 +35,3 @@ def test_send_live_message():
err = telegram_bot.send_message(chat_id, message)
if err is not None:
raise err

4 changes: 0 additions & 4 deletions tests/test_node_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ def _test_analyze(self):




def test_control():
"""Test the control case. No nodes down."""
# init
Expand Down Expand Up @@ -115,7 +114,6 @@ def test_control():




def test_one_node_bounce():
"""Test the case where one node bounces.
Should not result in a false positive.
Expand Down Expand Up @@ -147,7 +145,6 @@ def test_one_node_bounce():
mock_telegram_bot.reset_mock()



def test_two_nodes_down():
"""Test the case where two nodes truly go down."""
# init
Expand Down Expand Up @@ -177,7 +174,6 @@ def test_two_nodes_down():
mock_telegram_bot.reset_mock()



def test_one_new_node_online():
"""Test the case where one new node comes online."""
# init
Expand Down

0 comments on commit 88b0ff3

Please sign in to comment.