API Reference¶
Client¶
- class telegrampy.Client(token: str, *, loop: AbstractEventLoop | None = None, timeout: int = 10, api_url: str = 'https://api.telegram.org', is_local: bool = False)¶
A client that polls updates and make requests to Telegram.
- Parameters:
token (
str) – The bot token used to authenticate the account.loop (
asyncio.AbstractEventLoop| None) – The event loop associated with the client. Usesasyncio.get_event_loop()if left unspecified.timeout (
int) – The long polling timeout in seconds. Defaults to10.api_url (
str) – The URL of a selfhosted API server. Otherwise, Telegram’s servers will be used.api_is_local (
bool) – Whether the API server is in local mode.
- loop¶
The event loop that the bot is running on.
- add_inline_keyboard(message_id: int, keyboard: InlineKeyboard) None¶
Binds
InlineKeyboardto a specific message on the client, to listen for updates. This is intended to be used when you want the inline keyboard to last longer than the lifecycle of the program.
- add_listener(func: CoroFunc, name: str | None = None) None¶
Registers a function as a listener.
- Parameters:
func – The function to register.
name (
str| None) – The name of the event to register the function as.
- event(func: CoroFunc) CoroFunc¶
Turns a function into an event handler.
- Parameters:
func – The function to make an event handler.
- await get_chat(chat_id: int) Chat¶
This function is a coroutine.
Fetches a chat by ID.
- Parameters:
chat_id (
int) – The ID of the chat to fetch.- Returns:
The chat that was fetched.
- Return type:
- Raises:
telegrampy.HTTPException – Fetching the chat failed.
- await get_me() ClientUser¶
This function is a coroutine.
Fetches the authenticated bot account.
- Returns:
The user that was fetched.
- Return type:
- Raises:
telegrampy.HTTPException – Fetching the bot user failed.
- get_partial_chat(chat_id) PartialChat¶
Returns a partial chat for the given ID, without fetching anything from Telegram.
This is useful for interacting with chat that you have an ID for, without making extra API calls.
- Returns:
The partial chat that can be interacted with.
- Return type:
- listen(name: str | None = None) Callable[[CoroFunc], CoroFunc]¶
A decorator that registers a function as a listener.
- Parameters:
name (
str| None) – The name of the event to register the function as.
- remove_listener(func: CoroFunc) None¶
Removes a listener.
- Parameters:
func – The function that is registered as a listener.
- await set_description(description: str | None, *, language_code: str | None = None, short: bool = False) None¶
This function is a coroutine.
Sets the description that is shown for the bot.
- Parameters:
description (
str) – The new description. Maxmium of 512 characters for full description and 120 for short description.language_code (
str) – The two-letter ISO 639-1 language code for this description.short (
str) – Whether to set the short or full description. The full description appears on empty chats with the bot, while the long decription appears on the profile page.
- await set_name(name: str | None, *, language_code: str | None = None) None¶
This function is a coroutine.
Sets the description that is shown for the bot.
- Parameters:
name (
str) – The display name of the bot, no longer than 64 chatacters.
- await setup_hook() None¶
This function is a coroutine.
Called before the bot begins polling updates. This is meant to be overrided by subclasses that require asynchronous setup.
- await start() None¶
This function is a coroutine.
Starts the bot.
- Raises:
RuntimeError – This instance of the bot is already running.
- await stop() None¶
This function is a coroutine.
Stops the bot.
- Raises:
RuntimeError – This instance of the bot is not running.
Events¶
- telegrampy.on_message(message: telegrampy.Message)¶
Called when a message is sent.
- telegrampy.on_message_edit(message: telegrampy.Message)¶
Called when a message is edited.
- telegrampy.on_post(message: telegrampy.Message)¶
Called when a channel post is sent.
- telegrampy.on_post_edit(message: telegrampy.Message)¶
Called when a channel post is edited.
- telegrampy.on_callback_query(query: telegrampy.CallbackQuery)¶
Called when a callback query is received from an inline keyboard button. Instead of trying to handle callbacks yourself, consider using
InlineKeyboard, which handles this for you.
- telegrampy.on_member_update(member_update: telegrampy.MemberUpdated)¶
Called when a chat member is updated.
- telegrampy.on_poll(poll: telegrampy.Poll)¶
Called when a poll is sent.
- telegrampy.on_poll_answer(answer: telegrampy.PollAnswer)¶
Called when someone answers a non-anonymous poll.
- telegrampy.on_error(error)¶
Called when an error occurs within an event handler.
Utilities¶
- telegrampy.utils.escape_markdown(text: str, *, version: Version = 2) str¶
Tool that escapes markdown from a given string.
- Parameters:
- Returns:
The escaped text.
- Return type:
- Raises:
ValueError – An unsupported version was provided.
Telegram Models¶
Base¶
- class telegrampy.Messageable¶
Represents any Telegram resource that messages can be sent to.
This is implemented by the following subclasses:
- action(action: str) MessageableAction¶
Returns a context manager that sends a chat action to the destination until completed or for up to 5 seconds if called with
await.Usage:
async with chat.action("typing"): # do some computing here await chat.send("Done!")
Usage:
await chat.action("typing")
- Parameters:
action (
str) – The action to send.
- await send(content: str, *, parse_mode: ParseMode | None = None, reply_markup: ReplyMarkup | None = None) Message¶
This function is a coroutine.
Sends a message to the destination.
- Parameters:
content (
str) – The content of the message to send.parse_mode (
str| None) – The parse mode of the message to send.reply_markup (
InlineKeyboard|ReplyKeyboard|ReplyKeyboardRemove|ForceReply| None) – The reply markup interface to send with the message.
- Returns:
The message sent.
- Return type:
- Raises:
errors.HTTPException – Sending the message failed.
- await send_document(document: io.BytesIO | str, *, filename: str | None = None, caption: str | None = None, parse_mode: str | None = None, reply_markup: ReplyMarkup | None = None) Message¶
This function is a coroutine.
Sends a document to the destination.
- Parameters:
document (
io.BytesIO|str) – The document to send. Either a file or the path to one.filename (
str| None) – The filename of the document.caption (
str| None) – The document’s caption.parse_mode (
str| None) – The parse mode for the caption.reply_markup (
InlineKeyboard|ReplyKeyboard|ReplyKeyboardRemove|ForceReply| None) – The reply markup interface to send with the message.
- Raises:
errors.HTTPException – Sending the document failed.
- await send_photo(photo: io.BytesIO | str, *, filename: str | None = None, caption: str | None = None, parse_mode: str | None = None, reply_markup: ReplyMarkup | None = None) Message¶
This function is a coroutine.
Sends a photo to the destination.
- Parameters:
photo (
io.BytesIO|str) – The photo to send. Either a file or the path to one.filename (
str| None) – The filename of the photo.caption (
str| None) – The caption for the photo.parse_mode (
str| None) – The parse mode for the caption.reply_markup (
InlineKeyboard|ReplyKeyboard|ReplyKeyboardRemove|ForceReply| None) – The reply markup interface to send with the message.
- Raises:
errors.HTTPException – Sending the photo failed.
- await send_poll(question: str, options: list[str]) Poll¶
This function is a coroutine.
Sends a poll to the destination.
- Parameters:
question (
str) – The question of the poll.options (list[
str]) – The options in the poll.reply_markup (
InlineKeyboard|ReplyKeyboard|ReplyKeyboardRemove | :class:`ForceReply| None) – The reply markup interface to send with the message.
- Returns:
The poll sent.
- Return type:
- Raises:
telegrampy.HTTPException – Sending the poll failed.
- typing() MessageableAction¶
Returns a context manager that shows a typing indicicator in the destination. This is eqivalent to calling
action("typing")()
Chat¶
- class telegrampy.PartialChat¶
Represents a partial chat that can be interacted with, without containing information.
- x == y
Checks if two partial chats are equal.
- x != y
Checks if two chats partial are not equal.
- action(action: str) MessageableAction¶
Returns a context manager that sends a chat action to the destination until completed or for up to 5 seconds if called with
await.Usage:
async with chat.action("typing"): # do some computing here await chat.send("Done!")
Usage:
await chat.action("typing")
- Parameters:
action (
str) – The action to send.
- await clear_pins() None¶
This function is a coroutine.
Removes all pins from the chat. Must have administrator with proper permissions.
- Raises:
telegrampy.HTTPException – Clearing all pins failed.
- await get_member(user_id: int) Member¶
This function is a coroutine.
Fetches a member in the chat.
- Parameters:
user_id (
int) – The user ID of the member.- Returns:
The member fetched.
- Return type:
- Raises:
telegrampy.HTTPException – Fetching the member failed.
- await get_member_count() int¶
This function is a coroutine.
Fetches the member count of the chat.
- Returns:
The number of members in the chat.
- Return type:
- Raises:
telegrampy.HTTPException – Fetching the member count failed.
- get_partial_message(message_id: int) PartialMessage¶
Returns a partial message for the given ID, without fetching anything from Telegram.
This is useful for interacting with the messages if you only have an ID.
- Returns:
The partial message that can be interacted with.
- Return type:
- await leave() None¶
This function is a coroutine.
Removes the logged in bot from a non-private chat.
- Raises:
telegrampy.HTTPException – Leaving the chat failed.
- await send(content: str, *, parse_mode: ParseMode | None = None, reply_markup: ReplyMarkup | None = None) Message¶
This function is a coroutine.
Sends a message to the destination.
- Parameters:
content (
str) – The content of the message to send.parse_mode (
str| None) – The parse mode of the message to send.reply_markup (
InlineKeyboard|ReplyKeyboard|ReplyKeyboardRemove|ForceReply| None) – The reply markup interface to send with the message.
- Returns:
The message sent.
- Return type:
- Raises:
errors.HTTPException – Sending the message failed.
- await send_document(document: io.BytesIO | str, *, filename: str | None = None, caption: str | None = None, parse_mode: str | None = None, reply_markup: ReplyMarkup | None = None) Message¶
This function is a coroutine.
Sends a document to the destination.
- Parameters:
document (
io.BytesIO|str) – The document to send. Either a file or the path to one.filename (
str| None) – The filename of the document.caption (
str| None) – The document’s caption.parse_mode (
str| None) – The parse mode for the caption.reply_markup (
InlineKeyboard|ReplyKeyboard|ReplyKeyboardRemove|ForceReply| None) – The reply markup interface to send with the message.
- Raises:
errors.HTTPException – Sending the document failed.
- await send_photo(photo: io.BytesIO | str, *, filename: str | None = None, caption: str | None = None, parse_mode: str | None = None, reply_markup: ReplyMarkup | None = None) Message¶
This function is a coroutine.
Sends a photo to the destination.
- Parameters:
photo (
io.BytesIO|str) – The photo to send. Either a file or the path to one.filename (
str| None) – The filename of the photo.caption (
str| None) – The caption for the photo.parse_mode (
str| None) – The parse mode for the caption.reply_markup (
InlineKeyboard|ReplyKeyboard|ReplyKeyboardRemove|ForceReply| None) – The reply markup interface to send with the message.
- Raises:
errors.HTTPException – Sending the photo failed.
- await send_poll(question: str, options: list[str]) Poll¶
This function is a coroutine.
Sends a poll to the destination.
- Parameters:
question (
str) – The question of the poll.options (list[
str]) – The options in the poll.reply_markup (
InlineKeyboard|ReplyKeyboard|ReplyKeyboardRemove | :class:`ForceReply| None) – The reply markup interface to send with the message.
- Returns:
The poll sent.
- Return type:
- Raises:
telegrampy.HTTPException – Sending the poll failed.
- await set_description(description: str | None) None¶
This function is a coroutine.
Changes the description of the chat. This only works in non-private chats with administrator privillages.
- Parameters:
description (
str) – The new description of the chat, up to 255 characters.- Raises:
telegrampy.HTTPException – Changing the description failed.
- await set_photo(photo: BytesIO | str | None) None¶
This function is a coroutine.
Sets the photo for the chat. This does not work in private chats.
- Parameters:
photo (
io.BytesIO|str) – The new profile photo for the chat. PassNoneto clear.
- await set_title(title: str) None¶
This function is a coroutine.
Changes the title of the chat. This only works in non-private chats with administrator privillages.
- Parameters:
title (
str) – The new name of the chat, from 1 to 128 characaters.- Raises:
telegrampy.HTTPException – Changing the title failed.
- typing() MessageableAction¶
Returns a context manager that shows a typing indicicator in the destination. This is eqivalent to calling
action("typing")()
- class telegrampy.Chat¶
Represents a chat in Telegram.
- x == y
Checks if two chats are equal.
- x != y
Checks if two chats are not equal.
- str(x)
Returns the chat’s title.
- action(action: str) MessageableAction¶
Returns a context manager that sends a chat action to the destination until completed or for up to 5 seconds if called with
await.Usage:
async with chat.action("typing"): # do some computing here await chat.send("Done!")
Usage:
await chat.action("typing")
- Parameters:
action (
str) – The action to send.
- await clear_pins() None¶
This function is a coroutine.
Removes all pins from the chat. Must have administrator with proper permissions.
- Raises:
telegrampy.HTTPException – Clearing all pins failed.
- await get_member(user_id: int) Member¶
This function is a coroutine.
Fetches a member in the chat.
- Parameters:
user_id (
int) – The user ID of the member.- Returns:
The member fetched.
- Return type:
- Raises:
telegrampy.HTTPException – Fetching the member failed.
- await get_member_count() int¶
This function is a coroutine.
Fetches the member count of the chat.
- Returns:
The number of members in the chat.
- Return type:
- Raises:
telegrampy.HTTPException – Fetching the member count failed.
- get_partial_message(message_id: int) PartialMessage¶
Returns a partial message for the given ID, without fetching anything from Telegram.
This is useful for interacting with the messages if you only have an ID.
- Returns:
The partial message that can be interacted with.
- Return type:
- await leave() None¶
This function is a coroutine.
Removes the logged in bot from a non-private chat.
- Raises:
telegrampy.HTTPException – Leaving the chat failed.
- await send(content: str, *, parse_mode: ParseMode | None = None, reply_markup: ReplyMarkup | None = None) Message¶
This function is a coroutine.
Sends a message to the destination.
- Parameters:
content (
str) – The content of the message to send.parse_mode (
str| None) – The parse mode of the message to send.reply_markup (
InlineKeyboard|ReplyKeyboard|ReplyKeyboardRemove|ForceReply| None) – The reply markup interface to send with the message.
- Returns:
The message sent.
- Return type:
- Raises:
errors.HTTPException – Sending the message failed.
- await send_document(document: io.BytesIO | str, *, filename: str | None = None, caption: str | None = None, parse_mode: str | None = None, reply_markup: ReplyMarkup | None = None) Message¶
This function is a coroutine.
Sends a document to the destination.
- Parameters:
document (
io.BytesIO|str) – The document to send. Either a file or the path to one.filename (
str| None) – The filename of the document.caption (
str| None) – The document’s caption.parse_mode (
str| None) – The parse mode for the caption.reply_markup (
InlineKeyboard|ReplyKeyboard|ReplyKeyboardRemove|ForceReply| None) – The reply markup interface to send with the message.
- Raises:
errors.HTTPException – Sending the document failed.
- await send_photo(photo: io.BytesIO | str, *, filename: str | None = None, caption: str | None = None, parse_mode: str | None = None, reply_markup: ReplyMarkup | None = None) Message¶
This function is a coroutine.
Sends a photo to the destination.
- Parameters:
photo (
io.BytesIO|str) – The photo to send. Either a file or the path to one.filename (
str| None) – The filename of the photo.caption (
str| None) – The caption for the photo.parse_mode (
str| None) – The parse mode for the caption.reply_markup (
InlineKeyboard|ReplyKeyboard|ReplyKeyboardRemove|ForceReply| None) – The reply markup interface to send with the message.
- Raises:
errors.HTTPException – Sending the photo failed.
- await send_poll(question: str, options: list[str]) Poll¶
This function is a coroutine.
Sends a poll to the destination.
- Parameters:
question (
str) – The question of the poll.options (list[
str]) – The options in the poll.reply_markup (
InlineKeyboard|ReplyKeyboard|ReplyKeyboardRemove | :class:`ForceReply| None) – The reply markup interface to send with the message.
- Returns:
The poll sent.
- Return type:
- Raises:
telegrampy.HTTPException – Sending the poll failed.
- await set_description(description: str | None) None¶
This function is a coroutine.
Changes the description of the chat. This only works in non-private chats with administrator privillages.
- Parameters:
description (
str) – The new description of the chat, up to 255 characters.- Raises:
telegrampy.HTTPException – Changing the description failed.
- await set_photo(photo: BytesIO | str | None) None¶
This function is a coroutine.
Sets the photo for the chat. This does not work in private chats.
- Parameters:
photo (
io.BytesIO|str) – The new profile photo for the chat. PassNoneto clear.
- await set_title(title: str) None¶
This function is a coroutine.
Changes the title of the chat. This only works in non-private chats with administrator privillages.
- Parameters:
title (
str) – The new name of the chat, from 1 to 128 characaters.- Raises:
telegrampy.HTTPException – Changing the title failed.
- typing() MessageableAction¶
Returns a context manager that shows a typing indicicator in the destination. This is eqivalent to calling
action("typing")()
- class telegrampy.ChatInvite¶
Represents an invite to a chat in the form of a link.
- creator¶
The user who created the link
- Type:
- creates_join_request¶
Whether users joining the chat need to be approved by the administrators.
- Type:
- expire_date¶
The time that the link will expire or the time it expired at if it already has.
- Type:
datetime.datetime| None
- member_limit¶
The maxmimum number of users that can be simultaneous chat members, by joining through this link.
- Type:
int| None
- pending_join_request_count¶
The number of pending join requests for users of the link.
- Type:
int| None
- subscription_period¶
The number of seconds the subscription will be active for before the next payment
- Type:
;class:int | None
Message¶
- class telegrampy.PartialMessage(message_id: int, chat: PartialChat)¶
Represents a partial message that can be interacted with, without containing information.
- x == y
Checks if two partial chats are equal.
- x != y
Checks if two chats partial are not equal.
- chat¶
The chat the partial message was sent in.
- await delete() None¶
This function is a coroutine.
Deletes the message.
- Raises:
telegrampy.HTTPException – Deleting the message failed.
- await edit(*, content: str, parse_mode: ParseMode | None = None) Message | None¶
This function is a coroutine.
Edits the message.
- await forward(destination: Chat) Message¶
This function is a coroutine.
Forwards the message to a destination.
- Parameters:
destination (
telegrampy.PartialChat|telegrampy.Chat) – The chat forward the message to.- Returns:
The message sent.
- Return type:
- Raises:
telegrampy.HTTPException – Forwarding the message failed.
- await pin(*, silent: bool = False) None¶
This function is a coroutine.
Pins the message to the chat.
- Parameters:
silent (
bool) – Whether to not notify chat members when a message is sent. Notifications are never sent for channels and private chats.- Raises:
telegrampy.HTTPException – Pinning the message failed.
- await reply(content: str, parse_mode: ParseMode | None = None, reply_markup: ReplyMarkup | None = None) Message¶
This function is a coroutine.
Sends a reply to the message.
- Parameters:
content (
str) – The content of the message to send.parse_mode (
str| None) – The parse mode of the message to send.reply_markup (
InlineKeyboard|ReplyKeyboard|ReplyKeyboardRemove |:class:`ForceReply) – The reply markup interface to send with the message.
- Returns:
The message sent.
- Return type:
- Raises:
telegrampy.HTTPException – Sending the message failed.
- await unpin() None¶
This function is a coroutine.
Unpins the message from the chat.
- Raises:
telegrampy.HTTPException – Unpinning the message failed.
- class telegrampy.Message¶
Represents a message in Telegram.
- x == y
Checks if two messages are equal.
- x != y
Checks if two messages are not equal.
- edited_at¶
The time the message was edited.
- Type:
datetime.datetime| None
- chat¶
The chat the message was sent in.
- Type:
- await delete() None¶
This function is a coroutine.
Deletes the message.
- Raises:
telegrampy.HTTPException – Deleting the message failed.
- await edit(*, content: str, parse_mode: ParseMode | None = None) Message | None¶
This function is a coroutine.
Edits the message.
- await forward(destination: Chat) Message¶
This function is a coroutine.
Forwards the message to a destination.
- Parameters:
destination (
telegrampy.PartialChat|telegrampy.Chat) – The chat forward the message to.- Returns:
The message sent.
- Return type:
- Raises:
telegrampy.HTTPException – Forwarding the message failed.
- await pin(*, silent: bool = False) None¶
This function is a coroutine.
Pins the message to the chat.
- Parameters:
silent (
bool) – Whether to not notify chat members when a message is sent. Notifications are never sent for channels and private chats.- Raises:
telegrampy.HTTPException – Pinning the message failed.
- await reply(content: str, parse_mode: ParseMode | None = None, reply_markup: ReplyMarkup | None = None) Message¶
This function is a coroutine.
Sends a reply to the message.
- Parameters:
content (
str) – The content of the message to send.parse_mode (
str| None) – The parse mode of the message to send.reply_markup (
InlineKeyboard|ReplyKeyboard|ReplyKeyboardRemove |:class:`ForceReply) – The reply markup interface to send with the message.
- Returns:
The message sent.
- Return type:
- Raises:
telegrampy.HTTPException – Sending the message failed.
- await unpin() None¶
This function is a coroutine.
Unpins the message from the chat.
- Raises:
telegrampy.HTTPException – Unpinning the message failed.
- class telegrampy.MessageEntity¶
Represents a message entity.
- user¶
The mentioned user for text_mention.
- Type:
telegrampy.User| None
User¶
- class telegrampy.User¶
Represents a Telegram user.
- x == y
Checks if two users are equal.
- x != y
Checks if two users are not equal.
- str(x)
Returns the user’s name.
Whether the logged in bot is added to this user’s attachment menu.
- Type:
Whether the user is subscribed to Telegram Premium.
- Type:
- class telegrampy.ClientUser¶
Represents your Telegram user.
- x == y
Checks if two users are equal.
- x != y
Checks if two users are not equal.
- str(x)
Returns the user’s name.
Whether the logged in bot is added to this user’s attachment menu.
- Type:
Whether the user is subscribed to Telegram Premium.
- Type:
- can_connect_to_business¶
Whether the logged in bot can be connected to a Telegram business account to receive its messages.
- Type:
- class telegrampy.UserProfilePhotos¶
Represents a collection of profile photos that belong to a user.
Member¶
- class telegrampy.Member¶
Represents a chat member, a superset of
telegrampy.Usercontaining chat-specific details.- chat¶
The chat that the user belongs to.
Whether the logged in bot is added to this user’s attachment menu.
- Type:
Whether the user is subscribed to Telegram Premium.
- Type:
- action(action: str) MessageableAction¶
Returns a context manager that sends a chat action to the destination until completed or for up to 5 seconds if called with
await.Usage:
async with chat.action("typing"): # do some computing here await chat.send("Done!")
Usage:
await chat.action("typing")
- Parameters:
action (
str) – The action to send.
- await get_profile_photos(*, offset: int | None = None, limit: int = 100) UserProfilePhotos¶
Fetches the profile pictures for the user.
- :param
int| None: The sequential number of the first photo to be retrieved. If unspecified, all photos will be retrieved.
- Parameters:
limit (
int) – The maximum number of photos to be retrieved. Defaults to and cannot be more than100.
- :param
- mention(text: str | None = None, parse_mode: ParseMode = 'HTML') str¶
Returns a mention for the user.
- await send(content: str, *, parse_mode: ParseMode | None = None, reply_markup: ReplyMarkup | None = None) Message¶
This function is a coroutine.
Sends a message to the destination.
- Parameters:
content (
str) – The content of the message to send.parse_mode (
str| None) – The parse mode of the message to send.reply_markup (
InlineKeyboard|ReplyKeyboard|ReplyKeyboardRemove|ForceReply| None) – The reply markup interface to send with the message.
- Returns:
The message sent.
- Return type:
- Raises:
errors.HTTPException – Sending the message failed.
- await send_document(document: io.BytesIO | str, *, filename: str | None = None, caption: str | None = None, parse_mode: str | None = None, reply_markup: ReplyMarkup | None = None) Message¶
This function is a coroutine.
Sends a document to the destination.
- Parameters:
document (
io.BytesIO|str) – The document to send. Either a file or the path to one.filename (
str| None) – The filename of the document.caption (
str| None) – The document’s caption.parse_mode (
str| None) – The parse mode for the caption.reply_markup (
InlineKeyboard|ReplyKeyboard|ReplyKeyboardRemove|ForceReply| None) – The reply markup interface to send with the message.
- Raises:
errors.HTTPException – Sending the document failed.
- await send_photo(photo: io.BytesIO | str, *, filename: str | None = None, caption: str | None = None, parse_mode: str | None = None, reply_markup: ReplyMarkup | None = None) Message¶
This function is a coroutine.
Sends a photo to the destination.
- Parameters:
photo (
io.BytesIO|str) – The photo to send. Either a file or the path to one.filename (
str| None) – The filename of the photo.caption (
str| None) – The caption for the photo.parse_mode (
str| None) – The parse mode for the caption.reply_markup (
InlineKeyboard|ReplyKeyboard|ReplyKeyboardRemove|ForceReply| None) – The reply markup interface to send with the message.
- Raises:
errors.HTTPException – Sending the photo failed.
- await send_poll(question: str, options: list[str]) Poll¶
This function is a coroutine.
Sends a poll to the destination.
- Parameters:
question (
str) – The question of the poll.options (list[
str]) – The options in the poll.reply_markup (
InlineKeyboard|ReplyKeyboard|ReplyKeyboardRemove | :class:`ForceReply| None) – The reply markup interface to send with the message.
- Returns:
The poll sent.
- Return type:
- Raises:
telegrampy.HTTPException – Sending the poll failed.
- typing() MessageableAction¶
Returns a context manager that shows a typing indicicator in the destination. This is eqivalent to calling
action("typing")()
- class telegrampy.MemberUpdated¶
Contains information about any change regarding a chat member.
- chat¶
The chat that the update occured in.
- Type:
- author¶
The user who performed the action resulting in a change.
- Type:
- taken_at¶
The time that the action was taken at.
- Type:
- old_member¶
The affected chat member, prior to the change.
- Type:
- new_member¶
The affected chat member, after the change.
- Type:
- invite_link¶
The invite link used to join the chat, if applicable.
- Type:
ChatInviteLink| None
Poll¶
- class telegrampy.Poll¶
A Telegram poll.
- x == y
Checks if two polls are equal.
- x != y
Checks if two polls are not equal.
- str(x)
Returns the poll’s question.
Files¶
- class telegrampy.File¶
Represents basic information about a uploaded file.
- unique_id¶
The globally unique identifier of the file, which is supposed to be permanent. This cannot be used for downloading or resuing the file.
- Type:
- path¶
The relative path of the file on the server. In local mode, this is the absolute path on the filesystem, which may contain sensitive information such as the bot’s token. The path is guaranteed to be valid for at least an hour. It is preferable to use
read()orsave()rather than using this directly.- Type:
str| None
- await read() bytes¶
This function is a coroutine.
Retrieves the contenst of the file.
- Returns:
The content of the file.
- Return type:
- Raises:
HTTPException – Fetching the content of the file failed.
ClientException – The file has an absolute path to the local filesystem or none was provided.
- await refresh() File¶
This function is a coroutine.
Creates a new file with an updated path.
- Return type:
The refreshed file.
- Raises:
HTTPException – Fetching the updated file failed.
- await save(fp: str | BytesIO, *, seek_begin: bool = True) int¶
This function is a coroutine.
Writes the content of the file to the disk or a file-like object.
- Parameters:
fp (
str|io.BytesIO) – The file-like object to write the content to or a filename to use.seek_being (
bool) – Whether to seek the beginning of the bytes-like object passed, after writing to it.
- Returns:
The length of the content written, in bytes.
- Return type:
- Raises:
HTTPException – Fetching the content of the file failed.
RuntimeError – The file has no download path associated with it.
- class telegrampy.PhotoSize¶
Represents a photo of a specific size. Also used for thumbnails of files and stickers.
- file_unique_id¶
The globally unique identifier of the file, which is supposed to be permanent. This cannot be used for downloading or resuing the file.
- Type:
- await get_file() File¶
This function is a coroutine.
Fetches the file associated with the photo, whiches basic information and allows for downloading.
- Returns:
The associated file.
- Return type:
- Raises:
HTTPException – Fetching the associated file failed.
Markup¶
- class telegrampy.InlineKeyboard¶
Represents an inline keyboard that appears as buttons on the message it is sent in.
- add_button(button: InlineKeyboardButton) None¶
Removes a button from the keyboard.
- property buttons: list[InlineKeyboardButton]¶
The list of buttons added to the inline keyboard.
- Type:
list[
InlineKeyboardButton]
- await on_error(query: CallbackQuery, error: Exception, button: InlineKeyboardButton) None¶
This function is a coroutine.
Called when a button inside the inline keyboard experiences an unhandled exception.
Paramaters¶
- query:
CallbackQuery| None The callback query that triggered the button callback.
- error:
Exception| None The exception that was raised.
- button:
InlineKeyboardButton| None The button associated with the callback query.
- query:
- remove_button(button: InlineKeyboardButton) None¶
Adds a button to the keyboard.
- class telegrampy.InlineKeyboardButton(*, text: str, url: str | None = None, data: str | None = None, row: int | None = None)¶
Represents a button that can be added to an
InlineKeyboard.- Parameters:
text (
str) – The label of the button.url (
str| None) – The HTTP or tg:// URL to be opened upon clicking the button.data (
str| None) – A unique piece of data to identify the button in a callback query. Only for buttons without a URL.row (
int] | None) – The row to show the button on inside of the keyboard.
- await callback(query: CallbackQuery) None¶
This function is a coroutine.
Called when the button is clicked. This should be implemented by subclasses.
- Parameters:
query (
CallbackQuery) – The callback query that triggered the button.
- property keyboard: InlineKeyboard | None¶
InlineKeyboard| None The inline keyboard that the button belongs to.
- @telegrampy.inline_keyboard_button(**kwargs: Any) Callable[[InlineKeyboardCallbackType], InlineKeyboardCallbackType]¶
Creates a button inside of an
InlineKeyboardclass, by using the decorated function as the callback.The wrapped function should take three positional parameters: the current instance of the
InlineKeyboard, theCallbackQuerythat triggered the button, and theInlineKeyboardButtonbeing pressed.- Parameters:
kwargs – The keyword arguments to pass into the constructor when added to the
InlineKeyboardinstance.
- class telegrampy.ReplyKeyboard(*, persistent: bool = False, resize: bool = False, one_time: bool = False, input_placeholder: str | None = None, selective: bool = False)¶
Represents a custom keyboard with reply options that is shown to a user.
- Parameters:
persistent (
bool) – Whether the keyboard should always be shown.resize (
bool) – Whether clients should shrink/expand the keyboard to fit the buttons more optimially. IfFalse, the keyboard will always take up the same amount of space as the standard keyboard.one_time (
bool) – Whether to close the keyboard after using it. Users will still be able to upon it up later.placeholder (
bool) – The placeholder to show in the input field when the keyboard is active.selective (
bool) – Whether to target all users in the chat or specific users. If selective, the markup will only be applied to users mentioned in the message and the author of the reply message.
- property buttons: list[ReplyKeyboardButton]¶
The list of buttons added to the reply keyboard.
- Type:
list[
ReplyKeyboardButton]
- class telegrampy.ReplyKeyboardButton(*, text: str, row: int | None = None)¶
Represents a button that can be added to a reply keyboard.
- class telegrampy.ReplyKeyboardRemove(selective: bool = False)¶
Removes a reply keyboard from the chat when sent as a reply markup.
- Parameters:
selective (
bool) – Whether to target all users in the chat or specific users. If selective, the markup will only be applied to users mentioned in the message and the author of the reply message.
- class telegrampy.ForceReply(*, placeholder: str | None = None, selective: bool = False)¶
Populates a reply interface in the chat when sent as a reply markup.
- Parameters:
placeholder (
str| None) – The placeholder that will be shown in the input field when the reply interface is active.selective (
bool) – Whether to target all users in the chat or specific users. If selective, the markup will only be applied to users mentioned in the message and the author of the reply message.
- class telegrampy.CallbackQuery(http: HTTPClient, data: CallbackQueryPayload)¶
A callback query triggered by a button on an inline keyboard.
Paramaters¶
- id:
str The ID of the callback query
- user:
User The user who sent the callback query.
- message:
PartialMessage| None The message containing the button that originated the query, if sent by the bot.
- inline_message_id:
str| None The ID of the message sent by the through in inline mode that originated the query.
- chat_instance:
str Global unique ID for the chat which the message with the button was sent in.
- data:
str| None The data associated with the button that triggered the query.
- game_short_name:
str| None The short name of the game to be returned, serving as a unique ID.
- await answer(*, text: str | None = None, show_alert: bool = False, url: str | None = None, cache_time: int = 0) None¶
This function is a coroutine.
Responds to a callback query. The answer will be displayed to the user as notification at the top of their screen or as an alert.
Paramaters¶
- text:
str The text that will be shown to the user. If not passed, nothing will be shown to the user.
- show_alert:
bool Whether to show an alert instead of a notification at the top of the screen to the user.
- url:
bool Specifies the URL that opens your game for callback games buttons. Otherwise the link must be a t.me link that opens your bot with a specific parameter.
- cache_time:
int The amount of time the callback query may be cached client side.
- text:
- id:
Exceptions¶
- class telegrampy.TelegramException¶
Base exception for errors relating to telegram.py.
- class telegrampy.ClientException¶
Raised when something miscellaneous goes wrong.
- class telegrampy.HTTPException(response: aiohttp.ClientResponse, message: str)¶
Raised when an HTTP request fails.
This inherits from
telegrampy.TelegramException.- response¶
The response for the request that failed.
- Type:
- class telegrampy.BadRequest(response: aiohttp.ClientResponse, message: str)¶
Raised when a bad request is made.
This inherits from
telegrampy.HTTPException.
- class telegrampy.InvalidToken(response: aiohttp.ClientResponse, message: str)¶
Raised when a token is invalid.
This inherits from
telegrampy.HTTPException.
- class telegrampy.Forbidden(response: aiohttp.ClientResponse, message: str)¶
Raised when something is forbidden.
This inherits from
telegrampy.HTTPException.
- class telegrampy.Conflict(response: aiohttp.ClientResponse, message: str)¶
Raised when another instance of the bot is running.
This inherits from
telegrampy.HTTPException.
- class telegrampy.ServerError(response: aiohttp.ClientResponse, message: str)¶
Raised when telegram returns a 500 error.
This inherits from
telegrampy.HTTPException.