Telegram Base#

This module implements a base interface for interactions with the Telegram API.

class _AbstractTelegramInterface(token, attachments_directory=None)[source]#

Bases: MessengerInterfaceWithAttachments

Messenger interface mixin for Telegram API usage.

supported_request_attachment_types: set[Type[Attachment]] = {<class 'chatsky.core.message.Sticker'>, <class 'chatsky.core.message.Contact'>, <class 'chatsky.core.message.Video'>, <class 'chatsky.core.message.Audio'>, <class 'chatsky.core.message.Document'>, <class 'chatsky.core.message.Image'>, <class 'chatsky.core.message.Location'>, <class 'chatsky.core.message.VideoMessage'>, <class 'chatsky.core.message.Animation'>, <class 'chatsky.core.message.Invoice'>, <class 'chatsky.core.message.VoiceMessage'>, <class 'chatsky.core.message.Poll'>}#

Types of attachment that this messenger interface can receive. Attachments not in this list will be neglected.

supported_response_attachment_types: set[Type[Attachment]] = {<class 'chatsky.core.message.Sticker'>, <class 'chatsky.core.message.Contact'>, <class 'chatsky.core.message.Video'>, <class 'chatsky.core.message.Audio'>, <class 'chatsky.core.message.Document'>, <class 'chatsky.core.message.Image'>, <class 'chatsky.core.message.Location'>, <class 'chatsky.core.message.VideoMessage'>, <class 'chatsky.core.message.Animation'>, <class 'chatsky.core.message.VoiceMessage'>, <class 'chatsky.core.message.MediaGroup'>, <class 'chatsky.core.message.Poll'>}#

Types of attachment that this messenger interface can send. Attachments not in this list will be neglected.

async get_attachment_bytes(source)[source]#

Get attachment bytes from file source.

E.g. if a file attachment consists of a URL of the file uploaded to the messenger servers, this method is the right place to call the messenger API for the file downloading.

Parameters:

source (str) – Identifying string for the file.

Return type:

bytes

Returns:

The attachment bytes.

extract_message_from_telegram(update)[source]#

Convert Telegram update to Chatsky message. Extract text and supported attachments.

Parameters:

update (Message) – Telegram update object.

Return type:

Message

Returns:

Chatsky message object.

async cast_message_to_telegram_and_send(bot, chat_id, message)[source]#

Send Chatsky message to Telegram. Sometimes, if several attachments included into message can not be sent as one update, several Telegram updates will be produced. Sometimes, if no text and none of the supported attachments are included, nothing will happen.

Parameters:
  • bot (ExtBot) – Telegram bot, that is used for connection to Telegram API.

  • chat_id (int) – Telegram dialog ID that the message will be sent to.

  • message (Message) – Chatsky message that will be processed into Telegram updates.

Return type:

None

async _on_event(update, _, create_message)[source]#

Process Telegram update, run pipeline and send response to Telegram.

Parameters:
  • update (Update) – Telegram update that will be processed.

  • create_message (Callable[[Update], Message]) – function that converts Telegram update to Chatsky message.

Return type:

None

async on_message(update, _)[source]#

Process normal Telegram update, extracting Chatsky message from it using extract_message_from_telegram().

Parameters:

update (Update) – Telegram update that will be processed.

Return type:

None

async on_callback(update, _)[source]#

Process Telegram callback update, creating empty Chatsky message with only one callback query attachment from callback_query.data field.

Parameters:

update (Update) – Telegram update that will be processed.

Return type:

None

async connect(pipeline_runner, *args, **kwargs)[source]#

Method invoked when message interface is instantiated and connection is established. May be used for sending an introduction message or displaying general bot information.

Parameters:

pipeline_runner (PipelineRunnerFunction) – A function that should process user request and return context; usually it’s a _run_pipeline() function.