2023-03-05 09:07:25 -05:00
|
|
|
from nio import AsyncClient
|
2023-04-11 01:41:26 -04:00
|
|
|
import re
|
|
|
|
import markdown
|
|
|
|
|
2023-03-05 09:07:25 -05:00
|
|
|
|
|
|
|
async def send_room_message(client: AsyncClient,
|
|
|
|
room_id: str,
|
2023-04-09 22:52:18 -04:00
|
|
|
reply_message: str,
|
|
|
|
sender_id: str = '',
|
|
|
|
user_message: str = '',
|
2023-04-11 01:41:26 -04:00
|
|
|
reply_to_event_id: str = '',
|
|
|
|
markdown_formatted: bool = False) -> None:
|
|
|
|
NORMAL_BODY = content = {"msgtype": "m.text", "body": reply_message, }
|
2023-03-22 10:28:22 -04:00
|
|
|
if reply_to_event_id == '':
|
2023-04-11 01:41:26 -04:00
|
|
|
if markdown_formatted:
|
2023-04-14 07:03:34 -04:00
|
|
|
# only format message contains multiline codes, *, |
|
|
|
|
if re.search(r"```|\*|\|", reply_message) is not None:
|
2023-04-11 01:41:26 -04:00
|
|
|
content = {
|
|
|
|
"msgtype": "m.text",
|
|
|
|
"body": reply_message,
|
|
|
|
"format": "org.matrix.custom.html",
|
|
|
|
"formatted_body": markdown.markdown(reply_message, extensions=['nl2br', 'tables', 'fenced_code'])
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
content = NORMAL_BODY
|
|
|
|
|
|
|
|
else:
|
|
|
|
content = NORMAL_BODY
|
2023-03-22 10:28:22 -04:00
|
|
|
else:
|
2023-04-09 22:52:18 -04:00
|
|
|
body = r'> <' + sender_id + r'> ' + user_message + r'\n\n' + reply_message
|
|
|
|
format = r'org.matrix.custom.html'
|
2023-04-10 07:37:43 -04:00
|
|
|
formatted_body = r'<mx-reply><blockquote><a href="https://matrix.to/#/' + room_id + r'/' + reply_to_event_id \
|
2023-04-11 01:41:26 -04:00
|
|
|
+ r'">In reply to</a> <a href="https://matrix.to/#/' + sender_id + r'">' + sender_id \
|
|
|
|
+ r'</a><br>' + user_message + r'</blockquote></mx-reply>' + reply_message
|
2023-04-09 22:52:18 -04:00
|
|
|
|
2023-04-11 01:41:26 -04:00
|
|
|
content = {"msgtype": "m.text", "body": body, "format": format, "formatted_body": formatted_body,
|
|
|
|
"m.relates_to": {"m.in_reply_to": {"event_id": reply_to_event_id}}, }
|
2023-03-05 09:07:25 -05:00
|
|
|
await client.room_send(
|
|
|
|
room_id,
|
|
|
|
message_type="m.room.message",
|
2023-03-22 10:28:22 -04:00
|
|
|
content=content,
|
2023-03-14 10:37:30 -04:00
|
|
|
ignore_unverified_devices=True,
|
2023-03-05 09:07:25 -05:00
|
|
|
)
|
|
|
|
await client.room_typing(room_id, typing_state=False)
|