Message object. You obtain instances of it through :class:`deltachat.account.Account` or :class:`deltachat.chat.Chat`.
| 11 | |
| 12 | |
| 13 | class Message: |
| 14 | """Message object. |
| 15 | |
| 16 | You obtain instances of it through :class:`deltachat.account.Account` or |
| 17 | :class:`deltachat.chat.Chat`. |
| 18 | """ |
| 19 | |
| 20 | def __init__(self, account, dc_msg) -> None: |
| 21 | self.account = account |
| 22 | assert isinstance(self.account._dc_context, ffi.CData) |
| 23 | assert isinstance(dc_msg, ffi.CData) |
| 24 | assert dc_msg != ffi.NULL |
| 25 | self._dc_msg = dc_msg |
| 26 | msg_id = self.id |
| 27 | assert msg_id is not None and msg_id >= 0, repr(msg_id) |
| 28 | |
| 29 | def __eq__(self, other) -> bool: |
| 30 | if other is None: |
| 31 | return False |
| 32 | return self.account == other.account and self.id == other.id |
| 33 | |
| 34 | def __repr__(self) -> str: |
| 35 | c = self.get_sender_contact() |
| 36 | typ = "outgoing" if self.is_outgoing() else "incoming" |
| 37 | return ( |
| 38 | f"<Message {typ} sys={self.is_system_message()} {repr(self.text[:100])} " |
| 39 | f"id={self.id} sender={c.id}/{c.addr} chat={self.chat.id}/{self.chat.get_name()}>" |
| 40 | ) |
| 41 | |
| 42 | @classmethod |
| 43 | def from_db(cls, account, id) -> Optional["Message"]: |
| 44 | """Attempt to load the message from the database given its ID. |
| 45 | |
| 46 | None is returned if the message does not exist, i.e. deleted.""" |
| 47 | assert id > 0 |
| 48 | res = lib.dc_get_msg(account._dc_context, id) |
| 49 | if res == ffi.NULL: |
| 50 | return None |
| 51 | return cls(account, ffi.gc(res, lib.dc_msg_unref)) |
| 52 | |
| 53 | @classmethod |
| 54 | def new_empty(cls, account, view_type): |
| 55 | """create a non-persistent message. |
| 56 | |
| 57 | :param view_type: the message type code or one of the strings: |
| 58 | "text", "audio", "video", "file", "sticker", "videochat", "webxdc" |
| 59 | """ |
| 60 | view_type_code = view_type if isinstance(view_type, int) else get_viewtype_code_from_name(view_type) |
| 61 | return Message( |
| 62 | account, |
| 63 | ffi.gc(lib.dc_msg_new(account._dc_context, view_type_code), lib.dc_msg_unref), |
| 64 | ) |
| 65 | |
| 66 | def create_chat(self): |
| 67 | """create or get an existing chat (group) object for this message. |
| 68 | |
| 69 | If the message is a contact request |
| 70 | the sender will become an accepted contact. |