diff --git a/app/bot/utils/tg_formatter.py b/app/bot/utils/tg_formatter.py index 69d7a6e..4361cf0 100644 --- a/app/bot/utils/tg_formatter.py +++ b/app/bot/utils/tg_formatter.py @@ -1,35 +1,44 @@ from datetime import datetime + from pytz import timezone + from app.bot.utils.tg_escape_chars import escape_telegram_chars +from config import ZABBIX_URL, ZABBIX_TZ -def format_trigger_message(trigger, zabbix_url: str) -> str: - tz = timezone('Europe/Moscow') + +def format_trigger_for_tg(trigger): + """ + Формирует текст сообщения для одного триггера + и возвращает (text, inline_buttons_data) + """ priority_map = {'4': 'HIGH', '5': 'DISASTER'} - - event_time_epoch = int(trigger.get('lastEvent', {}).get('clock', trigger.get('lastchange', 0))) - event_time = datetime.fromtimestamp(event_time_epoch, tz=tz) + priority_map_emoji = {'4': '⚠️','5': '⛔️'} + event_time_epoch = int(trigger['lastEvent']['clock']) + event_time = datetime.fromtimestamp(event_time_epoch, tz=timezone(ZABBIX_TZ)) event_time_formatted = event_time.strftime('%Y-%m-%d %H:%M:%S Мск') - host = trigger.get('hosts', [{}])[0].get('name', 'Неизвестно') - priority = priority_map.get(str(trigger.get('priority')), 'Неизвестно') - description = escape_telegram_chars(trigger.get('description', '')).replace("{HOST.NAME}", host) - - items = trigger.get('items', []) - item_ids = [item['itemid'] for item in items] - - for i, item in enumerate(items): + description = escape_telegram_chars(trigger['description']) + host = trigger['hosts'][0]['name'] + priority = priority_map.get(trigger['priority'], 'Неизвестно') + icon = priority_map_emoji.get(trigger['priority'], 'Неизвестно') + description = description.replace("{HOST.NAME}", host) + for i, item in enumerate(trigger['items']): placeholder = f"{{ITEM.LASTVALUE{i + 1}}}" if placeholder in description: - description = description.replace(placeholder, item.get('lastvalue', '?')) + description = description.replace(placeholder, item['lastvalue']) - batchgraph_link = f"{zabbix_url}/history.php?action=batchgraph&" + item_ids = [item['itemid'] for item in trigger['items']] + batchgraph_link = f"{ZABBIX_URL}/history.php?action=batchgraph&" batchgraph_link += "&".join([f"itemids[{item_id}]={item_id}" for item_id in item_ids]) batchgraph_link += "&graphtype=0" - return ( + text = ( f"Host: {host}\n" f"Описание: {description}\n" - f"Критичность: {priority}\n" + f"Критичность: {icon} {priority}\n" f"Время создания: {event_time_formatted}\n" - f'URL: Ссылка на график' + # f'URL: Ссылка на график' ) + + # Возвращаем текст и ссылку, чтобы потом сделать inline кнопку + return text, batchgraph_link