From 878373b19b691e7c054d36037a002b771dc9b3ab Mon Sep 17 00:00:00 2001 From: sergey Date: Wed, 9 Oct 2024 19:28:02 +0300 Subject: [PATCH] add net service alert --- config/.env-template | 2 ++ config/config.py | 2 ++ osnova-api-alert.service | 12 ++++++++++++ routers/tg_send.py | 17 ++++++++++++++++- schemas/__init__.py | 6 +++++- schemas/tg_send.py | 4 ++++ telegram/__init__.py | 2 +- telegram/{msg.py => zbx_msg.py} | 22 ++++++++++++++++++++++ 8 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 osnova-api-alert.service rename telegram/{msg.py => zbx_msg.py} (65%) diff --git a/config/.env-template b/config/.env-template index 18baf6b..971ca39 100644 --- a/config/.env-template +++ b/config/.env-template @@ -18,3 +18,5 @@ OAA_CFG__TG__TOKEN=string OAA_CFG__TG__DASHBOARD_CHAT_ID=0 OAA_CFG__TG__DASHBOARD_TRED_ID=0 OAA_CFG__TG__CLOSE_ALERT_PATTERN=^Problem has been resolved +OAA_CFG__TG__NET_CHAT_ID=0 +OAA_CFG__TG__NET_TRED_ID=0 \ No newline at end of file diff --git a/config/config.py b/config/config.py index 4498f60..9b27842 100644 --- a/config/config.py +++ b/config/config.py @@ -51,6 +51,8 @@ class TelegramConfig(BaseModel): dashboard_chat_id: int dashboard_tred_id: int | None = None close_alert_pattern: str + net_chat_id: int + net_tred_id: int | None = None class SwaggerConfig(BaseModel): diff --git a/osnova-api-alert.service b/osnova-api-alert.service new file mode 100644 index 0000000..b078ed2 --- /dev/null +++ b/osnova-api-alert.service @@ -0,0 +1,12 @@ + [Unit] + Description=Send alert from Osnova to telegram + After=multi-user.target + + [Service] + User=linuxadm + Type=simple + Restart=always + ExecStart=/home/linuxadm/python-scripts/osnova-api-alert/.venv/bin/python3 /home/linuxadm/python-scripts/osnova-api-alert/main.py + + [Install] + WantedBy=multi-user.target \ No newline at end of file diff --git a/routers/tg_send.py b/routers/tg_send.py index a65d78a..836033d 100644 --- a/routers/tg_send.py +++ b/routers/tg_send.py @@ -1,7 +1,7 @@ from fastapi import APIRouter, Depends import logging as log -from schemas import TelegramMessageToDashboard +from schemas import TelegramMessageToDashboard, TelegramMessageToNetwork from auth import verify_token_admin from telegram import send_message_to_dashboard, del_message_from_dashboard from redis_db import set_value, pop_value @@ -31,4 +31,19 @@ async def send_message( if result["status"] == 200: log.info(f"Message sent to dashboard: {message.text}") await set_value(message.problem_id, result["msg_id"]) + else: + log.warning(f"Failed to send message code: {result['status']}") + return + + +@router.post("/send-net-service") +async def send_message_net_service( + message: TelegramMessageToNetwork, + token: str = Depends(verify_token_admin), +): + result = await send_message_to_dashboard(text=message.text) + if result["status"] == 200: + log.info(f"Message sent to network chat: {message.text}") + else: + log.warning(f"Failed to send message code: {result['status']}") return diff --git a/schemas/__init__.py b/schemas/__init__.py index b7838bb..c255d86 100644 --- a/schemas/__init__.py +++ b/schemas/__init__.py @@ -1,5 +1,9 @@ -from .tg_send import TelegramMessageToDashboard +from .tg_send import ( + TelegramMessageToDashboard, + TelegramMessageToNetwork, +) __all__ = [ "TelegramMessageToDashboard", + "TelegramMessageToNetwork", ] diff --git a/schemas/tg_send.py b/schemas/tg_send.py index fe3604d..c33bdbd 100644 --- a/schemas/tg_send.py +++ b/schemas/tg_send.py @@ -4,3 +4,7 @@ from pydantic import BaseModel class TelegramMessageToDashboard(BaseModel): text: str problem_id: int + + +class TelegramMessageToNetwork(BaseModel): + text: str diff --git a/telegram/__init__.py b/telegram/__init__.py index b14207c..28d119d 100644 --- a/telegram/__init__.py +++ b/telegram/__init__.py @@ -1,4 +1,4 @@ -from .msg import send_message_to_dashboard, del_message_from_dashboard +from .zbx_msg import send_message_to_dashboard, del_message_from_dashboard __all__ = [ "send_message_to_dashboard", diff --git a/telegram/msg.py b/telegram/zbx_msg.py similarity index 65% rename from telegram/msg.py rename to telegram/zbx_msg.py index a0c194f..6572f5f 100644 --- a/telegram/msg.py +++ b/telegram/zbx_msg.py @@ -40,3 +40,25 @@ async def del_message_from_dashboard(message_id): resp = await response.json() log.info(f"Message ID {message_id} deleted") return resp + + +async def send_message_to_net_chat(text): + url = f"https://api.telegram.org/bot{conf.tg.token}/sendMessage" + params = { + "chat_id": conf.tg.net_chat_id, + "text": text, + } + if conf.tg.net_tred_id: + params["message_thread_id"] = conf.tg.net_tred_id + async with aiohttp.ClientSession() as session: + async with session.post( + url, + json=params, + ) as response: + log.info(f"Response status: {response.status}") + resp = await response.json() + log.info(f"Message ID: {resp['result']['message_id']}") + return { + "status": response.status, + "msg_id": resp["result"]["message_id"], + }