【问题标题】:The same phrase is repeating while using different command使用不同命令时重复相同的短语
【发布时间】:2021-12-20 16:09:10
【问题描述】:

我正在开发一个电报机器人。我在我的代码上执行此操作,问题是,为什么我的机器人在我输入第二个命令时会重复相同的第一个短语?

TOKEN_BOT = "secret token"
bot = telebot.TeleBot(TOKEN_BOT)

@bot.message_handler(commands=["hello", "start"])
def send(message):
    bot.reply_to(message, "Welcome, I am RIOPy! BOT designed to organize your SME to your liking. To start write / start and we will get to work.")

bot.polling()

def enviar_start (message):
    bot.reply_to(message, "Great! wait 5 seconds.")

#HERE DATABASE CODE

time.sleep(5)
bot.reply_to, "DATABASE CREATED"

bot.polling()

PS:代码现在可以工作了。我删除了 if 下的 bot.pulling(),并将 /hello 和 /start 命令放在了 2 个不同的行中。

@bot.message_handler(commands=["hello"])
def send(message):
    bot.reply_to(message, "Welcome, I am RIOPy! BOT designed to organize your SME to your liking. To start write / start and we will get to work.")

@bot.message_handler(commands=["start"])
def enviar_start (message):
    bot.reply_to(message, "Great! wait 5 seconds.")

【问题讨论】:

    标签: python py-telegram-bot-api


    【解决方案1】:

    你的机器人在你发送 /start 命令时说的第一件事也是你发送 /hello 命令时说的第一件事,因为你的机器人的 /hello 命令的消息处理程序也是 /start 的消息处理程序命令。要解决此问题,您需要创建两个单独的消息处理程序,一个用于 /hello 命令,一个用于 /start 命令。

    【讨论】:

    • 谢谢你,在哪里对,我更改了代码并删除了 if 下的 bot.pulling()。
    【解决方案2】:

    我觉得下面这几行代码是不需要的,你可以删掉

    if message == "start":
        enviar_start(message)```
    
    The edited code should work.
    

    【讨论】:

    • 你在哪里对!非常感谢!
    猜你喜欢
    • 2011-04-15
    • 2021-07-06
    • 2021-05-08
    • 2015-07-20
    • 2019-06-28
    • 1970-01-01
    • 2013-07-18
    • 2017-11-08
    • 1970-01-01
    相关资源
    最近更新 更多