vk_chat_bot/tg/handlers/default.py
Kirill Kirilenko 4b265b5405 Устранено дублирование кода в AiAgent.
Добавлена возможность пересылки сообщений в личных чатах.
Обновлены зависимости.
Добавлен requirements.txt.
Исправлены предупреждения PyCharm 2026.1.
2026-04-07 01:01:34 +03:00

99 lines
3.7 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 functools import partial
from aiogram import Router, F, Bot
from aiogram.types import Message
from aiogram.enums.content_type import ContentType
import ai
import utils
from messages import *
import tg.tg_database as database
from tg.utils import *
router = Router()
ACCEPTED_CONTENT_TYPES: list[ContentType] = [
ContentType.TEXT,
ContentType.ANIMATION,
ContentType.AUDIO,
ContentType.DOCUMENT,
ContentType.PAID_MEDIA,
ContentType.PHOTO,
ContentType.STICKER,
ContentType.STORY,
ContentType.VIDEO,
ContentType.VIDEO_NOTE,
ContentType.VOICE,
ContentType.CHECKLIST,
ContentType.CONTACT,
ContentType.POLL,
ContentType.VENUE,
ContentType.LOCATION
]
@router.message(F.content_type.in_(ACCEPTED_CONTENT_TYPES))
async def any_message_handler(message: Message, bot: Bot):
chat_id = message.chat.id
chat = database.DB.create_chat_if_not_exists(bot.id, chat_id)
if chat['active'] == 0:
return
# Игнорировать ботов
if message.from_user is None or message.from_user.is_bot:
return
user_id = message.from_user.id
database.DB.create_user_if_not_exists(bot.id, chat_id, user_id)
database.DB.user_set_last_message(bot.id, chat_id, user_id, utils.posix_time())
database.DB.user_increment_messages(bot.id, chat_id, user_id)
bot_user = await bot.me()
ai_fwd_messages: list[ai.Message] = []
try:
message_text = get_message_text(message)
assert bot_user.username is not None
bot_username_mention = '@' + bot_user.username
if message_text is not None and message_text.find(bot_username_mention) != -1:
# Сообщение содержит @bot_username
message_text = message_text.replace(bot_username_mention, bot_user.first_name)
if message.reply_to_message:
# Сообщение также является ответом -> переслать оригинальное сообщение
ai_fwd_messages = [await create_ai_message(message.reply_to_message, bot)]
elif (message.reply_to_message and
message.reply_to_message.from_user is not None and message.reply_to_message.from_user.id == bot_user.id):
# Ответ на сообщение бота
last_id = ai.agent_instance.get_last_assistant_message_id(bot.id, chat_id)
if message.reply_to_message.message_id != last_id:
# Оригинального сообщения нет в контексте, или оно не последнее -> переслать его
ai_fwd_messages = [await create_ai_message(message.reply_to_message, bot)]
else:
return
except utils.UnsupportedContentType:
await message.reply(MESSAGE_UNSUPPORTED_CONTENT_TYPE)
return
ai_message = await create_ai_message(message, bot)
ai_message.text = message_text
answer: ai.agent.Message
success: bool
answer, success = await utils.run_with_progress(
partial(ai.agent_instance.get_group_chat_reply, bot.id, chat_id, ai_message, ai_fwd_messages),
partial(bot.send_chat_action, chat_id, 'typing'),
interval=4)
if answer.image is not None and answer.image_hires is not None:
answer_id = (await message.reply_photo(photo=wrap_photo(answer.image),
caption=trim_caption(answer.text))).message_id
await message.reply_document(document=wrap_document(answer.image_hires, 'image', 'png'))
elif answer.text is not None:
answer_id = (await message.reply(answer.text)).message_id
else:
return
if success:
ai.agent_instance.set_last_response_id(bot.id, chat_id, answer_id)