vk_chat_bot/vk/handlers/default.py
Kirill Kirilenko 707eef30a5 Добавлен ответ на сообщение боту без текста.
Исправлен подсчет сообщений без текста в VK.
2026-01-19 16:03:42 +03:00

87 lines
3.2 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.

import re
from functools import partial
from typing import Optional
from vkbottle.bot import Message
from vkbottle.framework.labeler import BotLabeler
from vkbottle_types.codegen.objects import GroupsGroup
import ai_agent
import utils
from messages import *
import vk.vk_database as database
from vk.utils import get_user_name_for_ai
labeler = BotLabeler()
bot_user: Optional[GroupsGroup] = None
# Обычные сообщения (не команды и не действия)
@labeler.chat_message()
async def any_message_handler(message: Message):
chat_id = message.peer_id
chat = database.DB.create_chat_if_not_exists(chat_id)
if chat['active'] == 0:
return
# Игнорировать ботов
if message.from_id < 0:
return
# Не учитывать служебные сообщения
if message.action is not None:
return
user_id = message.from_id
database.DB.create_user_if_not_exists(chat_id, user_id)
database.DB.user_set_last_message(chat_id, user_id, utils.posix_time())
database.DB.user_increment_messages(chat_id, user_id)
global bot_user
if bot_user is None:
bot_user = (await message.ctx_api.groups.get_by_id()).groups[0]
ai_message = ai_agent.Message()
ai_fwd_messages: list[ai_agent.Message] = []
# Ответ на сообщение бота
if message.reply_message and message.reply_message.from_id == -bot_user.id:
ai_message.text = message.text
else:
# Сообщение содержит @bot_username
bot_username_mention = '@' + bot_user.screen_name
if message.text is not None and message.text.find(bot_username_mention) != -1:
ai_message.text = message.text.replace(bot_username_mention, bot_user.name)
else:
# Сообщение содержит [club<bot_id>|<some_name>]
pattern = r"\[club" + str(bot_user.id) + r"\|(.+)]"
if message.text is not None and re.search(pattern, message.text) is not None:
ai_message.text = re.sub(pattern, r'\1', message.text)
else:
return
if message.reply_message and len(message.reply_message.text) > 0:
ai_fwd_messages.append(
ai_agent.Message(user_name=await get_user_name_for_ai(message.ctx_api, message.reply_message.from_id),
text=message.reply_message.text))
else:
for fwd_message in message.fwd_messages:
if len(fwd_message.text) > 0:
ai_fwd_messages.append(
ai_agent.Message(user_name=await get_user_name_for_ai(message.ctx_api, fwd_message.from_id),
text=fwd_message.text))
if len(ai_message.text) == 0:
await message.reply(MESSAGE_NOT_TEXT)
return
ai_message.user_name = await get_user_name_for_ai(message.ctx_api, message.from_id)
chat_prompt = chat['ai_prompt']
await message.reply(
await utils.run_with_progress(
partial(ai_agent.agent.get_group_chat_reply, chat_id, chat_prompt, ai_message, ai_fwd_messages),
partial(message.ctx_api.messages.set_activity, peer_id=chat_id, type='typing'),
interval=4))