Why is business_message_handler not working #2223
-
I want to write a bot that will respond to incoming messages in my personal account, but for some reason my bot does not process business_message_handler. I have enabled business in BotFather and update all libraries. import os
import sqlite3
import aiohttp
import asyncio
import json
import logging
import uuid
from telebot import TeleBot, types
from config import TOKEN, GIGACHATTOKEN
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
bot = TeleBot(TOKEN)
async def authorize():
url = "https://ngw.devices.sberbank.ru:9443/api/v2/oauth"
payload = 'scope=GIGACHAT_API_PERS'
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
'RqUID': str(uuid.uuid4()),
'Authorization': f'Basic {GIGACHATTOKEN}'
}
async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, data=payload, ssl=False) as response:
if response.status == 200:
data = await response.json()
return data.get('access_token')
else:
return None
async def call_gigachat_api(access_token, message_content):
url = "https://gigachat.devices.sberbank.ru/api/v1/chat/completions"
payload = {
"model": "GigaChat",
"messages": [{"role": "user", "content": message_content}],
"temperature": 1,
"top_p": 0.1,
"n": 1,
"stream": False,
"max_tokens": 512,
"repetition_penalty": 1
}
headers = {
'Authorization': f'Bearer {access_token}'
}
async with aiohttp.ClientSession() as session:
async with session.post(url, json=payload, headers=headers, ssl=False) as response:
return await response.json()
# Создание таблицы для хранения истории сообщений
def create_table():
conn = sqlite3.connect('chat_history.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS chat_history
(user_id INTEGER, business_connection_id TEXT, message_role TEXT, message_content TEXT)''')
conn.commit()
conn.close()
# Сохранение сообщения в базу данных
def save_message(user_id, business_connection_id, message_role, message_content):
conn = sqlite3.connect('chat_history.db')
c = conn.cursor()
c.execute(
"INSERT INTO chat_history (user_id, business_connection_id, message_role, message_content) VALUES (?, ?, ?, ?)",
(user_id, business_connection_id, message_role, message_content))
conn.commit()
conn.close()
# Получение истории сообщений для определенного пользователя и бизнес-соединения
def get_chat_history(user_id, business_connection_id):
conn = sqlite3.connect('chat_history.db')
c = conn.cursor()
c.execute(
"SELECT message_role, message_content FROM chat_history WHERE user_id=? AND business_connection_id=? ORDER BY rowid",
(user_id, business_connection_id))
chat_history = c.fetchall()
conn.close()
return chat_history
# Путь к файлу JSON для хранения данных о бизнес-соединениях
json_db_path = 'business_connections.json'
# Загрузка данных о бизнес-соединениях
def load_business_connections():
if os.path.exists(json_db_path):
with open(json_db_path, 'r') as file:
return json.load(file)
else:
logging.warning("Business connections file not found. Creating a new one.")
return {}
# Сохранение данных о бизнес-соединениях
def save_business_connections(business_connections):
logging.debug(f"Saving business connections: {business_connections}")
try:
with open(json_db_path, 'w') as file:
json.dump(business_connections, file, indent=2)
except Exception as e:
logging.error(f"Error saving business connections: {e}")
# Обновление бизнес-соединения
def update_business_connection(business_connection):
business_connection_id = business_connection.id
user_chat_id = business_connection.user_chat_id
can_reply = business_connection.can_reply
is_enabled = business_connection.is_enabled
date = business_connection.date
logging.debug(f"Updating business connection: {business_connection_id}")
business_connections = load_business_connections()
business_connections[business_connection_id] = {
'user_chat_id': user_chat_id,
'can_reply': can_reply,
'is_enabled': is_enabled,
'date': date
}
save_business_connections(business_connections)
# Получение бизнес-соединения
def get_business_connection(business_connection_id):
business_connections = load_business_connections()
return business_connections.get(business_connection_id)
# Обработчик бизнес-соединений
@bot.business_connection_handler(func=lambda business_connection: True)
def handle_business_connection(business_connection):
update_business_connection(business_connection)
logging.info(f"Business connection updated: {business_connection.id}")
@bot.business_message_handler(func=lambda message: True, content_types=['text', 'photo', 'video'])
def handle_business_message(message):
user_id = message.chat.id
business_connection_id = message.business_connection_id
logging.info(f"Received business message from {user_id}: {message.text}")
# Обновляем информацию о бизнес-соединении
business_connection = get_business_connection(business_connection_id)
if business_connection:
business_connection['can_reply'] = True
business_connection['is_enabled'] = True
save_business_connections(load_business_connections())
# Если это первое сообщение, создаем новое бизнес-соединение
else:
business_connection = {
'user_chat_id': message.chat.id,
'can_reply': True,
'is_enabled': True,
'date': message.date
}
business_connections = load_business_connections()
business_connections[business_connection_id] = business_connection
save_business_connections(business_connections)
if business_connection and business_connection.get('is_enabled') and business_connection.get('can_reply'):
try:
# Сохранение входящего сообщения в базу данных
save_message(user_id, business_connection_id, 'user', message.text)
# Получение истории сообщений из базы данных
chat_history = get_chat_history(user_id, business_connection_id)
# Генерация ответа с помощью GigaChat API
access_token = asyncio.run(authorize())
if access_token:
response = asyncio.run(call_gigachat_api(access_token, message.text))
if response:
message_text = response['choices'][0]['message']['content']
# Сохранение ответа бота в базу данных
save_message(user_id, business_connection_id, 'assistant', message_text)
bot.send_message(message.chat.id, message_text, reply_to_message_id=message.id,
business_connection_id=business_connection_id)
logging.info("Response sent to business chat")
else:
logging.error("Failed to generate response from GigaChat API")
else:
logging.error("Failed to get access token for GigaChat API")
except Exception as e:
logging.error(f"Error generating or sending response to business chat: {e}")
else:
logging.warning(f"Business connection not found or not enabled: {business_connection_id}")
# Обработчик личных сообщений
@bot.message_handler(content_types=['text'])
def handle_private_message(message):
logging.debug("Handling private message")
try:
# Генерация ответа с помощью GigaChat API
access_token = asyncio.run(authorize())
if access_token:
response = asyncio.run(call_gigachat_api(access_token, message.text))
if response:
message_text = response['choices'][0]['message']['content']
bot.reply_to(message, message_text)
else:
logging.error("Failed to generate response from GigaChat API")
else:
logging.error("Failed to get access token for GigaChat API")
except Exception as e:
logging.error(f"Error generating or sending response to private chat: {e}")
# Обработчик команды /start
@bot.message_handler(commands=['start'])
def handle_start(message):
logging.debug("Handling /start command")
bot.reply_to(message, "Привет! Я ваш бот.")
if __name__ == "__main__":
create_table() # Создание таблицы для хранения истории сообщений
logging.info("Starting the bot")
bot.infinity_polling() |
Beta Was this translation helpful? Give feedback.
Answered by
coder2020official
Apr 11, 2024
Replies: 2 comments
-
Well, you forgot to specify allowed_updates that would include business_message. For more, read the API docs |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Sirnilin
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Well, you forgot to specify allowed_updates that would include business_message. For more, read the API docs