【问题标题】:Python telebot not working with different usersPython Telebot 不能与不同的用户一起工作
【发布时间】:2021-01-09 07:36:11
【问题描述】:

我也是开发和 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()

【问题讨论】:

    标签: python telegram py-telegram-bot-api


    【解决方案1】:

    我也遇到过类似的问题。

    1. 不要在另一个内部创建处理程序/装饰器。它不是那样工作的。我对python也比较陌生,所以我不知道确切的原因。我也从我的错误中吸取了教训。
    2. 不要将消息发送回 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')
        
    
    

    我现在也在开发一个机器人,这对我来说很好。

    【讨论】:

      【解决方案2】:

      您需要将以下代码移至顶级缩进。否则,它不会按您的预期工作。

          @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')
      

      【讨论】:

      • 我试着像你写的那样做,但我遇到了同样的问题。
      • 我在answer 中发布了一个代码示例,因为我不能像这里的“cmets”中的代码那样格式化
      【解决方案3】:

      @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')
      

      【讨论】:

      • 不要在另一个方法中定义一种方法,就像我之前在回答中提到的那样。了解 Python 制表符、空格和缩进。
      猜你喜欢
      • 2020-12-03
      • 2013-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-09
      • 2018-01-06
      相关资源
      最近更新 更多