PyTelegramBotApi、Telegram 中用户的输入

Input from user in PyTelegramBotApi, Telegram

我想在 Telegram 中创建一个带计算器的机器人,但我不知道如何将用户消息的输入保存到变量中。

我的想法是用户发送两个数字,然后机器人将两个数字相加并将结果发送给用户。

The API i am using is this.

您可以使用 register_next_step_handler.
示例 1:
https://github.com/eternnoir/pyTelegramBotAPI/blob/master/examples/step_example.py

示例 2:

import telebot

bot = telebot.TeleBot("api") 

@bot.message_handler(content_types=['text'])
def welcome(pm):
    sent_msg = bot.send_message(pm.chat.id, "Welcome to bot. what's your name?")
    bot.register_next_step_handler(sent_msg, name_handler) #Next message will call the name_handler function
    
def name_handler(pm):
    name = pm.text
    sent_msg = bot.send_message(pm.chat.id, f"Your name is {name}. how old are you?")
    bot.register_next_step_handler(sent_msg, age_handler, name) #Next message will call the age_handler function

def age_handler(pm, name):
    age = pm.text
    bot.send_message(pm.chat.id, f"Your name is {name}, and your age is {age}.")

bot.polling()