Python telebot 不能与不同的用户一起工作
Python telebot not working with different users
我是开发新手,python也是。我尝试使用 Telebot 编写一个简单的电报机器人。该方案是在用户单击按钮执行某些逻辑时向用户显示内联键盘。在下面的示例中,我剪切了代码,但它显示了问题。问题是:
当第一个用户开始工作时,他会收到正确的所有通知。但是当第二个用户开始使用机器人时,他得到了正确的键盘,但通知将发送给第一个用户。
这是一个代码示例:
import telebot
import datetime
bot = telebot.TeleBot(insert_token_here)
keyboard1 = telebot.types.ReplyKeyboardMarkup(True)
keyboard1.row('Choose date', 'dont push it')
@bot.message_handler(commands=['start'])
def start_message(message):
bot.send_message(message.chat.id, 'Welcome', reply_markup=keyboard1)
def dates_inline():
current_date = datetime.datetime.today()
# Inline keyboard
keyboard_dates = telebot.types.InlineKeyboardMarkup()
key_now = telebot.types.InlineKeyboardButton(text=current_date.strftime('%d.%m.%Y') + ' (Today)',
callback_data=current_date.strftime('%Y-%m-%d'))
keyboard_dates.add(key_now)
return keyboard_dates
@bot.message_handler(content_types=['text'])
def choose_message(message):
if message.text == "Choose date":
bot.send_message(message.chat.id, 'Choose date:', reply_markup=dates_inline())
@bot.callback_query_handler(func=lambda call: True)
def choose_date(call):
dt = call.data
print('chose_date dt: %s' % dt)
bot.send_message(message.chat.id, 'All done')
print('end')
else:
print('smth else')
def main():
bot.polling(none_stop=True)
if __name__ == '__main__':
main()
您需要将以下代码移动到 top-level 缩进。否则它不会像您预期的那样工作。
@bot.callback_query_handler(func=lambda call: True)
def choose_date(call):
dt = call.data
print('chose_date dt: %s' % dt)
bot.send_message(message.chat.id, 'All done')
@wowkin2 这是示例代码:
@bot.message_handler(content_types=['text'])
def choose_message(message):
if message.text == "Choose date":
bot.send_message(message.chat.id, 'Choose date:', reply_markup=dates_inline())
print('end')
else:
print('smth else')
@bot.callback_query_handler(func=lambda call: True)
def choose_date(call):
dt = call.data
bot.send_message(message.chat.id, 'All done')
我也遇到了类似的问题
- 不要在另一个里面创建 handler/decorator。它不是那样工作的。我对python也比较陌生,所以我不知道确切的原因。我也从我的错误中吸取了教训。
- 不要将消息发回给 message.chat.id。将其发送至 call.from_user.id,以便它始终将回复发送回来电的用户。
@bot.message_handler(content_types=['text'])
def choose_message(message):
if message.text == "Choose date":
bot.send_message(message.chat.id, 'Choose date:', reply_markup=dates_inline())
else:
print('smth else')
@bot.callback_query_handler(func=lambda call: True)
def choose_date(call):
dt = call.data
print('chose_date dt: %s' % dt)
bot.send_message(call.from_user.id, 'All done')
print('end')
我现在也在开发一个机器人,这对我来说工作得很好。
我是开发新手,python也是。我尝试使用 Telebot 编写一个简单的电报机器人。该方案是在用户单击按钮执行某些逻辑时向用户显示内联键盘。在下面的示例中,我剪切了代码,但它显示了问题。问题是: 当第一个用户开始工作时,他会收到正确的所有通知。但是当第二个用户开始使用机器人时,他得到了正确的键盘,但通知将发送给第一个用户。
这是一个代码示例:
import telebot
import datetime
bot = telebot.TeleBot(insert_token_here)
keyboard1 = telebot.types.ReplyKeyboardMarkup(True)
keyboard1.row('Choose date', 'dont push it')
@bot.message_handler(commands=['start'])
def start_message(message):
bot.send_message(message.chat.id, 'Welcome', reply_markup=keyboard1)
def dates_inline():
current_date = datetime.datetime.today()
# Inline keyboard
keyboard_dates = telebot.types.InlineKeyboardMarkup()
key_now = telebot.types.InlineKeyboardButton(text=current_date.strftime('%d.%m.%Y') + ' (Today)',
callback_data=current_date.strftime('%Y-%m-%d'))
keyboard_dates.add(key_now)
return keyboard_dates
@bot.message_handler(content_types=['text'])
def choose_message(message):
if message.text == "Choose date":
bot.send_message(message.chat.id, 'Choose date:', reply_markup=dates_inline())
@bot.callback_query_handler(func=lambda call: True)
def choose_date(call):
dt = call.data
print('chose_date dt: %s' % dt)
bot.send_message(message.chat.id, 'All done')
print('end')
else:
print('smth else')
def main():
bot.polling(none_stop=True)
if __name__ == '__main__':
main()
您需要将以下代码移动到 top-level 缩进。否则它不会像您预期的那样工作。
@bot.callback_query_handler(func=lambda call: True)
def choose_date(call):
dt = call.data
print('chose_date dt: %s' % dt)
bot.send_message(message.chat.id, 'All done')
@wowkin2 这是示例代码:
@bot.message_handler(content_types=['text'])
def choose_message(message):
if message.text == "Choose date":
bot.send_message(message.chat.id, 'Choose date:', reply_markup=dates_inline())
print('end')
else:
print('smth else')
@bot.callback_query_handler(func=lambda call: True)
def choose_date(call):
dt = call.data
bot.send_message(message.chat.id, 'All done')
我也遇到了类似的问题
- 不要在另一个里面创建 handler/decorator。它不是那样工作的。我对python也比较陌生,所以我不知道确切的原因。我也从我的错误中吸取了教训。
- 不要将消息发回给 message.chat.id。将其发送至 call.from_user.id,以便它始终将回复发送回来电的用户。
@bot.message_handler(content_types=['text'])
def choose_message(message):
if message.text == "Choose date":
bot.send_message(message.chat.id, 'Choose date:', reply_markup=dates_inline())
else:
print('smth else')
@bot.callback_query_handler(func=lambda call: True)
def choose_date(call):
dt = call.data
print('chose_date dt: %s' % dt)
bot.send_message(call.from_user.id, 'All done')
print('end')
我现在也在开发一个机器人,这对我来说工作得很好。