mirror of
https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git
synced 2025-08-14 00:25:46 +02:00
отправка уведомлений в телеграм
This commit is contained in:
@@ -3,11 +3,12 @@ import os
|
||||
from fastapi import APIRouter, Request
|
||||
from telebot import TeleBot
|
||||
from telebot.types import Update
|
||||
from ..models.request import RequestStatus
|
||||
from ..models.request import RequestStatus, RequestPriority
|
||||
from ..database import SessionLocal
|
||||
from ..models.request import Request as DBRequest
|
||||
|
||||
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
|
||||
TELEGRAM_CHAT_ID = os.getenv("TELEGRAM_CHAT_ID")
|
||||
WEBHOOK_URL = "https://itformhelp.ru/telegram/webhook/"
|
||||
WEBHOOK_PATH = "/telegram/webhook/"
|
||||
|
||||
@@ -37,6 +38,34 @@ def handle_message(message):
|
||||
"""Handle all messages"""
|
||||
bot.reply_to(message, "Я получил ваше сообщение и обязательно обработаю его!")
|
||||
|
||||
def format_priority(priority: str) -> str:
|
||||
"""Format priority with emoji"""
|
||||
priority_emoji = {
|
||||
RequestPriority.LOW: "🟢",
|
||||
RequestPriority.MEDIUM: "🟡",
|
||||
RequestPriority.HIGH: "🔴"
|
||||
}
|
||||
return f"{priority_emoji.get(priority, '⚪')} {priority.capitalize()}"
|
||||
|
||||
def notify_new_request(request_id: int):
|
||||
"""Send notification about new request"""
|
||||
try:
|
||||
db = SessionLocal()
|
||||
request = db.query(DBRequest).filter(DBRequest.id == request_id).first()
|
||||
if request:
|
||||
message = (
|
||||
f"📋 <b>Новая заявка #{request.id}</b>\n\n"
|
||||
f"📝 <b>Заголовок:</b> {request.title}\n"
|
||||
f"👤 <b>Сотрудник:</b> {request.employee.last_name} {request.employee.first_name}\n"
|
||||
f"❗ <b>Приоритет:</b> {format_priority(request.priority)}\n\n"
|
||||
f"📄 <b>Описание:</b>\n{request.description}"
|
||||
)
|
||||
bot.send_message(TELEGRAM_CHAT_ID, message, parse_mode="HTML")
|
||||
except Exception as e:
|
||||
print(f"Error sending telegram notification: {e}")
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
def notify_status_change(request_id: int, new_status: RequestStatus):
|
||||
"""Notify user about request status change"""
|
||||
try:
|
||||
|
Reference in New Issue
Block a user