ErisPulse-MatrixAdapter 4.0.1__tar.gz → 4.0.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,441 @@
1
+ Metadata-Version: 2.4
2
+ Name: ErisPulse-MatrixAdapter
3
+ Version: 4.0.2
4
+ Summary: ErisPulse的Matrix协议适配模块
5
+ Author-email: wsu2059 <wsu2059@qq.com>
6
+ Project-URL: homepage, https://github.com/ErisPulse/ErisPulse-MatrixAdapter
7
+ Requires-Python: >=3.9
8
+ Description-Content-Type: text/markdown
9
+
10
+ # ErisPulse Matrix Adapter
11
+
12
+ [English](#english) | [中文](#中文)
13
+
14
+ ---
15
+
16
+ <a id="english"></a>
17
+
18
+ ## English
19
+
20
+ A Matrix protocol adapter built on the [ErisPulse](https://github.com/ErisPulse/ErisPulse/) framework. It receives events via the Long Polling Sync API and integrates modules for multiple scenarios such as private chats and groups, providing a unified interface for event handling and message operations.
21
+
22
+ ### Usage Examples
23
+
24
+ #### OneBot12 Standard Event Types
25
+
26
+ The MatrixAdapter is fully compatible with the OneBot12 standard event format, with some extension fields:
27
+
28
+ | Event Type | detail_type | Description |
29
+ |------------|-------------|-------------|
30
+ | Message (Private) | private | A message sent by a user in a DM room |
31
+ | Message (Group) | group | A message sent by a user in a group room |
32
+ | Group Member Increase | group_member_increase | A user joined the room |
33
+ | Group Member Decrease | group_member_decrease | A user left or was banned |
34
+ | Member Info Update | matrix_member_update | Room member information changed |
35
+ | Matrix Reaction | matrix_reaction | A reaction to a message |
36
+ | Matrix Redaction | matrix_redaction | A message was redacted/deleted |
37
+ | Matrix Room Name Change | matrix_name | The room name changed |
38
+ | Matrix Room Topic Change | matrix_topic | The room topic changed |
39
+ | Matrix Room Avatar Change | matrix_avatar | The room avatar changed |
40
+ | Matrix Power Levels Change | matrix_power_levels | Room permissions changed |
41
+
42
+ ---
43
+
44
+ ### Sending Messages
45
+
46
+ ```python
47
+ from ErisPulse import sdk
48
+ matrix = sdk.adapter.get("matrix")
49
+
50
+ # Send a text message
51
+ await matrix.Send.To("group", room_id).Text("Hello World!")
52
+
53
+ # Send a message with @mention
54
+ await matrix.Send.To("group", room_id).At("@user:matrix.org").Text("Hello")
55
+
56
+ # Send a message mentioning everyone
57
+ await matrix.Send.To("group", room_id).AtAll().Text("Announcement")
58
+
59
+ # Send a reply message
60
+ await matrix.Send.To("group", room_id).Reply("$event_id").Text("Reply content")
61
+
62
+ # Send an image (URL)
63
+ await matrix.Send.To("group", room_id).Image("https://example.com/image.png")
64
+
65
+ # Send an image (MXC URI)
66
+ await matrix.Send.To("group", room_id).Image("mxc://matrix.org/abc123")
67
+
68
+ # Send an image (binary data)
69
+ with open("image.png", "rb") as f:
70
+ image_data = f.read()
71
+ await matrix.Send.To("group", room_id).Image(image_data)
72
+
73
+ # Send an image (local file path)
74
+ await matrix.Send.To("group", room_id).Image("/path/to/image.png")
75
+
76
+ # Send a notice message (m.notice)
77
+ await matrix.Send.To("group", room_id).Notice("System notice")
78
+
79
+ # Send an HTML-formatted message
80
+ await matrix.Send.To("group", room_id).Html("<b>bold</b> <i>italic</i>", fallback="bold italic")
81
+
82
+ # Send a file (with filename)
83
+ await matrix.Send.To("group", room_id).File("/path/to/file.pdf", filename="document.pdf")
84
+
85
+ # Combined usage: reply + mention
86
+ await matrix.Send.To("group", room_id).Reply("$event_id").At("@user:matrix.org").Text("Composite message")
87
+
88
+ # Send a OneBot12-format message using Raw_ob12
89
+ message = [
90
+ {"type": "text", "data": {"text": "First line"}},
91
+ {"type": "image", "data": {"file": "https://example.com/img.jpg"}},
92
+ {"type": "text", "data": {"text": "Second line"}}
93
+ ]
94
+ await matrix.Send.To("group", room_id).Raw_ob12(message)
95
+ ```
96
+
97
+ ---
98
+
99
+ ### Configuration
100
+
101
+ A default configuration is generated automatically on first run. MatrixAdapter supports multi-account configuration.
102
+
103
+ ```toml
104
+ # config.toml
105
+ # Account 1
106
+ [Matrix_Adapter.accounts.default]
107
+ homeserver = "https://matrix.org" # Matrix server address (required)
108
+ access_token = "YOUR_ACCESS_TOKEN" # Access token (one of access_token or user_id+password)
109
+ user_id = "" # Matrix user ID (e.g. @bot:matrix.org)
110
+ password = "" # Matrix user password
111
+ auto_accept_invites = true # Whether to auto-accept room invites (optional, default true)
112
+ enabled = true # Whether to enable (optional, default true)
113
+
114
+ # Account 2
115
+ [Matrix_Adapter.accounts.bot2]
116
+ homeserver = "https://matrix.example.com"
117
+ access_token = "ANOTHER_TOKEN"
118
+ enabled = true
119
+ ```
120
+
121
+ > Backward compatibility: If an old single-account `[Matrix_Adapter]` configuration (containing access_token) is detected, it will be automatically migrated to `accounts.default`.
122
+
123
+ **Configuration fields (per account):**
124
+ - `homeserver`: Matrix server address (required), defaults to `https://matrix.org`
125
+ - `access_token`: Access token, obtainable from a Matrix client (e.g. Element) settings
126
+ - `user_id`: Matrix user ID (e.g. `@bot:matrix.org`), used together with `password`
127
+ - `password`: Matrix user password, used for auto-login to obtain an access_token
128
+ - `auto_accept_invites`: Whether to auto-accept room invites, defaults to `true`
129
+ - `enabled`: Whether to enable this account (optional, default true)
130
+
131
+ **Authentication methods:**
132
+ - Method 1 (recommended): Provide `access_token` directly
133
+ - Method 2: Provide `user_id` and `password`; the adapter will call the login API to obtain a token automatically
134
+
135
+ ---
136
+
137
+ ### Matrix-Specific Features
138
+
139
+ See the [Matrix platform features documentation](platform-features.md) for platform-specific capabilities, including the decentralized architecture, room concepts, Long Polling sync, MXC URIs, HTML rich text, reactions, message editing, and extension field descriptions.
140
+
141
+ For the detailed event conversion reference, see the [conversion mapping documentation](CoverToOnebot12.md).
142
+
143
+ ### Event Listening Examples
144
+
145
+ #### Using the Event Module (Recommended)
146
+
147
+ ```python
148
+ from ErisPulse.Core.Event import message, notice
149
+
150
+ @message.on_message()
151
+ async def handle_message(event):
152
+ if event["platform"] == "matrix":
153
+ detail_type = event["detail_type"]
154
+ if detail_type == "private":
155
+ # Handle private message
156
+ pass
157
+ elif detail_type == "group":
158
+ # Handle group message
159
+ pass
160
+
161
+ @notice.on_notice()
162
+ async def handle_notice(event):
163
+ if event["platform"] == "matrix":
164
+ detail_type = event["detail_type"]
165
+ if detail_type == "matrix_reaction":
166
+ # Handle reaction
167
+ reaction_key = event.get("matrix_reaction_key", "")
168
+ elif detail_type == "matrix_redaction":
169
+ # Handle message redaction
170
+ redacted_id = event.get("matrix_redacted_event_id", "")
171
+ elif detail_type == "group_member_increase":
172
+ # Handle member join
173
+ user_id = event.get("user_id", "")
174
+ ```
175
+
176
+ #### Using OneBot12 Standard Events
177
+
178
+ ```python
179
+ @sdk.adapter.on("message")
180
+ async def handle_message(event):
181
+ if event["platform"] == "matrix":
182
+ bot_id = event["self"]["user_id"]
183
+ print(f"Message from Bot: {bot_id}")
184
+
185
+ @sdk.adapter.on("notice")
186
+ async def handle_notice(event):
187
+ if event["platform"] == "matrix":
188
+ # Handle Matrix notice event
189
+ pass
190
+ ```
191
+
192
+ #### Using Event Mixin Methods
193
+
194
+ ```python
195
+ @message.on_message()
196
+ async def handle_message(event):
197
+ if event.get("platform") != "matrix":
198
+ return
199
+
200
+ room_id = event.get_room_id() # Get the room ID
201
+ event_type = event.get_matrix_event_type() # Get the raw Matrix event type
202
+ sender = event.get_matrix_sender() # Get the sender ID
203
+ is_edited = event.is_edited() # Whether the message is an edit
204
+ is_notice = event.is_notice() # Whether it is an m.notice type
205
+ ```
206
+
207
+ ### Notes
208
+
209
+ 1. Make sure all handlers are registered before calling `startup()`.
210
+ 2. Matrix is a decentralized protocol. User IDs use the format `@user:server.domain`, and room IDs use the format `!room_id:server.domain`.
211
+ 3. Matrix does not distinguish between group chats and private chats — all conversations are "rooms". The adapter identifies private chats via DM account data automatically.
212
+ 4. The adapter uses Long Polling (the `/sync` API) to receive events, not WebSocket.
213
+ 5. Media files are referenced via `mxc://` URIs; the adapter supports automatic upload and download.
214
+ 6. Call `shutdown()` on program exit to ensure resources are released.
215
+ 7. Auto-accepting room invites is supported (can be disabled via the `auto_accept_invites` config).
216
+ 8. HTML-formatted messages are supported via the `.Html()` method.
217
+ 9. Message replies (`.Reply()`) and user mentions (`.At()`, `.AtAll()`) are supported.
218
+
219
+ ---
220
+
221
+ ### Reference Links
222
+
223
+ - [ErisPulse Main Repository](https://github.com/ErisPulse/ErisPulse/)
224
+ - [Matrix Protocol Specification](https://spec.matrix.org/)
225
+ - [Matrix Client-Server API](https://spec.matrix.org/v1.11/client-server-api/)
226
+ - [Module Development Guide](https://www.erisdev.com/#docs/developer-guide/README.md)
227
+
228
+ ---
229
+
230
+ <a id="中文"></a>
231
+
232
+ ## 中文
233
+
234
+ ## 简介
235
+ MatrixAdapter 是基于 [ErisPulse](https://github.com/ErisPulse/ErisPulse/) 架构的 Matrix 协议适配器,通过 Long Polling Sync API 接收事件,整合了私聊、群组等多种场景的功能模块,提供统一的事件处理和消息操作接口。
236
+
237
+ ## 使用示例
238
+
239
+ ### OneBot12标准事件类型
240
+
241
+ MatrixAdapter 适配器完全兼容 OneBot12 标准事件格式,并提供了一些扩展字段:
242
+
243
+ | 事件类型 | detail_type | 说明 |
244
+ |----------|-------------|------|
245
+ | 消息事件(私聊) | private | DM房间中用户发送的消息 |
246
+ | 消息事件(群组) | group | 群组房间中用户发送的消息 |
247
+ | 群成员增加 | group_member_increase | 用户加入房间 |
248
+ | 群成员减少 | group_member_decrease | 用户离开/被封禁 |
249
+ | 成员信息更新 | matrix_member_update | 房间成员信息变更 |
250
+ | Matrix表情回应 | matrix_reaction | 消息表情回应 |
251
+ | Matrix消息撤回 | matrix_redaction | 消息被撤回/删除 |
252
+ | Matrix房间名称变更 | matrix_name | 房间名称变更 |
253
+ | Matrix房间话题变更 | matrix_topic | 房间话题变更 |
254
+ | Matrix房间头像变更 | matrix_avatar | 房间头像变更 |
255
+ | Matrix权限等级变更 | matrix_power_levels | 房间权限变更 |
256
+
257
+ ---
258
+
259
+ ## 消息发送示例
260
+
261
+ ```python
262
+ from ErisPulse import sdk
263
+ matrix = sdk.adapter.get("matrix")
264
+
265
+ # 发送文本消息
266
+ await matrix.Send.To("group", room_id).Text("Hello World!")
267
+
268
+ # 发送带@的消息
269
+ await matrix.Send.To("group", room_id).At("@user:matrix.org").Text("你好")
270
+
271
+ # 发送带@所有人的消息
272
+ await matrix.Send.To("group", room_id).AtAll().Text("公告通知")
273
+
274
+ # 发送回复消息
275
+ await matrix.Send.To("group", room_id).Reply("$event_id").Text("回复内容")
276
+
277
+ # 发送图片(URL)
278
+ await matrix.Send.To("group", room_id).Image("https://example.com/image.png")
279
+
280
+ # 发送图片(MXC URI)
281
+ await matrix.Send.To("group", room_id).Image("mxc://matrix.org/abc123")
282
+
283
+ # 发送图片(二进制数据)
284
+ with open("image.png", "rb") as f:
285
+ image_data = f.read()
286
+ await matrix.Send.To("group", room_id).Image(image_data)
287
+
288
+ # 发送图片(本地文件路径)
289
+ await matrix.Send.To("group", room_id).Image("/path/to/image.png")
290
+
291
+ # 发送通知消息(m.notice)
292
+ await matrix.Send.To("group", room_id).Notice("系统通知")
293
+
294
+ # 发送HTML格式消息
295
+ await matrix.Send.To("group", room_id).Html("<b>加粗</b> <i>斜体</i>", fallback="加粗 斜体")
296
+
297
+ # 发送文件(带文件名)
298
+ await matrix.Send.To("group", room_id).File("/path/to/file.pdf", filename="文档.pdf")
299
+
300
+ # 组合使用:回复 + @
301
+ await matrix.Send.To("group", room_id).Reply("$event_id").At("@user:matrix.org").Text("复合消息")
302
+
303
+ # 使用 Raw_ob12 发送 OneBot12 格式消息
304
+ message = [
305
+ {"type": "text", "data": {"text": "第一行"}},
306
+ {"type": "image", "data": {"file": "https://example.com/img.jpg"}},
307
+ {"type": "text", "data": {"text": "第二行"}}
308
+ ]
309
+ await matrix.Send.To("group", room_id).Raw_ob12(message)
310
+ ```
311
+
312
+ ---
313
+
314
+ ### 配置说明
315
+
316
+ 首次运行会自动生成默认配置。MatrixAdapter 支持多账户配置。
317
+
318
+ ```toml
319
+ # config.toml
320
+ # 账户1
321
+ [Matrix_Adapter.accounts.default]
322
+ homeserver = "https://matrix.org" # Matrix服务器地址(必填)
323
+ access_token = "YOUR_ACCESS_TOKEN" # 访问令牌(与 user_id+password 二选一)
324
+ user_id = "" # Matrix用户ID(如 @bot:matrix.org)
325
+ password = "" # Matrix用户密码
326
+ auto_accept_invites = true # 是否自动接受房间邀请(可选,默认为true)
327
+ enabled = true # 是否启用(可选,默认为true)
328
+
329
+ # 账户2
330
+ [Matrix_Adapter.accounts.bot2]
331
+ homeserver = "https://matrix.example.com"
332
+ access_token = "ANOTHER_TOKEN"
333
+ enabled = true
334
+ ```
335
+
336
+ > 兼容旧配置:若检测到旧的单账户 `[Matrix_Adapter]` 配置(含 access_token),会自动迁移为 `accounts.default`。
337
+
338
+ **配置项说明(每个账户):**
339
+ - `homeserver`:Matrix服务器地址(必填),默认为 `https://matrix.org`
340
+ - `access_token`:访问令牌,可从Matrix客户端(如 Element)的设置中获取
341
+ - `user_id`:Matrix用户ID(如 `@bot:matrix.org`),与 `password` 配合使用
342
+ - `password`:Matrix用户密码,用于自动登录获取 access_token
343
+ - `auto_accept_invites`:是否自动接受房间邀请,默认为 `true`
344
+ - `enabled`:是否启用该账户(可选,默认为true)
345
+
346
+ **认证方式:**
347
+ - 方式一(推荐):直接提供 `access_token`
348
+ - 方式二:提供 `user_id` 和 `password`,适配器会自动调用登录接口获取 token
349
+
350
+ ---
351
+
352
+ ## Matrix平台特有功能
353
+
354
+ 请参考 [Matrix平台特性文档](platform-features.md) 了解Matrix平台的特有功能,包括去中心化架构、房间概念、Long Polling同步、MXC URI、HTML富文本、表情回应、消息编辑、扩展字段说明等内容。
355
+
356
+ 详细的事件转换对照请参考 [转换对照文档](CoverToOnebot12.md)。
357
+
358
+ ## 事件监听示例
359
+
360
+ ### 使用 Event 模块(推荐)
361
+
362
+ ```python
363
+ from ErisPulse.Core.Event import message, notice
364
+
365
+ @message.on_message()
366
+ async def handle_message(event):
367
+ if event["platform"] == "matrix":
368
+ detail_type = event["detail_type"]
369
+ if detail_type == "private":
370
+ # 处理私聊消息
371
+ pass
372
+ elif detail_type == "group":
373
+ # 处理群组消息
374
+ pass
375
+
376
+ @notice.on_notice()
377
+ async def handle_notice(event):
378
+ if event["platform"] == "matrix":
379
+ detail_type = event["detail_type"]
380
+ if detail_type == "matrix_reaction":
381
+ # 处理表情回应
382
+ reaction_key = event.get("matrix_reaction_key", "")
383
+ elif detail_type == "matrix_redaction":
384
+ # 处理消息撤回
385
+ redacted_id = event.get("matrix_redacted_event_id", "")
386
+ elif detail_type == "group_member_increase":
387
+ # 处理成员加入
388
+ user_id = event.get("user_id", "")
389
+ ```
390
+
391
+ ### 使用 OneBot12 标准事件
392
+
393
+ ```python
394
+ @sdk.adapter.on("message")
395
+ async def handle_message(event):
396
+ if event["platform"] == "matrix":
397
+ bot_id = event["self"]["user_id"]
398
+ print(f"消息来自Bot: {bot_id}")
399
+
400
+ @sdk.adapter.on("notice")
401
+ async def handle_notice(event):
402
+ if event["platform"] == "matrix":
403
+ # 处理Matrix通知事件
404
+ pass
405
+ ```
406
+
407
+ ### 使用 Event Mixin 方法
408
+
409
+ ```python
410
+ @message.on_message()
411
+ async def handle_message(event):
412
+ if event.get("platform") != "matrix":
413
+ return
414
+
415
+ room_id = event.get_room_id() # 获取房间ID
416
+ event_type = event.get_matrix_event_type() # 获取原始Matrix事件类型
417
+ sender = event.get_matrix_sender() # 获取发送者ID
418
+ is_edited = event.is_edited() # 是否为编辑消息
419
+ is_notice = event.is_notice() # 是否为 m.notice 类型
420
+ ```
421
+
422
+ ## 注意事项:
423
+
424
+ 1. 确保在调用 `startup()` 前完成所有处理器的注册
425
+ 2. Matrix 是去中心化协议,用户ID格式为 `@user:server.domain`,房间ID格式为 `!room_id:server.domain`
426
+ 3. Matrix 不区分群聊和私聊,所有会话都是"房间"。适配器通过 DM 账户数据自动识别私聊
427
+ 4. 适配器使用 Long Polling(`/sync` API)获取事件,而非 WebSocket
428
+ 5. 媒体文件通过 `mxc://` URI 引用,适配器支持自动上传和下载
429
+ 6. 程序退出时请调用 `shutdown()` 确保资源释放
430
+ 7. 支持自动接受房间邀请(可通过 `auto_accept_invites` 配置关闭)
431
+ 8. 支持发送 HTML 格式消息,使用 `.Html()` 方法
432
+ 9. 支持消息回复(`.Reply()`)和用户提及(`.At()`、`.AtAll()`)
433
+
434
+ ---
435
+
436
+ ### 参考链接
437
+
438
+ - [ErisPulse 主库](https://github.com/ErisPulse/ErisPulse/)
439
+ - [Matrix 协议规范](https://spec.matrix.org/)
440
+ - [Matrix Client-Server API](https://spec.matrix.org/v1.11/client-server-api/)
441
+ - [模块开发指南](https://www.erisdev.com/#docs/developer-guide/README.md)
@@ -21,7 +21,7 @@ class MatrixAccountConfig(BotAccountConfig):
21
21
  homeserver: str = field(
22
22
  default="https://matrix.org",
23
23
  metadata={
24
- "description": "Matrix 服务器地址",
24
+ "description": "Matrix server address",
25
25
  "required": True,
26
26
  "webui": {"widget": "text", "group": "connection", "order": 1},
27
27
  },
@@ -29,7 +29,7 @@ class MatrixAccountConfig(BotAccountConfig):
29
29
  access_token: str = field(
30
30
  default="",
31
31
  metadata={
32
- "description": "访问令牌(与 user_id/password 二选一)",
32
+ "description": "Access token (alternative to user_id/password)",
33
33
  "secret": True,
34
34
  "webui": {"widget": "password", "group": "basic", "order": 2},
35
35
  },
@@ -37,14 +37,14 @@ class MatrixAccountConfig(BotAccountConfig):
37
37
  user_id: str = field(
38
38
  default="",
39
39
  metadata={
40
- "description": "用户名(密码登录时填写)",
40
+ "description": "Username (used for password login)",
41
41
  "webui": {"widget": "text", "group": "basic", "order": 3},
42
42
  },
43
43
  )
44
44
  password: str = field(
45
45
  default="",
46
46
  metadata={
47
- "description": "密码(密码登录时填写)",
47
+ "description": "Password (used for password login)",
48
48
  "secret": True,
49
49
  "webui": {"widget": "password", "group": "basic", "order": 4},
50
50
  },
@@ -52,7 +52,7 @@ class MatrixAccountConfig(BotAccountConfig):
52
52
  auto_accept_invites: bool = field(
53
53
  default=True,
54
54
  metadata={
55
- "description": "是否自动接受房间邀请",
55
+ "description": "Automatically accept room invitations",
56
56
  "webui": {"widget": "switch", "group": "behavior", "order": 5},
57
57
  },
58
58
  )