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 .config import BOT_TOKEN
|
||||
|
||||
bot = Bot(token="7677506032:AAHduD5EePz3bE23DKlo35KoOp2_9lZuS34")
|
||||
# Initialize bot and dispatcher
|
||||
bot = Bot(token=BOT_TOKEN)
|
||||
dp = Dispatcher()
|
||||
|
||||
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)
|
||||
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):
|
||||
TELEGRAM_BOT_TOKEN: str = "7677506032:AAHduD5EePz3bE23DKlo35KoOp2_9lZuS34"
|
||||
TELEGRAM_CHAT_ID: int = 5057752127
|
||||
# Chat ID for notifications
|
||||
NOTIFICATION_CHAT_ID = "-1002037023574"
|
||||
|
||||
class Config:
|
||||
env_file = ".env"
|
||||
|
||||
|
||||
settings = Settings()
|
||||
# Request status constants
|
||||
class RequestStatus:
|
||||
NEW = "new"
|
||||
IN_PROGRESS = "in_progress"
|
||||
COMPLETED = "completed"
|
||||
CANCELLED = "cancelled"
|
@@ -1,41 +1,32 @@
|
||||
import asyncio
|
||||
from logging import getLogger
|
||||
from aiogram.client.session.aiohttp import AiohttpSession
|
||||
from .bot import bot
|
||||
from .keyboards import create_status_keyboard
|
||||
from .messages import format_request_message
|
||||
from .config import settings
|
||||
"""
|
||||
Notifications module for the Telegram bot.
|
||||
Handles sending notifications about new requests and status updates.
|
||||
"""
|
||||
from aiogram import types
|
||||
from .config import NOTIFICATION_CHAT_ID
|
||||
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:
|
||||
request_data (dict): Request data including id, description, etc.
|
||||
"""
|
||||
message_text = (
|
||||
f"Новая заявка №{request_data['id']}\n"
|
||||
f"Отдел: {request_data['department']}\n"
|
||||
f"Тип: {request_data['request_type']}\n"
|
||||
f"Приоритет: {request_data['priority']}\n"
|
||||
f"Описание: {request_data['description']}"
|
||||
)
|
||||
|
||||
async def send_request_notification(request_data: dict):
|
||||
try:
|
||||
message = format_request_message(request_data)
|
||||
keyboard = create_status_keyboard(
|
||||
request_data["id"], request_data.get("status", "new")
|
||||
await bot.send_message(
|
||||
chat_id=NOTIFICATION_CHAT_ID,
|
||||
text=message_text,
|
||||
reply_markup=get_updated_keyboard(request_data['id'], "new")
|
||||
)
|
||||
|
||||
async with AiohttpSession() as session:
|
||||
bot.session = session
|
||||
await bot.send_message(
|
||||
chat_id=5057752127,
|
||||
text=message,
|
||||
parse_mode="HTML",
|
||||
reply_markup=keyboard,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"Error sending Telegram notification: {e}", exc_info=True)
|
||||
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
|
||||
print(f"Error sending notification: {e}")
|
@@ -1,3 +1,7 @@
|
||||
"""
|
||||
Main application entry point.
|
||||
Runs both the FastAPI application and Telegram bot.
|
||||
"""
|
||||
import asyncio
|
||||
import uvicorn
|
||||
from app.main import app
|
||||
@@ -6,30 +10,25 @@ from logging import getLogger
|
||||
|
||||
logger = getLogger(__name__)
|
||||
|
||||
|
||||
async def run_bot():
|
||||
"""Run Telegram bot"""
|
||||
print("Bot started")
|
||||
# try:
|
||||
# await start_bot()
|
||||
# except Exception as e:
|
||||
# logger.error(f"Bot crashed: {e}", exc_info=True)
|
||||
|
||||
try:
|
||||
await start_bot()
|
||||
except Exception as e:
|
||||
logger.error(f"Bot crashed: {e}", exc_info=True)
|
||||
|
||||
async def run_api():
|
||||
"""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)
|
||||
try:
|
||||
await server.serve()
|
||||
except Exception as e:
|
||||
logger.error(f"API crashed: {e}", exc_info=True)
|
||||
|
||||
|
||||
async def run_all():
|
||||
"""Run both bot and API"""
|
||||
await asyncio.gather(run_bot(), run_api())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(run_all())
|
Reference in New Issue
Block a user