1
1
1grand2019-08-22 00:00:36
Python
1grand, 2019-08-22 00:00:36

How to get user id / chat id telebot?

There is a code for a telegram bot (Telebot or pyTelegramBotApi).

@bot.message_handler(commands=['start'])
def start_message(message):
    bot.send_message(message.chat.id, 'test')

You need to get the user ID (message.chat.id) in a variable, without sending a message. How can I do that?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Danil K., 2019-08-28
@Danya_Violet

body = '{message}\n' \
           '--\n' \
           '{first}, {last}\n' \
           '{username}, {id}'.format(message=message.text, first=message.from_user.first_name,
                                     last=message.from_user.last_name, username=message.from_user.username,
                                     id=message.chat.id)

G
Groosha, 2019-08-22
@Groosha

message.from_user.id is more correct, because in groups, message.chat.id will return the group ID, not the specific user. But if you are sure that the case will be limited only to dialogues (private messages), then message.chat.id is also appropriate.

@bot.message_handler(commands=['start'])
def start_message(message):
    your_variable = message.from_user.id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question