generated from anoadragon453/nio-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
chat_functions.py
46 lines (33 loc) · 1.21 KB
/
chat_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# coding=utf-8
import logging
from markdown import markdown
from nio import SendRetryError
logger = logging.getLogger(__name__)
async def send_text_to_room(
client, room_id, message, notice=True, markdown_convert=True
):
"""Send text to a matrix room
Args:
client (nio.AsyncClient): The client to communicate to matrix with
room_id (str): The ID of the room to send the message to
message (str): The message content
notice (bool): Whether the message should be sent with an "m.notice" message type
(will not ping users)
markdown_convert (bool): Whether to convert the message content to markdown.
Defaults to true.
"""
# Determine whether to ping room members or not
msgtype = "m.notice" if notice else "m.text"
content = {
"msgtype": msgtype,
"format": "org.matrix.custom.html",
"body": message,
}
if markdown_convert:
content["formatted_body"] = markdown(message)
try:
await client.room_send(
room_id, "m.room.message", content, ignore_unverified_devices=True,
)
except SendRetryError:
logger.exception(f"Unable to send message response to {room_id}")