N
N
NbIKS2021-05-10 17:53:41
Python
NbIKS, 2021-05-10 17:53:41

How to save user's messages for further use of telegram bot?

i am new to python. I'm learning how to create bots for a cart, now I'm trying to create some kind of questionnaire, which ultimately should save the user's answers to a list. Inside the get_name function, it saves and displays the name normally, but I can’t display the name in the future, and I can’t save it into a separate variable to display it either. But I will need it for future use.

When I don't write #message (# to show exactly where the error occurs, see the code) it gives "missing 1 required positional argument", and when I write "name 'message' is not defined"

I don't understand what to do, because outside of telegram here for example this code works fine

def func(a = input('Введите имя')):
    return a
b = (func())
print(b)


The code with which there was a problem:

@bot.message_handler(content_types=['text'])
def hello(message):

  if message.text == '/start':
    bot.send_message(message.from_user.id, "Введите имя");
    bot.register_next_step_handler(message, get_name);
  else:
    bot.send_message(message.from_user.id, '/start');

def get_name(message): 
    name = message.text
    return name
b = (get_name(#message))
print(b)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-05-10
@SoreMix

register_next_step_handler is not bound to anything.

@bot.message_handler(content_types=['text'])
def hello(message):

  if message.text == '/start':
    sent = bot.send_message(message.from_user.id, "Введите имя");
    bot.register_next_step_handler(sent, get_name);
  else:
    bot.send_message(message.from_user.id, '/start');

def get_name(message): 
    name = message.text
    print(name)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question