vk_chat_bot/vk/handlers/private.py

66 lines
2.1 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 vkbottle.bot import Message
from vkbottle.dispatch.rules.base import RegexRule
from vkbottle.framework.labeler import BotLabeler
import ai_agent
import utils
from messages import *
import vk.vk_database as database
from vk.utils import *
labeler = BotLabeler()
@labeler.private_message(text="!старт")
async def start_handler(message: Message):
bot_id = get_bot_id(message.ctx_api)
chat_id = message.peer_id
database.DB.create_chat_if_not_exists(bot_id, chat_id)
database.DB.chat_update(bot_id, chat_id, active=1)
await message.answer("Привет!")
@labeler.private_message(RegexRule(r"^!личность ((?:.|\n)+)"))
async def set_prompt_handler(message: Message, match):
bot_id = get_bot_id(message.ctx_api)
chat_id = message.peer_id
database.DB.create_chat_if_not_exists(bot_id, chat_id)
database.DB.chat_update(bot_id, chat_id, ai_prompt=match[0])
await message.answer("Личность ИИ изменена.")
@labeler.private_message(text="!сброс")
async def reset_context_handler(message: Message):
bot_id = get_bot_id(message.ctx_api)
chat_id = message.peer_id
database.DB.create_chat_if_not_exists(bot_id, chat_id)
ai_agent.agent.clear_chat_context(bot_id, chat_id)
await message.answer("Контекст очищен.")
@labeler.private_message()
async def any_message_handler(message: Message):
bot_id = get_bot_id(message.ctx_api)
chat_id = message.peer_id
try:
ai_message = await create_ai_message(message)
except utils.UnsupportedContentType:
await message.answer(MESSAGE_UNSUPPORTED_CONTENT_TYPE)
return
answer: ai_agent.Message
success: bool
answer, success = await utils.run_with_progress(
partial(ai_agent.agent.get_private_chat_reply, bot_id, chat_id, ai_message),
partial(message.ctx_api.messages.set_activity, peer_id=chat_id, type='typing'),
interval=4)
answer_id = (await message.answer(answer.text)).message_id
if success:
ai_agent.agent.set_last_response_id(bot_id, chat_id, answer_id)