Answer the question
In order to leave comments, you need to log in
telebot. How to send a message to another user?
I recently started looking into the pytelegrambotapi library. I decided to try writing a bot that takes orders.
When the order is written, the bot sends it to the account with the chat_id, which is stored in the owner variable. There appears an inlain keyboard with an accept button. After clicking this button, a message is sent to the user who made the order. The chat_id of this user is pre-written to the user variable. But when several people use the bot at the same time, the bot remembers the last chat_id and does not send a message to the previous user. Help, please, to solve this problem.
import telebot
from telebot import types
owner=chat_id
bot=telebot.TeleBot('token')
@bot.message_handler(commands=['start'])
def start(message):
key=types.ReplyKeyboardMarkup()
button1=types.KeyboardButton('Меню')
button2=types.KeyboardButton('Сделать заказ')
key.add(button1,button2)
bot.send_message(message.chat.id,'Привет, здесь ты можешь сделать заказ.',reply_markup=key)
@bot.message_handler(content_types='text')
def work(message):
if message.text=='Меню':
p1=types.InputMediaPhoto(open(r'1.png','rb'))
p2=types.InputMediaPhoto(open(r'2.png','rb'))
bot.send_media_group(message.chat.id,[p1,p2])
elif message.text=='Сделать заказ':
msg=bot.send_message(message.chat.id,'Напишите что вы выбрали(Одним сообщением через запятую)')
bot.register_next_step_handler(msg,z)
else:
bot.send_message(message.chat.id,'Я вас не понял(')
def z(message):
global z
z=message.text
msg=bot.send_message(message.chat.id,'Напишите адрес')
bot.register_next_step_handler(msg,a)
def a(message):
global a
ikey=types.InlineKeyboardMarkup()
button_yes=types.InlineKeyboardButton(text='Принят',callback_data='yes')
ikey.row(button_yes)
a=message.text
e1='Заказ: '+z
e2='Адрес: '+a
e=e1+'\n'+e2
bot.send_message(owner,e,reply_markup=ikey)
bot.send_message(message.chat.id,'Я отправил ваш заказ.Пожалуйста, подождите.')
global user
user=message.chat.id
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
if call.message:
if call.data=='yes':
bot.send_message(user,'Принято')
bot.polling()
Answer the question
In order to leave comments, you need to log in
You recently started learning Python. And yes, programming.
Start with variable names. Not only do you have global a, z, but you also called the functions that way. You read and do not understand what the author wanted to say.
As a tip on the topic of the question - When creating an Inline (not Inlain) button for the owner user, put the user_id of the user from which the order came from in the callback_data.
button_yes=types.InlineKeyboardButton(text='Принят',callback_data=f'yes|{message.chat.id}')
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
answer, user = call.data.split('|')
if answer=='yes':
bot.send_message(user,'Принято')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question