Message#
The Message class is a universal data model for representing a message.
It only contains types and properties that are compatible with most messaging services.
- class DataModel(**kwargs)[source]#
Bases:
JSONSerializableExtras
This class is a Pydantic BaseModel that can have any type and number of extras.
- class Attachment(*, chatsky_attachment_type: str, **kwargs)[source]#
Bases:
DataModel
,ABC
Chatsky Message attachment base class. It is capable of serializing and validating all the model fields to JSON.
- chatsky_attachment_type: str#
- class CallbackQuery(*, chatsky_attachment_type: Literal['callback_query'] = 'callback_query', query_string: str, **kwargs)[source]#
Bases:
Attachment
This class is a data model that represents a callback query attachment. It is sent as a response to non-message events, e.g. keyboard UI interactions. It has query string attribute, that represents the response data string.
- query_string: str#
- chatsky_attachment_type: Literal['callback_query']#
- class Location(*, chatsky_attachment_type: Literal['location'] = 'location', longitude: float, latitude: float, **kwargs)[source]#
Bases:
Attachment
This class is a data model that represents a geographical location on the Earth’s surface. It has two attributes, longitude and latitude, both of which are float values. If the absolute difference between the latitude and longitude values of the two locations is less than 0.00004, they are considered equal.
- longitude: float#
- latitude: float#
- chatsky_attachment_type: Literal['location']#
- class Contact(*, chatsky_attachment_type: Literal['contact'] = 'contact', phone_number: str, first_name: str, last_name: str | None, **kwargs)[source]#
Bases:
Attachment
This class is a data model that represents a contact. It includes phone number, and user first and last name.
- phone_number: str#
- first_name: str#
- last_name: Optional[str]#
- chatsky_attachment_type: Literal['contact']#
- class Invoice(*, chatsky_attachment_type: Literal['invoice'] = 'invoice', title: str, description: str, currency: str, amount: int, **kwargs)[source]#
Bases:
Attachment
This class is a data model that represents an invoice. It includes title, description, currency name and amount.
- title: str#
- description: str#
- currency: str#
- amount: int#
- chatsky_attachment_type: Literal['invoice']#
- class PollOption(*, text: str, votes: int = 0, chatsky_attachment_type: Literal['poll_option'] = 'poll_option', **kwargs)[source]#
Bases:
DataModel
This class is a data model that represents a poll option. It includes the option name and votes number.
- text: str#
- votes: int#
- chatsky_attachment_type: Literal['poll_option']#
- class Poll(*, chatsky_attachment_type: Literal['poll'] = 'poll', question: str, options: List[PollOption], **kwargs)[source]#
Bases:
Attachment
This class is a data model that represents a poll. It includes a list of poll options.
- question: str#
- options: List[PollOption]#
- chatsky_attachment_type: Literal['poll']#
- class DataAttachment(*, chatsky_attachment_type: str, source: Url | Path | None = None, use_cache: bool = True, cached_filename: Path | None = None, id: str | None = None, **kwargs)[source]#
Bases:
Attachment
This class represents an attachment that can be either a local file, a URL to a file or a ID of a file on a certain server (such as telegram). This attachment can also be optionally cached for future use.
- source: Optional[Union[HttpUrl, FilePath]]#
Attachment source – either a URL to a file or a local filepath.
- use_cache: bool#
Whether to cache the file (only for URL and ID files). Disable this if you want to always respond with the most up-to-date version of the file.
- cached_filename: Optional[Path]#
This field is used to store a path to cached version of this file (retrieved from id or URL). This field is managed by framework.
- id: Optional[str]#
ID of the file on a file server (e.g. file_id for telegram attachments).
get_attachment_bytes()
is used to retrieve bytes from ID.
- async _cache_attachment(data, directory)[source]#
Cache attachment, save bytes into a file. File has a UUID name based on its self.source or self.id.
- Parameters:
data (
bytes
) – attachment data bytes.directory (
Path
) – cache directory where attachment will be saved.
- Return type:
None
- async get_bytes(from_interface)[source]#
Retrieve attachment bytes. If the attachment is represented by URL or saved in a file, it will be downloaded or read automatically. If cache use is allowed and the attachment is cached, cached file will be used. Otherwise, a
get_attachment_bytes()
will be used for receiving attachment bytes via ID.If cache use is allowed and the attachment is a URL or an ID, bytes will be cached locally.
- Parameters:
from_interface (
MessengerInterfaceWithAttachments
) – messenger interface the attachment was received from.- Return type:
Optional
[bytes
]
- class Audio(*, chatsky_attachment_type: Literal['audio'] = 'audio', source: Url | Path | None = None, use_cache: bool = True, cached_filename: Path | None = None, id: str | None = None, **kwargs)[source]#
Bases:
DataAttachment
Represents an audio file attachment.
- chatsky_attachment_type: Literal['audio']#
- class Video(*, chatsky_attachment_type: Literal['video'] = 'video', source: Url | Path | None = None, use_cache: bool = True, cached_filename: Path | None = None, id: str | None = None, **kwargs)[source]#
Bases:
DataAttachment
Represents a video file attachment.
- chatsky_attachment_type: Literal['video']#
- class Animation(*, chatsky_attachment_type: Literal['animation'] = 'animation', source: Url | Path | None = None, use_cache: bool = True, cached_filename: Path | None = None, id: str | None = None, **kwargs)[source]#
Bases:
DataAttachment
Represents an animation file attachment.
- chatsky_attachment_type: Literal['animation']#
- class Image(*, chatsky_attachment_type: Literal['image'] = 'image', source: Url | Path | None = None, use_cache: bool = True, cached_filename: Path | None = None, id: str | None = None, **kwargs)[source]#
Bases:
DataAttachment
Represents an image file attachment.
- chatsky_attachment_type: Literal['image']#
- class Sticker(*, chatsky_attachment_type: Literal['sticker'] = 'sticker', source: Url | Path | None = None, use_cache: bool = True, cached_filename: Path | None = None, id: str | None = None, **kwargs)[source]#
Bases:
DataAttachment
Represents a sticker as a file attachment.
- chatsky_attachment_type: Literal['sticker']#
- class Document(*, chatsky_attachment_type: Literal['document'] = 'document', source: Url | Path | None = None, use_cache: bool = True, cached_filename: Path | None = None, id: str | None = None, **kwargs)[source]#
Bases:
DataAttachment
Represents a document file attachment.
- chatsky_attachment_type: Literal['document']#
- class VoiceMessage(*, chatsky_attachment_type: Literal['voice_message'] = 'voice_message', source: Url | Path | None = None, use_cache: bool = True, cached_filename: Path | None = None, id: str | None = None, **kwargs)[source]#
Bases:
DataAttachment
Represents a voice message.
- chatsky_attachment_type: Literal['voice_message']#
- class VideoMessage(*, chatsky_attachment_type: Literal['video_message'] = 'video_message', source: Url | Path | None = None, use_cache: bool = True, cached_filename: Path | None = None, id: str | None = None, **kwargs)[source]#
Bases:
DataAttachment
Represents a video message.
- chatsky_attachment_type: Literal['video_message']#
- class MediaGroup(*, chatsky_attachment_type: Literal['media_group'] = 'media_group', group: List[Audio | Video | Image | Document | DataAttachment] = None, **kwargs)[source]#
Bases:
Attachment
Represents a group of media attachments. Without this class attachments are sent one-by-one.
Be mindful of limitations that certain services apply (e.g. Telegram does not allow audio or document files to be mixed with other types when using media groups, so you should send them separately by putting them directly in
attachments
).- group: List[Union[Audio, Video, Image, Document, DataAttachment]]#
- chatsky_attachment_type: Literal['media_group']#
- class Message(text=None, *, attachments=None, annotations=None, misc=None, original_message=None, **kwargs)[source]#
Bases:
DataModel
Class representing a message and contains several class level variables to store message information.
It includes message text, list of attachments, annotations, MISC dictionary (that consists of user-defined parameters) and original message field that represents the update received from messenger interface API.
- text: Optional[str]#
- attachments: Optional[List[Union[CallbackQuery, Location, Contact, Invoice, Poll, Audio, Video, Animation, Image, Sticker, Document, VoiceMessage, VideoMessage, MediaGroup, DataModel]]]#
- annotations: Optional[Dict[str, Any]]#
- misc: Optional[Dict[str, Any]]#
- original_message: Optional[Any]#
- pickle_serialize_dicts(value)[source]#
Serialize values that are not json-serializable via pickle. Allows storing arbitrary data in misc/annotations when using context storages.
- classmethod pickle_validate_dicts(value)[source]#
Restore values serialized with
pickle_serialize_dicts()
.
- pickle_serialize_original_message(value)[source]#
Cast
original_message
to string via pickle. Allows storing arbitrary data in this field when using context storages.
- classmethod pickle_validate_original_message(value)[source]#
Restore
original_message
after being processed withpickle_serialize_original_message()
.
- classmethod validate_from_str(data)[source]#
Allow instantiating this class from a single string which becomes
Message.text