0
votes

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.

1
Hi! Welcome to StackOverflow! Please read our tour to get a better understanding about how to ask questions around here. Could you share some code, showing your attempts is required here. - 0stone0
That said, there's no need to 'save the input into a variable' if you're trying to send back the sum of those messages - 0stone0
You get user input using register_next_step_handler - itsAPK

1 Answers

1
votes

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()