D
D
Danila2019-12-21 11:50:15
Python
Danila, 2019-12-21 11:50:15

How to find out the user_id in Telegram Bot by clicking on the button (InlineKeyboard)?

Good afternoon. I have an inline keyboard in the bot, when the buttons are pressed, values ​​are written / read from the database.
To read/write data, I need to know the user_id of the user.
How to get the user_id of the user who clicked the button?
Now I take the user_id step back and save it to a variable, but now when several people already use my bot, sometimes someone overwrites this variable and the person receives data from the database of a completely different user ...
In general, how to take the user_id of the user who clicked inline button?
Like in this code for example:

@bot.callback_query_handler(func=lambda call: call.data in ['locs0', 'locs1', 'locs2'])
        def check_weather(c):
            global latitude, longitude
            if c.data == 'locs0':
                values = ({'user_id': idToSave})
                cursor.execute("SELECT latitude, longitude FROM locations WHERE user_id = %(user_id)s", values)
                lalo = cursor.fetchall()
                latitude = lalo[0][0]
                longitude = lalo[0][1]
                nextstep_check_weather(c)

find user_id? How to replace the idToSave variable?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Neverov, 2019-12-21
@TTATPuOT

The CallbackQuery object has a "from" field - this is the User object that initiated the callback.
User'a has an id - this is your chat_id.

K
Kt0T0, 2021-04-27
@Kt0T0

A bit late, here's how to get:
call.from_user.id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question