import { fetch } from "https://esm.town/v/std/fetch";
export const sendMatrixChatRoomMessage = async (
accessToken: string,
roomId: string,
transactionId: string,
message: MessageEventContent,
serverUrl: string = "https://matrix.org",
) => {
const url = `${serverUrl}/_matrix/client/v3/rooms/${
encodeURIComponent(roomId)
}/send/m.room.message/${encodeURIComponent(transactionId)}`;
const config = {
method: "PUT",
headers: {
"Authorization": `Bearer ${accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify(message),
};
return await fetch(url, config);
};
export interface MessageEventContent {
msgtype?: string;
body?: string;
format?: string;
formatted_body?: string;
info?: MessageEventContentInfo_;
url?: string;
membership?: RoomPhase;
"m.relates_to"?: {
event_id?: string;
is_falling_back?: boolean;
rel_type?: string;
"m.in_reply_to"?: {
[event_id: string]: string;
};
};
name?: string;
alias?: string;
join_rule?: string;
topic?: string;
display_name?: string;
displayname?: string;
avatar_url?: string;
is_direct?: boolean;
third_party_signed?: string;
last_active_ago?: number;
users?: {
[id: string]: number;
};
"m.new_content"?: MessageEventContent;
"org.matrix.msc1767.message"?: any[];
"org.matrix.msc1767.text"?: string;
join_authorised_via_users_server?: string;
[other: string]: any;
}
export interface MessageEventContentInfo_ {
mimetype?: string;
size?: number;
h?: number;
w?: number;
thumbnail_url?: string;
thumbnail_info?: ThumbnailInfo_;
}
export interface ThumbnailInfo_ {
mimetype: string;
size: number;
h: number;
w: number;
}
export type RoomPhase = "join" | "invite" | "leave";
export interface LinkPreview_ {
url: string;
text?: string;
image_url?: string;
image_width?: number;
image_height?: number;
title?: string;
site_name?: string;
}