Python telegram bot username

How do I get the username of a Telegram user in the main function of a python Telegram bot?

I’m programming a Telegram bot in python using the library python-telegram-bot , but I have a problem: I need to get the username of the user (sorry for the pun), but I don’t know how to do it in the main function. I have already searched on the internet, and everyone get the username in a function, using update.message.from_user.username . But I need to do it in the main function, because I want to send this username and some text to another Telegram user. Can you help me? My current code is this:

import telegram import logging from telegram.ext import CommandHandler, CallbackQueryHandler, Updater from functions import start, button bot = telegram.Bot(token='') updater = Updater(token='') dispatcher = updater.dispatcher logging.basicConfig(format='%(asctime)s - %(name)s' ' - %(levelname)s - %(message)s', level=logging.INFO) start_handler = CommandHandler('start', start) dispatcher.add_handler(start_handler) updater.dispatcher.add_handler(CallbackQueryHandler(button)) updater.start_polling() updater.idle() updater.stop() 

2 Answers 2

See if this is what you’re looking for:

def start (update, context): #this will retrieve the user's username, as you already know user = update.message.from_user #this will send the information to some Telegram user context.bot.send_message(chat_id = some_user_chat_id, text = f'User just hit start command!') 

Once the user hit /start command, his/her username will be sent to the chat_id of your choice.

Источник

telegram.User¶

str – Optional. IETF language tag of the user’s language.

telegram.Bot – Optional. The Bot to use for instance methods.

  • id ( int ) – Unique identifier for this user or bot.
  • is_bot ( bool ) – True, if this user is a bot
  • first_name ( str ) – User’s or bot’s first name.
  • last_name ( str , optional) – User’s or bot’s last name.
  • username ( str , optional) – User’s or bot’s username.
  • language_code ( str , optional) – IETF language tag of the user’s language.
  • bot ( telegram.Bot , optional) – The Bot to use for instance methods.
Читайте также:  Питон как переименовать файл

str – Convenience property. The user’s first_name , followed by (if available) last_name .

get_profile_photos ( *args, **kwargs ) ¶

bot.get_user_profile_photos(update.message.from_user.id, *args, **kwargs) 

str – Convenience property. If username is available, returns a t.me link of the user.

mention_html ( name=None ) ¶

Parameters: name ( str ) – The name used as a link for the user. Defaults to full_name .
Returns: The inline mention for the user as HTML.
Return type: str

mention_markdown ( name=None ) ¶

Parameters: name ( str ) – The name used as a link for the user. Defaults to full_name .
Returns: The inline mention for the user as markdown.
Return type: str

name ¶

str – Convenience property. If available, returns the user’s username prefixed with “@”. If username is not available, returns full_name .

bot.send_animation(User.id, *args, **kwargs) 

Where User is the current instance.

Returns: On success, instance representing the message posted.
Return type: telegram.Message

send_audio ( *args, **kwargs ) ¶

bot.send_audio(User.id, *args, **kwargs) 

Where User is the current instance.

Returns: On success, instance representing the message posted.
Return type: telegram.Message

send_document ( *args, **kwargs ) ¶

bot.send_document(User.id, *args, **kwargs) 

Where User is the current instance.

Returns: On success, instance representing the message posted.
Return type: telegram.Message

send_message ( *args, **kwargs ) ¶

bot.send_message(User.id, *args, **kwargs) 

Where User is the current instance.

Returns: On success, instance representing the message posted.
Return type: telegram.Message

send_photo ( *args, **kwargs ) ¶

bot.send_photo(User.id, *args, **kwargs) 

Where User is the current instance.

Returns: On success, instance representing the message posted.
Return type: telegram.Message

send_sticker ( *args, **kwargs ) ¶

bot.send_sticker(User.id, *args, **kwargs) 

Where User is the current instance.

Returns: On success, instance representing the message posted.
Return type: telegram.Message

send_video ( *args, **kwargs ) ¶

bot.send_video(User.id, *args, **kwargs) 

Where User is the current instance.

Returns: On success, instance representing the message posted.
Return type: telegram.Message

send_video_note ( *args, **kwargs ) ¶

bot.send_video_note(User.id, *args, **kwargs) 

Where User is the current instance.

Returns: On success, instance representing the message posted.
Return type: telegram.Message

send_voice ( *args, **kwargs ) ¶

bot.send_voice(User.id, *args, **kwargs) 

Where User is the current instance.

Returns: On success, instance representing the message posted.
Return type: telegram.Message

© Copyright 2015-2018, Leandro Toledo Revision 7eeb670a .

Versions latest stable Downloads pdf On Read the Docs Project Home Builds Free document hosting provided by Read the Docs.

Источник

telegram.User¶

class telegram. User ( id: int, first_name: str, is_bot: bool, last_name: str = None, username: str = None, language_code: str = None, can_join_groups: bool = None, can_read_all_group_messages: bool = None, supports_inline_queries: bool = None, bot: Bot = None, **kwargs ) ¶

This object represents a Telegram user or bot.

Objects of this class are comparable in terms of equality. Two objects of this class are considered equal, if their id is equal.

Unique identifier for this user or bot.

True , if this user is a bot.

Optional. User’s or bot’s last name.

Optional. User’s or bot’s username.

Type: str

language_code ¶

Optional. IETF language tag of the user’s language.

Type: str

can_join_groups ¶

Optional. True , if the bot can be invited to groups. Returned only in telegram.Bot.get_me requests.

Type: str

can_read_all_group_messages ¶

Optional. True , if privacy mode is disabled for the bot. Returned only in telegram.Bot.get_me requests.

Type: str

supports_inline_queries ¶

Optional. True , if the bot supports inline queries. Returned only in telegram.Bot.get_me requests.

Optional. The Bot to use for instance methods.

  • id ( int ) – Unique identifier for this user or bot.
  • is_bot ( bool ) – True , if this user is a bot.
  • first_name ( str ) – User’s or bot’s first name.
  • last_name ( str , optional) – User’s or bot’s last name.
  • username ( str , optional) – User’s or bot’s username.
  • language_code ( str , optional) – IETF language tag of the user’s language.
  • can_join_groups ( str , optional) – True , if the bot can be invited to groups. Returned only in telegram.Bot.get_me requests.
  • can_read_all_group_messages ( str , optional) – True , if privacy mode is disabled for the bot. Returned only in telegram.Bot.get_me requests.
  • supports_inline_queries ( str , optional) – True , if the bot supports inline queries. Returned only in telegram.Bot.get_me requests.
  • bot ( telegram.Bot , optional) – The Bot to use for instance methods.

Convenience property. The user’s first_name , followed by (if available) last_name .

Type: str

get_profile_photos ( *args, **kwargs ) → UserProfilePhotos¶

bot.get_user_profile_photos(update.effective_user.id, *args, **kwargs) 

Convenience property. If username is available, returns a t.me link of the user.

Type: str

mention_html ( name: str = None ) → str¶

Parameters: name ( str ) – The name used as a link for the user. Defaults to full_name .
Returns: The inline mention for the user as HTML.
Return type: str

mention_markdown ( name: str = None ) → str¶

telegram.ParseMode.MARKDOWN is is a legacy mode, retained by Telegram for backward compatibility. You should use mention_markdown_v2() instead.

Parameters: name ( str ) – The name used as a link for the user. Defaults to full_name .
Returns: The inline mention for the user as markdown (version 1).
Return type: str

mention_markdown_v2 ( name: str = None ) → str¶

Parameters: name ( str ) – The name used as a link for the user. Defaults to full_name .
Returns: The inline mention for the user as markdown (version 2).
Return type: str

name ¶

Convenience property. If available, returns the user’s username prefixed with “@”. If username is not available, returns full_name .

Type: str

send_action ( *args, **kwargs ) → bool¶

send_animation ( *args, **kwargs ) → Message¶

bot.send_animation(update.effective_user.id, *args, **kwargs) 
Returns: On success, instance representing the message posted.
Return type: telegram.Message

send_audio ( *args, **kwargs ) → Message¶

bot.send_audio(update.effective_user.id, *args, **kwargs) 
Returns: On success, instance representing the message posted.
Return type: telegram.Message

send_chat_action ( *args, **kwargs ) → bool¶

bot.send_chat_action(update.effective_user.id, *args, **kwargs) 
bot.send_contact(update.effective_user.id, *args, **kwargs) 
Returns: On success, instance representing the message posted.
Return type: telegram.Message

send_dice ( *args, **kwargs ) → Message¶

bot.send_dice(update.effective_user.id, *args, **kwargs) 
Returns: On success, instance representing the message posted.
Return type: telegram.Message

send_document ( *args, **kwargs ) → Message¶

bot.send_document(update.effective_user.id, *args, **kwargs) 
Returns: On success, instance representing the message posted.
Return type: telegram.Message

send_game ( *args, **kwargs ) → Message¶

bot.send_game(update.effective_user.id, *args, **kwargs) 
Returns: On success, instance representing the message posted.
Return type: telegram.Message

send_invoice ( *args, **kwargs ) → Message¶

bot.send_invoice(update.effective_user.id, *args, **kwargs) 
Returns: On success, instance representing the message posted.
Return type: telegram.Message

send_location ( *args, **kwargs ) → Message¶

bot.send_location(update.effective_user.id, *args, **kwargs) 
Returns: On success, instance representing the message posted.
Return type: telegram.Message

send_media_group ( *args, **kwargs ) → List[Message]¶

bot.send_media_group(update.effective_user.id, *args, **kwargs) 
Returns: ] On success, instance representing the message posted.
Return type: List[ telegram.Message

send_message ( *args, **kwargs ) → Message¶

bot.send_message(update.effective_user.id, *args, **kwargs) 
Returns: On success, instance representing the message posted.
Return type: telegram.Message

send_photo ( *args, **kwargs ) → Message¶

bot.send_photo(update.effective_user.id, *args, **kwargs) 
Returns: On success, instance representing the message posted.
Return type: telegram.Message

send_poll ( *args, **kwargs ) → Message¶

bot.send_poll(update.effective_user.id, *args, **kwargs) 
Returns: On success, instance representing the message posted.
Return type: telegram.Message

send_sticker ( *args, **kwargs ) → Message¶

bot.send_sticker(update.effective_user.id, *args, **kwargs) 
Returns: On success, instance representing the message posted.
Return type: telegram.Message

send_venue ( *args, **kwargs ) → Message¶

bot.send_venue(update.effective_user.id, *args, **kwargs) 
Returns: On success, instance representing the message posted.
Return type: telegram.Message

send_video ( *args, **kwargs ) → Message¶

bot.send_video(update.effective_user.id, *args, **kwargs) 
Returns: On success, instance representing the message posted.
Return type: telegram.Message

send_video_note ( *args, **kwargs ) → Message¶

bot.send_video_note(update.effective_user.id, *args, **kwargs) 
Returns: On success, instance representing the message posted.
Return type: telegram.Message

send_voice ( *args, **kwargs ) → Message¶

bot.send_voice(update.effective_user.id, *args, **kwargs) 

© Copyright 2015-2020, Leandro Toledo Revision bf68942c .

Источник

Оцените статью