B
B
Beko962021-02-19 09:45:11
Python
Beko96, 2021-02-19 09:45:11

How to record the data that is sent to the bot?

The bot is given the usual str, they must be written to a regular list (data), but the list is empty as a result and nothing was written, what to do?

import telebot

bot = telebot.TeleBot(' - - - ')

data = []

def main():
  
  @bot.message_handler(commands = ['start'])
  def foo(message):
    msg = bot.send_message(message.chat.id,'введите имя')
    bot.register_next_step_handler(msg,name)

  def name(message):
    
    name = message.text
    
    data.append(name)

    msg = bot.send_message(message.chat.id,'введите возраст')

    bot.register_next_step_handler(msg,age)


  def age(message):
    data.append(message.age)

  return data

if __name__ == '__main__':
  
  print(main())
  
  bot.enable_save_next_step_handlers(delay=2)
  bot.load_next_step_handlers()
  bot.polling()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2021-02-19
@Beko96

The list is filled, it's just that in the current code it is returned only once (at the very beginning, when it is still empty). main() only runs once.
Can be seen by adding the /info command for example

@bot.message_handler(commands = ['info'])
def send_info(message):
  bot.send_message(message.chat.id, f'data={data}')

So you don't need to wrap all these functions in main, they just have to go at the global level.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question