API Reference¶
Client¶
-
class
telegrampy.Client(token: str)¶ Represents a client connection to Telegram.
- Parameters
token (
str) – The API token.
-
add_listener(func, name: str = None)¶ Registers a function as a listener.
- Parameters
func – The function to register.
name (Optional[
str]) – The name of the event to register the function as.
-
event(func)¶ Turns a function into an event handler.
- Parameters
func – The function to make an event handler.
-
await
get_chat(chat_id: int)¶ 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.
-
listen(name=None)¶ A decorator that registers a function as a listener.
- Parameters
name (Optional[
str]) – The name of the event to register the function as.
-
remove_listener(func)¶ Removes a listener.
- Parameters
func – The function that is registered as a listener.
-
run()¶ Runs the bot
Events¶
-
telegrampy.on_message(message: telegrampy.Message)¶ Called when a message is sent.
-
telegrampy.on_edit(before: telegrampy.Message, after: telegrampy.Message)¶ Called when a message is edited.
-
telegrampy.on_poll(poll: telegrampy.Poll)¶ Called when poll is created or updated.
-
telegrampy.on_error(error)¶ Called when an error occurs.
Utilities¶
-
telegrampy.utils.escape_markdown(text: str, *, version: int = 2)¶ Tool that escapes markdown from a given string.
- Parameters
- Returns
The escaped text.
- Return type
- Raises
ValueError – An unsupported version was provided.
Telegram Models¶
Message¶
-
class
telegrampy.Message(http, data: dict)¶ Represents a message in Telegram.
-
x == y Checks if two messages are equal.
-
x != y Checks if two messages are not equal.
-
created_at¶ The time the message was created.
- Type
-
edited_at¶ The time the message was edited.
- Type
Optional[
datetime.datetime]
-
chat¶ The chat the message is in.
- Type
The author of the message.
- Type
-
await
delete()¶ This function is a coroutine.
Deletes the message.
- Raises
telegrampy.HTTPException – Deleting the message failed.
-
await
forward(destination)¶ This function is a coroutine.
Forwards the message to a destination.
- Parameters
destination (:class:telegrampy.Chat`) – The chat forward the message to.
- Returns
The message sent.
- Return type
- Raises
telegrampy.HTTPException – Forwarding the message failed.
-
await
reply(content: str, parse_mode: str = None)¶ This function is a coroutine.
Replys to the message.
- Parameters
- Returns
The message sent.
- Return type
- Raises
telegrampy.HTTPException – Sending the message failed.
-
User¶
-
class
telegrampy.User(http, data)¶ 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.
-
Chat¶
-
class
telegrampy.Chat(http, data: dict)¶ 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.
-
fetch_message(message_id: int)¶ Fetches a message from the cache.
- Parameters
message_id (
int) – The ID of the message to fetch.- Returns
The message fetched.
- Return type
chat.Message
-
await
get_member(user_id: int)¶ 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
send(content: str = None, file: telegrampy.file.File = None, parse_mode: str = None)¶ This function is a coroutine.
Sends a message to the chat.
- Parameters
- Returns
The message sent.
- Return type
- Raises
errors.HTTPException – Sending the message failed.
-
await
send_action(action: str)¶ This function is a coroutine.
Sends an action to the chat.
- Parameters
action (
str) – The action to send.- Raises
telegrampy.HTTPException – Sending the action failed.
-
await
send_poll(question: str, options: list)¶ This function is a coroutine.
Sends a poll to the chat.
- Parameters
- Returns
The poll sent.
- Return type
- Raises
telegrampy.HTTPException – Sending the poll failed.
-
Files¶
-
class
telegrampy.Document(file: _io.BytesIO, filename: str = None)¶ A document to send.
-
file¶ The document.
- Type
- Parameters
file (
io.BytesIO) – The document.filename (
str) – The filename of the document.
-
-
class
telegrampy.Photo(file: _io.BytesIO, filename: str = None, caption: str = None)¶ A photo to send.
-
file¶ The photo.
- Type
- Parameters
file (
io.BytesIO) – The photo.filename (
str) – The filename of the photo.caption (
str) – The caption of the photo.
-
Exceptions¶
-
class
telegrampy.TelegramException¶ Base exception for all errors.
-
class
telegrampy.HTTPException(response, message='')¶ Raised when an HTTP request fails.
-
response¶ The response for the request that failed.
-
-
class
telegrampy.InvalidToken(response, message='')¶ Raised when a token is invalid.
-
class
telegrampy.Forbidden(response, message='')¶ Raised when something is forbidden.
-
class
telegrampy.Conflict(response, message='')¶ Raised when another instance of the bot is running.