This commit is contained in:
夜坂雅 2022-11-23 20:53:25 +08:00
parent 514f844ea5
commit 5c8588d037
2 changed files with 15 additions and 39 deletions

View file

@ -50,6 +50,7 @@ async def send_text_to_room(
markdown_convert: bool = True,
reply_to_event_id: Optional[str] = None,
literal_text: Optional[bool] = False,
literal_text_substitute: Optional[str] = None,
) -> Union[RoomSendResponse, ErrorResponse]:
"""Send text to a matrix room.
@ -130,7 +131,10 @@ async def send_text_to_room(
body += ref_body.rstrip().replace("\n", "\n> ")
body += "\n\n"
body += message
if literal_text_substitute:
body += literal_text_substitute
else:
body += message
content = {
"msgtype": msgtype,

View file

@ -1,17 +1,8 @@
import logging
from typing import Union
from nio import (
AsyncClient,
ErrorResponse,
MatrixRoom,
RoomGetEventError,
RoomMessageText,
RoomSendResponse,
SendRetryError,
)
from nio import AsyncClient, MatrixRoom, RoomGetEventError, RoomMessageText
from nyx_bot.chat_functions import make_pill
from nyx_bot.chat_functions import make_pill, send_text_to_room
logger = logging.getLogger(__name__)
@ -43,31 +34,6 @@ def make_jerryxiao_reply(from_sender: str, to_sender: str, ref: str, room: Matri
return (reply, reply_formatted)
async def send_in_reply_to(
client: AsyncClient,
room_id: str,
event: RoomMessageText,
body: str,
formatted_body: str,
) -> Union[RoomSendResponse, ErrorResponse]:
content = {
"msgtype": "m.text",
"format": "org.matrix.custom.html",
"body": body,
"formatted_body": formatted_body,
}
content["m.relates_to"] = {"m.in_reply_to": {"event_id": event.event_id}}
try:
return 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}")
async def send_jerryxiao(
client: AsyncClient,
room: MatrixRoom,
@ -96,6 +62,12 @@ async def send_jerryxiao(
send_text_tuple = make_jerryxiao_reply(from_sender, to_sender, action, room)
send_text = send_text_tuple[0]
send_text_formatted = send_text_tuple[1]
await send_in_reply_to(
client, room.room_id, event, send_text, send_text_formatted
await send_text_to_room(
client,
room.room_id,
send_text_formatted,
False,
False,
reply_to_event_id=event.event_id,
literal_text_substitute=send_text,
)