【问题标题】:how to make telegram bot listens to "normal messages" after handling "commands messages"处理“命令消息”后如何使电报机器人收听“正常消息”
【发布时间】:2020-09-26 03:55:37
【问题描述】:

我有一个电报机器人,它处理纯文本存储文本,用它做一些事情,然后返回结果。输出是正确的,但是,我希望机器人以不同的方式回复诸如“/start”“/help”之类的命令消息。我也成功地实现了这一点,事情是,例如,一旦 /start 完成,它就会产生所需的输出,然后它会继续处理文本,就像它是纯文本一样(在命令情况下不需要)。例如下面的一些代码,

@bot.message_handler(commands=['start'])
def send_welcome(message):
    msg = bot.reply_to(message, """\
Hi there, I am Humobot.
What do you want to verify?
""")
    bot.register_next_step_handler(msg,bot.set_update_listener(handle_messages))


##....  similar functions for /help and functions to process the info in message....


def handle_messages(messages):
    for message in messages:
        # Do something with the message
        bot.send_message(chat_id=message.chat.id,
                         text="Hold on Humo is running for you, he is still new and can barely walk")
        process_check_info(message)


bot.set_update_listener(handle_messages)
bot.polling()
bot.enable_save_next_step_handlers(delay=2)
bot.load_next_step_handlers()

bot.polling()

我觉得问题出在bot.register_next_step_handler(msg,bot.set_update_listener(handle_messages)) 我的可调用函数是 set_update_listener(handle_messages),我不知道如何告诉机器人只是等待下一条消息,然后如果是命令,则仅将其作为命令处理,如果是纯文本,则仅将其作为文本处理(后半部分,实际上工作正常)

我希望问题很清楚。

【问题讨论】:

    标签: python python-3.x telegram python-telegram-bot


    【解决方案1】:

    我环顾四周后解决了我的问题,错误是由于

    bot.set_update_listener(handle_messages)
    bot.polling()
    bot.enable_save_next_step_handlers(delay=2)
    bot.load_next_step_handlers()
    

    必须删除。

    【讨论】:

      猜你喜欢
      • 2017-01-08
      • 1970-01-01
      • 1970-01-01
      • 2017-03-19
      • 2017-07-17
      • 2021-09-09
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      相关资源
      最近更新 更多