S
S
Stanislav Tronikov2020-04-25 22:54:33
Python
Stanislav Tronikov, 2020-04-25 22:54:33

How to force telegram bot to send username?

Friends, there is a bot that just redirects me messages when they write to it, but it does not forward me the username of the client, it just sends me messages from those who downloaded it. Here is his code, I somehow wrote it a couple of years ago and forgot a little how I did it, now I need it again, but how can I screw it up?

import telebot
bot = telebot.TeleBot('00000')

chat_id = 'тут айди моего чата обычного'  

# Telegram bot: Get My Id

@bot.message_handler(commands=["start"])
def main(message):
  bot.send_message(message.chat.id, text="Привет, это бот!")


@bot.message_handler(func=lambda message: True)
def echo_message(message):
  bot.send_message(chat_id=chat_id, text=message.text)

bot.polling()


In principle, the solution is ready here https://github.com/trofimovdev/telresender but it sends me a message like

556664647: Hello world

The first is a chat id, how can I answer it quickly, is it possible that he would send me Normal data a user that I could quickly reply to

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Krostelev, 2020-04-26
@twistfire92

Some users have neither a normal username, which can be found through @, nor a last name, etc.
In general, all information about the sender is in message.from_user
message.from_user.id - user id, in fact, what the example from the github
message returns .from_user.first_name - First name
message.from_user.last_name - Last name
message.from_user.username - username
Perhaps these fields will not be in responses from those users who do not have them filled.
But it's all very well described in the documentation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question