-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(organizations): add optional Telegram notifications for detectio…
…ns (#381) * feat(services): add Telegram management * feat(organizations): add telegram_id field in organizations * feat(detections): add telegram notifications * test(services): add test cases for TelegramClient * docs(contributing): update env arg * build(deps): add httpx as deps * fix(models); remove faulty pattern * test(organizations): fix test cases * style(lint): fix formatting * docs(uml): update UML * test(organizations): update test case * refactor(telegram): clarified behaviour
- Loading branch information
Showing
15 changed files
with
156 additions
and
28 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,58 @@ | ||
# Copyright (C) 2024, Pyronear. | ||
|
||
# This program is licensed under the Apache License 2.0. | ||
# See LICENSE or go to <https://opensource.org/licenses/Apache-2.0> for full license details. | ||
|
||
import logging | ||
from typing import Union | ||
|
||
import requests | ||
|
||
from app.core.config import settings | ||
|
||
logger = logging.getLogger("uvicorn.error") | ||
|
||
__all__ = ["telegram_client"] | ||
|
||
|
||
class TelegramClient: | ||
BASE_URL = "https://api.telegram.org/bot{token}" | ||
|
||
def __init__(self, token: Union[str, None] = None) -> None: | ||
self.is_enabled = isinstance(token, str) | ||
if isinstance(token, str): | ||
self.token = token | ||
# Validate token | ||
response = requests.get( | ||
f"{self.BASE_URL.format(token=self.token)}/getMe", | ||
timeout=1, | ||
) | ||
if response.status_code != 200: | ||
raise ValueError(f"Invalid Telegram Bot token: {response.text}") | ||
logger.info("Telegram notifications enabled") | ||
|
||
def has_channel_access(self, channel_id: str) -> bool: | ||
if not self.is_enabled: | ||
raise AssertionError("Telegram notifications are not enabled") | ||
response = requests.get( | ||
f"{self.BASE_URL.format(token=self.token)}/getChat", | ||
json={"chat_id": channel_id}, | ||
timeout=1, | ||
) | ||
return response.status_code == 200 | ||
|
||
def notify(self, channel_id: str, message: str) -> requests.Response: | ||
if not self.is_enabled: | ||
raise AssertionError("Telegram notifications are not enabled") | ||
response = requests.post( | ||
f"{self.BASE_URL.format(token=self.token)}/sendMessage", | ||
json={"chat_id": channel_id, "text": message}, | ||
timeout=2, | ||
) | ||
if response.status_code != 200: | ||
logger.error(f"Failed to send message to Telegram: {response.text}") | ||
|
||
return response | ||
|
||
|
||
telegram_client = TelegramClient(token=settings.TELEGRAM_TOKEN) |
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,24 @@ | ||
import pytest | ||
|
||
from app.core.config import settings | ||
from app.services.telegram import TelegramClient | ||
|
||
|
||
def test_telegram_client(): | ||
with pytest.raises(ValueError, match="Invalid Telegram Bot token"): | ||
TelegramClient("invalid-token") | ||
|
||
client = TelegramClient(None) | ||
assert not client.is_enabled | ||
|
||
client = TelegramClient(settings.TELEGRAM_TOKEN) | ||
assert client.is_enabled == isinstance(settings.TELEGRAM_TOKEN, str) | ||
|
||
if isinstance(settings.TELEGRAM_TOKEN, str): | ||
assert not client.has_channel_access("invalid-channel-id") | ||
assert client.notify("invalid-channel-id", "test").status_code == 404 | ||
else: | ||
with pytest.raises(AssertionError, match="Telegram notifications are not enabled"): | ||
client.has_channel_access("invalid-channel-id") | ||
with pytest.raises(AssertionError, match="Telegram notifications are not enabled"): | ||
client.notify("invalid-channel-id", "test") |