PatataVerde
PatataVerde

Reputation: 3

Input from user in PyTelegramBotApi, Telegram

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.

The API i am using is this.

Upvotes: 0

Views: 4975

Answers (1)

PokerFaCe
PokerFaCe

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

Related Questions