2019-09-25 14:26:29 +02:00
|
|
|
import logging
|
2020-08-10 00:02:07 +02:00
|
|
|
|
2019-09-25 14:26:29 +02:00
|
|
|
from markdown import markdown
|
2020-08-10 00:02:07 +02:00
|
|
|
from nio import SendRetryError
|
2019-09-25 14:26:29 +02:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-10-04 15:46:51 +02:00
|
|
|
async def send_text_to_room(
|
2020-08-10 00:02:07 +02:00
|
|
|
client, room_id, message, notice=True, markdown_convert=True
|
2019-10-04 15:46:51 +02:00
|
|
|
):
|
2019-09-25 14:26:29 +02:00
|
|
|
"""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
|
|
|
|
|
2019-10-04 15:46:51 +02:00
|
|
|
notice (bool): Whether the message should be sent with an "m.notice" message type
|
|
|
|
(will not ping users)
|
|
|
|
|
2019-09-25 14:26:29 +02:00
|
|
|
markdown_convert (bool): Whether to convert the message content to markdown.
|
|
|
|
Defaults to true.
|
|
|
|
"""
|
2019-10-04 15:46:51 +02:00
|
|
|
# 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,
|
|
|
|
}
|
|
|
|
|
2019-09-25 14:26:29 +02:00
|
|
|
if markdown_convert:
|
2019-10-04 15:46:51 +02:00
|
|
|
content["formatted_body"] = markdown(message)
|
2019-09-25 14:26:29 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
await client.room_send(
|
2020-08-10 00:02:07 +02:00
|
|
|
room_id, "m.room.message", content, ignore_unverified_devices=True,
|
2019-09-25 14:26:29 +02:00
|
|
|
)
|
|
|
|
except SendRetryError:
|
|
|
|
logger.exception(f"Unable to send message response to {room_id}")
|