Bot class¶
-
class
aiotelebot.
Bot
(token, base_path='https://api.telegram.org/{prefix}bot{token}', client_name='TelegramBot', client_plugins=None, updates_timeout=100, spec=None, logger=None, loop=None)[source]¶ -
answer_callback_query
(request)[source]¶ Use this method to send answers to callback queries sent from inline keyboards. The answer will be displayed to the user as a notification at the top of the chat screen or as an alert. On success, True is returned.
Parameters: request ( AnswerCallbackQueryRequest
) – Request modelIt is possible to use keyword parameters to build an object
AnswerCallbackQueryRequest
for parameterrequest
.Return type: bool
-
answer_inline_query
(request)[source]¶ Use this method to send answers to an inline query. On success, True is returned.
No more than 50 results per query are allowed.
Parameters: request ( AnswerInlineQueryRequest
) – Request modelIt is possible to use keyword parameters to build an object
AnswerInlineQueryRequest
for parameterrequest
.Return type: bool
-
download_file
(file_path)[source]¶ Download file by file path. File path is retrieve using
get_file()
method.It returns ClientResponse object. You could use read method in order to get file data.
Use this method to edit captions of messages sent by the bot or via the bot (for inline bots).
On success, if edited message is sent by the bot, the edited
Message
is returned, otherwise True is returned.Parameters: request ( EditMessageCaptionRequest
) – Request modelIt is possible to use keyword parameters to build an object
EditMessageCaptionRequest
for parameterrequest
.Return type: Union
[bool
,Message
]
-
edit_message_reply_markup
(request)[source]¶ Use this method to edit only the reply markup of messages sent by the bot or via the bot (for inline bots). On success, if edited message is sent by the bot, the edited
Message
is returned, otherwise True is returned.Parameters: request ( EditMessageReplyMarkupRequest
) – Request modelIt is possible to use keyword parameters to build an object
EditMessageReplyMarkupRequest
for parameterrequest
.Return type: Union
[bool
,Message
]
-
edit_message_text
(request)[source]¶ Use this method to edit text messages sent by the bot or via the bot (for inline bots). On success, if edited message is sent by the bot, the edited
Message
is returned, otherwise True is returned.Parameters: request ( EditMessageTextRequest
) – Request modelIt is possible to use keyword parameters to build an object
EditMessageTextRequest
for parameterrequest
.Return type: Union
[bool
,Message
]
-
get_chat
(request)[source]¶ Use this method to get up to date information about the chat (current name of the user for one-on-one conversations, current username of a user, group or channel, etc.). Returns a
Chat
object on success.Parameters: request ( GetChatRequest
) – Request modelIt is possible to use keyword parameters to build an object
GetChatRequest
for parameterrequest
.Return type: Chat
-
get_chat_administrators
(request)[source]¶ Use this method to get a list of administrators in a chat. On success, returns an List of
ChatMember
objects that contains information about all chat administrators except other bots. If the chat is a group or a supergroup and no administrators were appointed, only the creator will be returned.Parameters: request ( GetChatAdministratorsRequest
) – Request modelIt is possible to use keyword parameters to build an object
GetChatAdministratorsRequest
for parameterrequest
.Return type: List
[ChatMember
]
-
get_chat_member
(request)[source]¶ Use this method to get information about a member of a chat. Returns a
ChatMember
object on success.Parameters: request ( GetChatMemberRequest
) – Request modelIt is possible to use keyword parameters to build an object
GetChatMemberRequest
for parameterrequest
.Return type: ChatMember
-
get_chat_members_count
(request)[source]¶ Use this method to get the number of members in a chat. Returns
int
on success.Parameters: request ( GetChatCountRequest
) – Request modelIt is possible to use keyword parameters to build an object
GetChatCountRequest
for parameterrequest
.Return type: int
-
get_file
(request)[source]¶ Use this method to get basic info about a file and prepare it for downloading. For the moment, bots can download files of up to 20MB in size. On success, a File object is returned. The file can then be downloaded via
download_file()
, where<file_path>
is taken from the response. It is guaranteed that the link will be valid for at least 1 hour. When the link expires, a new one can be requested by callingget_file()
again.Parameters: request ( GetFileRequest
) – Request modelIt is possible to use keyword parameters to build an object
GetFileRequest
for parameterrequest
.Return type: File
-
get_me
()[source]¶ A simple method for testing your bot’s auth token. Requires no parameters. Returns basic information about the bot in form of a
User
object.Return type: User
Returns: Bot metainfo
-
get_updates
(query=None)[source]¶ Use this method to receive incoming updates using long polling. An Array of
Update
objects is returned.Parameters: query ( Optional
[GetUpdatesRequest
]) – Custom get updates requestReturn type: List
[Update
]Returns: List of updates It is possible to use keyword parameters to build an object
GetUpdatesRequest
for parameterquery
.
-
get_user_profile_photos
(request)[source]¶ Use this method to get a list of profile pictures for a user.
Parameters: request ( GetUserProfilePhotoRequest
) – Request modelIt is possible to use keyword parameters to build an object
GetUserProfilePhotoRequest
for parameterrequest
.Return type: UserProfilePhotos
-
kick_chat_member
(request)[source]¶ Use this method to kick a user from a group or a supergroup. In the case of supergroups, the user will not be able to return to the group on their own using invite links, etc., unless unbanned first. The bot must be an administrator in the group for this to work. Returns True on success.
Parameters: request ( KickChatMemberRequest
) – Request modelIt is possible to use keyword parameters to build an object
KickChatMemberRequest
for parameterrequest
.Return type: bool
-
leave_chat
(request)[source]¶ Use this method for your bot to leave a group, supergroup or channel. Returns True on success.
Parameters: request ( LeaveChatRequest
) – Request modelIt is possible to use keyword parameters to build an object
LeaveChatRequest
for parameterrequest
.Return type: bool
-
process_callback_query
(callback_query)[source]¶ Parameters: callback_query (telebot.messages.CallbackQuery) – Returns:
-
process_chosen_inline_result
(chosen_inline_result)[source]¶ Parameters: chosen_inline_result (telebot.messages.ChosenInlineResult) – Returns:
-
process_inline_query
(inline_query)[source]¶ Parameters: inline_query (telebot.messages.InlineQuery) – Returns:
-
process_message
(message)[source]¶ Process and route messages received by bot.
Parameters: message ( Message
) – Message to process by bot.
-
process_update
(update)[source]¶ Process a new update message. It will be processed by all registered update processors and by all specific message processors (message, command, chosen inline result or callback query).
Parameters: update ( Update
) – Update message
-
register_message_processor
(func)[source]¶ Register a function in order to process messages.
It could be used as decorator:
@bot.register_message_processor def new_message_processor(message: Message): do_some_thing(message)
Parameters: func ( Callable
[[Message
],Any
]) – Message processorReturn type: Callable
[[Message
],Any
]Returns: Function registered
-
register_update_processor
(func)[source]¶ Register a function in order to process any update data. If function returns True update message will be dropped. Otherwise update message will processed by other processors.
It could be used as decorator:
@bot.register_update_processor def new_update_processor(update: Update): do_some_thing(update) return True
Parameters: func ( Callable
[[Update
],Optional
[bool
]]) – Update processorReturn type: Callable
[[Update
],Optional
[bool
]]Returns: Function registered
-
send_audio
(request)[source]¶ Use this method to send audio files, if you want Telegram clients to display them in the music player. Your audio must be in the .mp3 format. On success, the sent
Message
is returned. Bots can currently send audio files of up to 50 MB in size, this limit may be changed in the future.Note
For sending voice messages, use the
Bot.send_voice()
method instead.Parameters: request ( SendAudioRequest
) – Request modelIt is possible to use keyword parameters to build an object
SendAudioRequest
for parameterrequest
.Return type: Message
-
send_chat_action
(request)[source]¶ Use this method when you need to tell the user that something is happening on the bot’s side. The status is set for 5 seconds or less (when a message arrives from your bot, Telegram clients clear its typing status).
Note
The ImageBot needs some time to process a request and upload the image. Instead of sending a text message along the lines of “Retrieving image, please wait…”, the bot may use
send_chat_action()
with action = upload_photo. The user will see a “sending photo” status for the bot.Parameters: request ( SendChatActionRequest
) – Request modelIt is possible to use keyword parameters to build an object
SendChatActionRequest
for parameterrequest
.Return type: bool
-
send_contact
(request)[source]¶ Use this method to send phone contacts. On success, the sent
Message
is returned.Parameters: request ( SendContactRequest
) – Request modelIt is possible to use keyword parameters to build an object
SendContactRequest
for parameterrequest
.Return type: Message
-
send_document
(request)[source]¶ Use this method to send general files. On success, the sent
Message
is returned. Bots can currently send files of any type of up to 50 MB in size, this limit may be changed in the future.Parameters: request ( SendDocumentRequest
) – Request modelIt is possible to use keyword parameters to build an object
SendDocumentRequest
for parameterrequest
.Return type: Message
-
send_location
(request)[source]¶ Use this method to send point on the map. On success, the sent
Message
is returned.Parameters: request ( SendLocationRequest
) – Request modelIt is possible to use keyword parameters to build an object
SendLocationRequest
for parameterrequest
.Return type: Message
-
send_message
(request)[source]¶ Use this method to send text messages. On success, the sent
Message
is returned.Parameters: request ( SendMessageRequest
) – Request modelIt is possible to use keyword parameters to build an object
SendMessageRequest
for parameterrequest
.Return type: Message
-
send_photo
(request)[source]¶ Use this method to send photos. On success, the sent
Message
is returned.Parameters: request ( SendPhotoRequest
) – Request modelIt is possible to use keyword parameters to build an object
SendPhotoRequest
for parameterrequest
.Return type: Message
-
send_sticker
(request)[source]¶ Use this method to send .webp stickers. On success, the sent
Message
is returned.It is possible to use keyword parameters to build an object
SendStickerRequest
for parameterrequest
.Return type: Message
-
send_venue
(request)[source]¶ Use this method to send information about a venue. On success, the sent
Message
is returned.Parameters: request ( SendVenueRequest
) – Request modelIt is possible to use keyword parameters to build an object
SendVenueRequest
for parameterrequest
.Return type: Message
-
send_video
(request)[source]¶ Use this method to send video files, Telegram clients support mp4 videos (other formats may be sent as Document). On success, the sent
Message
is returned. Bots can currently send video files of up to 50 MB in size, this limit may be changed in the future.Parameters: request ( SendVideoRequest
) – Request modelIt is possible to use keyword parameters to build an object
SendVideoRequest
for parameterrequest
.Return type: Message
-
send_voice
(request)[source]¶ Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message. For this to work, your audio must be in an .ogg file encoded with OPUS (other formats may be sent as Audio or Document). On success, the sent
Message
is returned. Bots can currently send voice messages of up to 50 MB in size, this limit may be changed in the future.Parameters: request ( SendVoiceRequest
) – Request modelIt is possible to use keyword parameters to build an object
SendVoiceRequest
for parameterrequest
.Return type: Message
-
set_webhook
(request)[source]¶ Use this method to specify a url and receive incoming updates via an outgoing webhook. Whenever there is an update for the bot, we will send an HTTPS POST request to the specified url, containing a JSON-serialized Update. In case of an unsuccessful request, we will give up after a reasonable amount of attempts.
If you’d like to make sure that the Webhook request comes from Telegram, we recommend using a secret path in the URL, e.g.
https://www.example.com/<token>
. Since nobody else knows your bot‘s token, you can be pretty sure it’s us.Note
- You will not be able to receive updates using getUpdates for as long as an outgoing webhook is set up.
- To use a self-signed certificate, you need to upload your public key certificate using certificate parameter.
- Ports currently supported for Webhooks: 443, 80, 88, 8443.
Parameters: request ( SetWebhookRequest
) – Request modelIt is possible to use keyword parameters to build an object
SetWebhookRequest
for parameterrequest
.Return type: bool
-
unban_chat_member
(request)[source]¶ Use this method to unban a previously kicked user in a supergroup. The user will not return to the group automatically, but will be able to join via link, etc. The bot must be an administrator in the group for this to work. Returns True on success.
Parameters: request ( UnbanChatMemberRequest
) – Request modelIt is possible to use keyword parameters to build an object
UnbanChatMemberRequest
for parameterrequest
.Return type: bool
-
-
exception
aiotelebot.
TelegramError
(msg, code)[source]¶ Telegram error exception.
-
code
¶ It contains Telegram error code.
-
msg
¶ It contains Telegram error description.
-
-
aiotelebot.
check_result
(func=None, message_cls=<class 'aiotelebot.messages.Message'>)[source]¶ Decorator to process Telegram responses. It raise
TelegramError
exception when result is not successful. Otherwise it processResponse
message using message_cls parameter as factory.Parameters: Return type: Returns:
-
aiotelebot.
list_of
(message_cls)[source]¶ Automatic result message processor for lists.
Parameters: message_cls – Items class Returns: callable
-
aiotelebot.
message_or_true
(msg)[source]¶ Result factory for editing requests.
Return type: Union
[bool
,Message
]Returns: True
orMessage