我正在使用TeleBot创建一个简单的机器人.Start Func为用户提供两个按钮(货币/天气).我预计当按下一个按钮时,机器人会移动到"go 哪里"的功能.只有当我按两次按钮或发送其他任何第二条消息时,才会出现这种情况.然后"Where_to_go"函数读取第一条消息.所以它是有效的,但为什么我要再发送一条消息来推动它呢?我怎么才能修好它呢?

以下是代码:

@bot.message_handler(commands = ['start','main'])
    def start(message):
        markup = types.ReplyKeyboardMarkup()  
        btn1 = types.KeyboardButton('Currency Calc')
        markup.row(btn1)
        btn2 = types.KeyboardButton('Weather Today')
        markup.row(btn2)
        file = open('./Start Photo.png', 'rb')
        bot.send_photo(message.chat.id, file, reply_markup=markup)
        bot.send_message(message.chat.id,
                             f"Good night, {message.from_user.first_name} {message.from_user.last_name} 💋. \n")
        bot.send_message(message.chat.id, f"I'm PogodiPogoda Bot. Choose a button or just talk to me.", reply_markup=markup)
        bot.register_next_step_handler(message, where_to_go)
    
   
def where_to_go(message):
        
        if message.text == 'Currency Calc':
            bot.register_next_step_handler(message, converter)
        elif message.text == 'Weather Today':
            bot.register_next_step_handler(message, city)

当他执行以下步骤(从"WHERE_TO_GO"到函数"Converter"或"City")时,机器人工作得很好.

推荐答案

您可以try 以下操作:

@bot.message_handler(commands = ['start','main'])
def start(message):
    markup = types.ReplyKeyboardMarkup()  
    btn1 = types.KeyboardButton('Currency Calc')
    markup.row(btn1)
    btn2 = types.KeyboardButton('Weather Today')
    markup.row(btn2)
    file = open('./Start Photo.png', 'rb')
    bot.send_photo(message.chat.id, file, reply_markup=markup)
    bot.send_message(message.chat.id, f"Good night, {message.from_user.first_name} {message.from_user.last_name} 💋. \n")
    bot.send_message(message.chat.id, f"I'm PogodiPogoda Bot. Choose a button or just talk to me.", reply_markup=markup)
    # Register next step handler directly within the start function
    bot.register_next_step_handler(message, where_to_go)

def where_to_go(message):
    if message.text == 'Currency Calc':
        converter(message)  # Call the function directly instead of registering another next step handler
    elif message.text == 'Weather Today':
        city(message)  # Call the function directly instead of registering another next step handler

@bot将在用户 Select 一个选项后立即移动到where_to_go函数,而不需要额外的消息

Python相关问答推荐

Python 约束无法解决n皇后之谜

如何标记Spacy中不包含特定符号的单词?

. str.替换pandas.series的方法未按预期工作

2D空间中的反旋算法

pandas滚动和窗口中有效观察的最大数量

如何在Python中并行化以下搜索?

从嵌套的yaml创建一个嵌套字符串,后面跟着点

无法连接到Keycloat服务器

从Windows Python脚本在WSL上运行Linux应用程序

如何在达到end_time时自动将状态字段从1更改为0

如果有2个或3个,则从pandas列中删除空格

如何编辑此代码,使其从多个EXCEL文件的特定工作表中提取数据以显示在单独的文件中

如何获取包含`try`外部堆栈的`__traceback__`属性的异常

极点替换值大于组内另一个极点数据帧的最大值

我怎么才能用拉夫分拣呢?

如何在Python中解析特定的文本,这些文本包含了同一行中的所有内容,

无法使用请求模块从网页上抓取一些产品的名称

如何在Python中实现高效地支持字典和堆操作的缓存?

普洛特利express 发布的人口普查数据失败

如何在Python中画一个只能在对角线内裁剪的圆?