I have a problem with the Telegram bot. When users send questions they come to chat with admins, they answer the question, but the bot does not send the answer back to the bot. I would be grateful for help in solving this problem.
import telebot
from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
bot_token = '***'
group_id = ***
bot = telebot.TeleBot(bot_token)
user_last_messages = {}
@bot.message_handler(func=lambda message: message.chat.id == group_id)
def tsak(message):
pass
@bot.message_handler(commands=['start'])
def start(message):
keyboard = InlineKeyboardMarkup()
keyboard.row(InlineKeyboardButton('Смена даты рождения', callback_data='question'))
keyboard.row(InlineKeyboardButton('Вопрос по заказу', callback_data='order_help'))
keyboard.row(InlineKeyboardButton('Информация о доставке', callback_data='delivery_dates'))
bot.send_message(message.chat.id, 'Здравствуйте! Я бот поддержки. Чем могу помочь?', reply_markup=keyboard)
@bot.callback_query_handler(func=lambda call: True)
def callback_handler(call):
if call.message:
user_id = call.from_user.id
if call.data == 'question':
user_last_messages[user_id] = {'type': 'question', 'message_id': call.message.message_id}
bot.send_message(call.message.chat.id,
"Вы выбрали 'Смена даты рождения'. Укажите верную дату рождения и номер телефона, "
"к которому привязан ваш аккаунт.")
elif call.data == 'order_help':
user_last_messages[user_id] = {'type': 'order_help', 'message_id': call.message.message_id}
bot.send_message(call.message.chat.id, "Вы выбрали 'Помощь с заказами'. Напишите свой вопрос.")
elif call.data == 'delivery_dates':
bot.send_message(call.message.chat.id,
"Информация о доставке: Обратите внимание, что после поступления заказа в "
"пункт выдачи вам придет СМС о его готовности к выдаче. Срок хранения "
"заказа составляет от 7 до 14 дней.")
@bot.message_handler(content_types=['text'])
def handle_text_message(message):
user_id = message.from_user.id
if user_id in user_last_messages:
last_message_info = user_last_messages[user_id]
if last_message_info['type'] in ['question', 'order_help']:
bot.send_message(group_id, f'Пользователь {message.from_user.username} задал вопрос:n{message.text}')
user_last_messages[user_id]['sent_message_id'] = message.message_id
bot.send_message(message.chat.id, "Специалист сейчас освободится и ответит на ваш вопрос.")
@bot.message_handler(func=lambda message: message.chat.id == group_id)
def handle_group_message(message, response_message=None):
if message.reply_to_message:
original_message = message.reply_to_message
user_id = None
for user, last_message_info in user_last_messages.items():
if last_message_info.get('sent_message_id') == original_message.message_id:
user_id = user
break
if user_id:
bot.send_message(chat_id=message.chat.id, text=response_message)
if __name__ == "__main__":
bot.polling(none_stop=True)
I tried to store in the user_last_messages array, in addition to message_id, the identifier of the chat from which the message was received.
New contributor
Alina is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.