add fail check and edit message

This commit is contained in:
sergey 2025-04-13 15:30:37 +03:00
parent 3e34f4567c
commit 12c124863e
7 changed files with 46 additions and 2 deletions

View File

@ -5,6 +5,7 @@ CFG__ZABBIX__URL=https://example.com
CFG__ZABBIX__TOKEN=string
CFG__ZABBIX__MIN_SEVERITY=0
CFG__ZABBIX__UPD_INTERVAL=60
CFG__ZABBIX__ALERT_FAIL_COUNT=3
CFG__TGBOT__TOKEN=string
CFG__TGBOT__CHAT_ID=00000000

View File

@ -49,6 +49,8 @@ class ZabbixConfig(BaseModel):
token: str
min_severity: int
upd_interval: int
alert_fail_count: int
current_fail: int = 0
class Settings(BaseSettings):

View File

@ -10,7 +10,7 @@ services:
command: [redis-server, --protected-mode yes, --port 6379, --requirepass, P@ssw0rd!]
tg-bot:
image: git.sm8255082.ru/osnova/zbx-tg-bot:2.0.0
image: git.sm8255082.ru/osnova/zbx-tg-bot:2.0.1
restart: always
depends_on:
- redis

View File

@ -1,6 +1,6 @@
[project]
name = "zbx-tg-bot"
version = "2.0.0"
version = "2.0.1"
description = "telegram bot for telegram-zabbix dashboard"
requires-python = ">=3.13"
dependencies = [

View File

@ -1,6 +1,7 @@
from .message import (
send_message,
del_message,
edit_message,
)
from .bot import start_bot
@ -8,4 +9,5 @@ __all__ = [
"send_message",
"del_message",
"start_bot",
"edit_message",
]

View File

@ -45,6 +45,38 @@ async def send_message(message: str, event_id: int) -> dict:
return {"status": e}
async def edit_message(
message_id: int,
new_message: str,
) -> dict:
url = f"https://api.telegram.org/bot{conf.tgbot.token}/editMessageText"
async with aiohttp.ClientSession() as session:
try:
async with session.post(
url,
json={
"chat_id": conf.tgbot.chat_id,
"message_id": message_id,
"text": new_message,
},
) as response:
if response.status == 200:
log.info(f"Message ID {message_id} edited")
return {
"status": response.status,
}
else:
log.warning(
f"Message ID {message_id} NOT edited. Response status: {response.status}"
)
return {
"status": response.status,
}
except Exception as e:
log.warning(f"Exception: {e}")
return {"status": e}
async def del_message(
message_id: int,
) -> dict:

View File

@ -40,9 +40,16 @@ def get_active_problems() -> dict:
event["host"] = event.pop("hosts", None)[0]["host"]
events_dict[event["eventid"]] = event
events_dict["event_ids"].append(event["eventid"])
if conf.zabbix.current_fail:
conf.zabbix.current_fail = 0
return events_dict
except:
log.warning("Get event from zabbix error")
if conf.zabbix.current_fail == conf.zabbix.alert_fail_count:
log.warning(f"Zabbix connection error. Count {conf.zabbix.current_fail}")
else:
conf.zabbix.current_fail += 1
log.warning(f"Zabbix connection error. Count {conf.zabbix.current_fail}")
def event_acknowledge(event_id: int, mute_time: int, muted_by: str):