Answer the question
In order to leave comments, you need to log in
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()
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question