Skip to content

Content

The content module provides typed message payload builders. Each class serialises its data into the dict format expected by the Matrix client-server API. You rarely need to instantiate these directly — Room methods create them for you — but they are useful when constructing custom message types.

from matrix.content import MarkdownMessage, TextContent

plain = TextContent(body="Hello, world!")
rich  = MarkdownMessage(body="**Hello**, world!")

matrix.content.BaseMessageContent

Bases: ABC

Base class for outgoing message payloads.

msgtype instance-attribute

msgtype

build abstractmethod

build()
Source code in matrix/content.py
12
13
14
@abstractmethod
def build(self) -> dict[str, Any]:
    pass

matrix.content.TextContent dataclass

TextContent(body)

Bases: BaseMessageContent

msgtype class-attribute instance-attribute

msgtype = 'm.text'

body instance-attribute

body

build

build()
Source code in matrix/content.py
22
23
def build(self) -> dict:
    return {"msgtype": self.msgtype, "body": self.body}

matrix.content.MarkdownMessage dataclass

MarkdownMessage(body)

Bases: TextContent

build

build()
Source code in matrix/content.py
28
29
30
31
32
33
34
def build(self) -> dict:
    return {
        "msgtype": self.msgtype,
        "body": self.body,
        "format": "org.matrix.custom.html",
        "formatted_body": markdown(self.body, extensions=["nl2br"]),
    }

matrix.content.NoticeContent dataclass

NoticeContent(body)

Bases: TextContent

msgtype class-attribute instance-attribute

msgtype = 'm.notice'

matrix.content.ReplyContent dataclass

ReplyContent(body, reply_to_event_id)

Bases: TextContent

reply_to_event_id instance-attribute

reply_to_event_id

build

build()
Source code in matrix/content.py
46
47
48
49
50
51
def build(self) -> dict:
    return {
        "msgtype": self.msgtype,
        "body": self.body,
        "m.relates_to": {"m.in_reply_to": {"event_id": self.reply_to_event_id}},
    }

matrix.content.EditContent dataclass

EditContent(body, original_event_id)

Bases: TextContent

original_event_id instance-attribute

original_event_id

build

build()
Source code in matrix/content.py
58
59
60
61
62
63
64
65
66
67
68
69
70
def build(self) -> dict:
    return {
        "msgtype": self.msgtype,
        "body": f"* {self.body}",
        "m.new_content": {
            "msgtype": "m.text",
            "body": self.body,
        },
        "m.relates_to": {
            "rel_type": "m.replace",
            "event_id": self.original_event_id,
        },
    }

matrix.content.FileContent dataclass

FileContent(filename, url, mimetype)

Bases: BaseMessageContent

msgtype class-attribute instance-attribute

msgtype = 'm.file'

filename instance-attribute

filename

url instance-attribute

url

mimetype instance-attribute

mimetype

build

build()
Source code in matrix/content.py
80
81
82
83
84
85
86
def build(self) -> dict:
    return {
        "msgtype": self.msgtype,
        "body": self.filename,
        "url": self.url,
        "info": {"mimetype": self.mimetype},
    }

matrix.content.ImageContent dataclass

ImageContent(filename, url, mimetype, height=0, width=0)

Bases: FileContent

msgtype class-attribute instance-attribute

msgtype = 'm.image'

height class-attribute instance-attribute

height = 0

width class-attribute instance-attribute

width = 0

build

build()
Source code in matrix/content.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
def build(self) -> dict:
    return {
        "msgtype": self.msgtype,
        "body": self.filename,
        "url": self.url,
        "info": {
            "mimetype": self.mimetype,
            "h": self.height,
            "w": self.width,
        },
    }

matrix.content.AudioContent dataclass

AudioContent(filename, url, mimetype, duration=0)

Bases: FileContent

msgtype class-attribute instance-attribute

msgtype = 'm.audio'

duration class-attribute instance-attribute

duration = 0

build

build()
Source code in matrix/content.py
113
114
115
116
117
118
119
120
121
122
def build(self) -> dict:
    return {
        "msgtype": self.msgtype,
        "body": self.filename,
        "url": self.url,
        "info": {
            "mimetype": self.mimetype,
            "duration": self.duration,
        },
    }

matrix.content.VideoContent dataclass

VideoContent(
    filename, url, mimetype, height=0, width=0, duration=0
)

Bases: FileContent

msgtype class-attribute instance-attribute

msgtype = 'm.video'

height class-attribute instance-attribute

height = 0

width class-attribute instance-attribute

width = 0

duration class-attribute instance-attribute

duration = 0

build

build()
Source code in matrix/content.py
132
133
134
135
136
137
138
139
140
141
142
143
def build(self) -> dict:
    return {
        "msgtype": self.msgtype,
        "body": self.filename,
        "url": self.url,
        "info": {
            "mimetype": self.mimetype,
            "h": self.height,
            "w": self.width,
            "duration": self.duration,
        },
    }

matrix.content.LocationContent dataclass

LocationContent(geo_uri, description='')

Bases: BaseMessageContent

msgtype class-attribute instance-attribute

msgtype = 'm.location'

geo_uri instance-attribute

geo_uri

description class-attribute instance-attribute

description = ''

build

build()
Source code in matrix/content.py
152
153
154
155
156
157
def build(self) -> dict:
    return {
        "msgtype": self.msgtype,
        "body": self.description or self.geo_uri,
        "geo_uri": self.geo_uri,
    }

matrix.content.ReactionContent dataclass

ReactionContent(event_id, emoji)

Bases: BaseMessageContent

For sending reactions to an event.

event_id instance-attribute

event_id

emoji instance-attribute

emoji

build

build()
Source code in matrix/content.py
167
168
169
170
171
172
173
174
def build(self) -> dict:
    return {
        "m.relates_to": {
            "rel_type": "m.annotation",
            "event_id": self.event_id,
            "key": self.emoji,
        }
    }