home-tg-bot/tg-bot-app/main.py

69 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from config import settings
import re
import asyncio
import logging
from aiogram import Bot, Dispatcher, types
from aiogram import F
import zabbix_api
bot = Bot(token=settings.tg.token)
dp = Dispatcher()
@dp.callback_query(F.data.startswith("ack_"))
async def event_acknowledge(callback: types.CallbackQuery):
alert_id = int(callback.data.split("_")[1])
await zabbix_api.event_acknowledge(alert_id)
await callback.message.answer(f"Алерт с id {alert_id} подтверждён и взят в работу")
await callback.answer()
@dp.callback_query(F.data.startswith("m1h_"))
async def maintenance_1h(callback: types.CallbackQuery):
host_id = int(callback.data.split("_")[1])
host_name = await zabbix_api.get_host_by_id(host_id=host_id)
await zabbix_api.maintenance_by_host_id(
host_id=host_id, period=60 * 60, host_name=host_name
)
await callback.message.answer(f"Закинул в обслуживание {host_name} на час")
await callback.answer()
@dp.callback_query(F.data.startswith("m1d_"))
async def maintenance_1d(callback: types.CallbackQuery):
host_id = int(callback.data.split("_")[1])
host_name = await zabbix_api.get_host_by_id(host_id=host_id)
await zabbix_api.maintenance_by_host_id(
host_id=host_id, period=60 * 60 * 24, host_name=host_name
)
await callback.message.answer(f"Закинул в обслуживание {host_name} на сутки")
await callback.answer()
@dp.message()
async def add_event_comment(message: types.Message):
await message.answer(f"Получен текст {message.text}")
@dp.message()
async def add_event_comment(message: types.Message):
if message.reply_to_message:
original_message = message.reply_to_message.text
match = re.search(
r"^Алерт с id (\d+) подтверждён и взят в работу$",
original_message,
)
if match:
comment_text = f"@{message.from_user.username} -> {message.text}"
await zabbix_api.add_event_comment(
event_id=int(match.group(1)), msg=comment_text
)
await message.answer(f"Комментарий добавлен")
async def main():
await dp.start_polling(bot)
if __name__ == "__main__":
asyncio.run(main())