K
K
kreeplambor2021-12-20 02:36:45
Bots
kreeplambor, 2021-12-20 02:36:45

Telegram bot shows its data on from_user. What to do?

I am learning how to write telegram bots. Here is this piece of code:

elif call.data=='id':
        bot.send_message(call.message.chat.id, F'Твой ID: {call.message.from_user.id}')
      elif call.data=='nick':
        bot.send_message(call.message.chat.id, f'Твой Nickname: {call.message.from_user.first_name}, {call.message.from_user.last_name}')

In this place, the bot gives out its data instead of the user's data (I don't know how to check the id, but the nickname writes its own).
Where did I screw up?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
shurshur, 2021-12-20
@kreeplambor

call.message - the initial message in which the user pressed the button. Of course, the author of this message is a bot.
Call.from_user should be used instead of call.message.from_user.
The idea of ​​using call.message.chat.id to get the user id will only work as long as it is used in a private chat with the user, where chat_id is always equal to user_id. This will not work in group chats.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question