mirror of
https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git
synced 2025-08-14 00:25:46 +02:00
Починка статусов в ботов4
This commit is contained in:
@@ -1,11 +1,19 @@
|
|||||||
|
"""
|
||||||
|
Bot initialization module.
|
||||||
|
Creates bot and dispatcher instances.
|
||||||
|
"""
|
||||||
from aiogram import Bot, Dispatcher
|
from aiogram import Bot, Dispatcher
|
||||||
from .config import BOT_TOKEN
|
from .config import BOT_TOKEN
|
||||||
|
|
||||||
bot = Bot(token="7677506032:AAHduD5EePz3bE23DKlo35KoOp2_9lZuS34")
|
# Initialize bot and dispatcher
|
||||||
|
bot = Bot(token=BOT_TOKEN)
|
||||||
dp = Dispatcher()
|
dp = Dispatcher()
|
||||||
|
|
||||||
async def start_bot():
|
async def start_bot():
|
||||||
# Импортируем здесь, чтобы избежать циклических импортов
|
"""
|
||||||
from .bot import router
|
Start the bot and include all routers.
|
||||||
|
This function is called when the application starts.
|
||||||
|
"""
|
||||||
|
from .handlers import router # Import here to avoid circular imports
|
||||||
dp.include_router(router)
|
dp.include_router(router)
|
||||||
await dp.start_polling(bot, skip_updates=True)
|
await dp.start_polling(bot, skip_updates=True)
|
@@ -1,12 +1,17 @@
|
|||||||
from pydantic_settings import BaseSettings
|
"""
|
||||||
|
Configuration module for the Telegram bot.
|
||||||
|
Contains all necessary settings and constants.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Bot token from environment variables
|
||||||
|
BOT_TOKEN = "7677506032:AAHduD5EePz3bE23DKlo35KoOp2_9lZuS34"
|
||||||
|
|
||||||
class Settings(BaseSettings):
|
# Chat ID for notifications
|
||||||
TELEGRAM_BOT_TOKEN: str = "7677506032:AAHduD5EePz3bE23DKlo35KoOp2_9lZuS34"
|
NOTIFICATION_CHAT_ID = "-1002037023574"
|
||||||
TELEGRAM_CHAT_ID: int = 5057752127
|
|
||||||
|
|
||||||
class Config:
|
# Request status constants
|
||||||
env_file = ".env"
|
class RequestStatus:
|
||||||
|
NEW = "new"
|
||||||
|
IN_PROGRESS = "in_progress"
|
||||||
settings = Settings()
|
COMPLETED = "completed"
|
||||||
|
CANCELLED = "cancelled"
|
@@ -1,41 +1,32 @@
|
|||||||
import asyncio
|
"""
|
||||||
from logging import getLogger
|
Notifications module for the Telegram bot.
|
||||||
from aiogram.client.session.aiohttp import AiohttpSession
|
Handles sending notifications about new requests and status updates.
|
||||||
from .bot import bot
|
"""
|
||||||
from .keyboards import create_status_keyboard
|
from aiogram import types
|
||||||
from .messages import format_request_message
|
from .config import NOTIFICATION_CHAT_ID
|
||||||
from .config import settings
|
from . import bot
|
||||||
|
from .handlers import get_updated_keyboard
|
||||||
|
|
||||||
logger = getLogger(__name__)
|
async def send_notification(request_data: dict):
|
||||||
|
"""
|
||||||
|
Send notification about new request to Telegram chat.
|
||||||
|
|
||||||
|
Args:
|
||||||
async def send_request_notification(request_data: dict):
|
request_data (dict): Request data including id, description, etc.
|
||||||
try:
|
"""
|
||||||
message = format_request_message(request_data)
|
message_text = (
|
||||||
keyboard = create_status_keyboard(
|
f"Новая заявка №{request_data['id']}\n"
|
||||||
request_data["id"], request_data.get("status", "new")
|
f"Отдел: {request_data['department']}\n"
|
||||||
|
f"Тип: {request_data['request_type']}\n"
|
||||||
|
f"Приоритет: {request_data['priority']}\n"
|
||||||
|
f"Описание: {request_data['description']}"
|
||||||
)
|
)
|
||||||
|
|
||||||
async with AiohttpSession() as session:
|
try:
|
||||||
bot.session = session
|
|
||||||
await bot.send_message(
|
await bot.send_message(
|
||||||
chat_id=5057752127,
|
chat_id=NOTIFICATION_CHAT_ID,
|
||||||
text=message,
|
text=message_text,
|
||||||
parse_mode="HTML",
|
reply_markup=get_updated_keyboard(request_data['id'], "new")
|
||||||
reply_markup=keyboard,
|
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error sending Telegram notification: {e}", exc_info=True)
|
print(f"Error sending notification: {e}")
|
||||||
raise
|
|
||||||
|
|
||||||
|
|
||||||
def send_notification(request_data: dict):
|
|
||||||
try:
|
|
||||||
|
|
||||||
loop = asyncio.new_event_loop()
|
|
||||||
asyncio.set_event_loop(loop)
|
|
||||||
loop.run_until_complete(send_request_notification(request_data))
|
|
||||||
loop.close()
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"Failed to send notification: {e}", exc_info=True)
|
|
||||||
raise
|
|
@@ -1,3 +1,7 @@
|
|||||||
|
"""
|
||||||
|
Main application entry point.
|
||||||
|
Runs both the FastAPI application and Telegram bot.
|
||||||
|
"""
|
||||||
import asyncio
|
import asyncio
|
||||||
import uvicorn
|
import uvicorn
|
||||||
from app.main import app
|
from app.main import app
|
||||||
@@ -6,30 +10,25 @@ from logging import getLogger
|
|||||||
|
|
||||||
logger = getLogger(__name__)
|
logger = getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
async def run_bot():
|
async def run_bot():
|
||||||
"""Run Telegram bot"""
|
"""Run Telegram bot"""
|
||||||
print("Bot started")
|
try:
|
||||||
# try:
|
await start_bot()
|
||||||
# await start_bot()
|
except Exception as e:
|
||||||
# except Exception as e:
|
logger.error(f"Bot crashed: {e}", exc_info=True)
|
||||||
# logger.error(f"Bot crashed: {e}", exc_info=True)
|
|
||||||
|
|
||||||
|
|
||||||
async def run_api():
|
async def run_api():
|
||||||
"""Run FastAPI application"""
|
"""Run FastAPI application"""
|
||||||
config = uvicorn.Config(app, host=["0.0.0.0"], port=8000, reload=True)
|
config = uvicorn.Config(app, host="0.0.0.0", port=8000, reload=True)
|
||||||
server = uvicorn.Server(config)
|
server = uvicorn.Server(config)
|
||||||
try:
|
try:
|
||||||
await server.serve()
|
await server.serve()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"API crashed: {e}", exc_info=True)
|
logger.error(f"API crashed: {e}", exc_info=True)
|
||||||
|
|
||||||
|
|
||||||
async def run_all():
|
async def run_all():
|
||||||
"""Run both bot and API"""
|
"""Run both bot and API"""
|
||||||
await asyncio.gather(run_bot(), run_api())
|
await asyncio.gather(run_bot(), run_api())
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(run_all())
|
asyncio.run(run_all())
|
Reference in New Issue
Block a user