L
L
Lenskii2021-02-10 19:09:32
Python
Lenskii, 2021-02-10 19:09:32

How to make a variable in a bot global for only one person?

Hello! Faced the following problem:
I am making a telegram bot that leaves comments on the site.
Using this code:

def desc(message):
    username = message.from_user.username
    idd = message.from_user.id
    msg = message.text
    c.execute("INSERT INTO ads (worker,wid,title) VALUES (%s,%s,%s)",(username,idd,msg))
    link.commit()
    c.execute("SELECT id FROM ads WHERE worker = %s AND wid = %s AND title = %s",(username,idd,msg))
    global ad_id
    ad_id = c.fetchall()[0]['id'];
    msg = bot.send_message(message.chat.id,"Коментарий:")
    bot.register_next_step_handler(msg,price)
def price(message):
    username = message.from_user.username
    idd = message.from_user.id
    msg = message.text
    c.execute("UPDATE ads SET description = %s WHERE id = %s",(msg,ad_id))
    link.commit()
    msg = bot.send_message(message.chat.id,"Теперь цена с копейками (Пример: 7000.00)")
    bot.register_next_step_handler(msg,comision)
def comision(message):
    username = message.from_user.username
    idd = message.from_user.id
    msg = message.text
    c.execute("UPDATE ads SET price = %s WHERE id = %s",(msg,ad_id))
    link.commit()
    msg = bot.send_message(message.chat.id,"На последок комиссию укажи (Пример: 70.00)")
    bot.register_next_step_handler(msg,succes)

The problem is that you need to remember somewhere who writes this comment at all, using a database is stupid in my opinion in this situation, local variables are visible only in the function scope. There is only one way out - to go to the global one, but here again there is a problem, the global variable is created for everyone, that is, if the second user starts writing a comment, he will write it not under his own nickname. In general, everything is somehow bad for me (Please help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-02-10
@Milovenskii

variables are only visible in the scope of the function. There is only one way out - to fuck in the global

Can this variable be passed to register_next_step_handler?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question