I'm making a bot using a staged processor, I need to get info from the bot's question into a date variable (and create it)for further addition to the database, how to do this ? Thanks in advance!

P.S: pay attention to code comments

P.S.s: I know how to add something to the database, the question is how can I get info from the user into a separate variable


import telebot
bot= telebot.TeleBot(token)
def add(message):
    msg= bot.send_message(message.chat.id, "Would you like to add a task?")
    bot.register_next_step_handler(msg, user_answer)
def user_answer(message):
    if message.text== "yes":
        msg= bot.send_message(message.chat.id, "Enter date for task:")#from this question
        bot.register_next_step_handler(msg, user_date)
    elif message.text== "no":
        bot.send_message(message.chat.id, "OK!")
        bot.send_message(message.chat.id, ntry)
def user_date(message):
    bot.send_message(message.chat.id, "Your date:")#here the date should be displayed to the user + saved (for further addition to the database)

If I understand your task correctly, then here is the link for you: docs.python.org/3/library/sqlite3.html

tomato-magnet-regulato2022-02-14 06:56:01

No, not that. It is necessary to "get" the user's message into a separate variable

Дмитрий Капралов2022-02-14 07:02:56

@DmitryKapralov in this case, create a global variable with the value None and store the message in the user_data function

fancier2022-02-14 07:30:25

tell me the code, it's hard

Дмитрий Капралов2022-02-14 07:31:57
  • Answer # 1
    #creating a variable outside of a function
    user_name= 'Bob'
    def save_user_name():
        #Declaring the user_name variable with global inside the function
        global user_name
        user_name= user_name + 'Jonson'
    print('New Name :', user_name)
    Output: New Name : Bob Jonson