-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from aviate-labs/62-implement-telegrambot
62 implement telegrambot
- Loading branch information
Showing
12 changed files
with
177 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,20 @@ | ||
import requests | ||
|
||
|
||
class TelegramBot: | ||
def __init__(self, telegram_token: str) -> None: | ||
self.telegram_token = telegram_token | ||
|
||
def send_message_to_channel(self) -> None: | ||
pass | ||
|
||
def send_message_to_chat(self) -> None: | ||
pass | ||
def send_message( | ||
self, chat_id: str, message: str | ||
) -> None | requests.exceptions.HTTPError: | ||
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() | ||
except requests.exceptions.HTTPError as e: | ||
print(f"Got an error: {e}") | ||
return e | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import pytest | ||
from unittest.mock import patch | ||
|
||
import node_monitor.load_config as c | ||
from node_monitor.bot_telegram import TelegramBot | ||
|
||
@patch("requests.get") | ||
def test_send_message(mock_get): | ||
telegram_bot = TelegramBot(c.TOKEN_TELEGRAM) | ||
chat_id = "1234567890" | ||
message = "Test message" | ||
mock_response = mock_get.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_response.raise_for_status.assert_called_once() | ||
|
||
|
||
|
||
@pytest.mark.live_telegram | ||
def test_send_live_message(): | ||
telegram_bot = TelegramBot(c.TOKEN_TELEGRAM) | ||
chat_id = "-1001925583150" | ||
message = "Test message" | ||
|
||
err = telegram_bot.send_message(chat_id, message) | ||
if err is not None: | ||
raise err |
Oops, something went wrong.