Answer the question
In order to leave comments, you need to log in
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)
Answer the question
In order to leave comments, you need to log in
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question