Reputation: 3
I want to create a bot with calculator in Telegram, but i don't know how i can save the input from user message to save in a variable.
My idea is the user send two numbers, and the bot sum both numbers and send in a message the result to the user.
Upvotes: 0
Views: 4975
Reputation: 409
You can use register_next_step_handler
.
example 1:
https://github.com/eternnoir/pyTelegramBotAPI/blob/master/examples/step_example.py
example 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()
Upvotes: 4