type Matrix = {
baseUrl: string;
token: string;
};
export async function main(matrix_res: Matrix, room: string, body: string) {
if (!matrix_res.token) {
throw Error("Sending a message requires an access token.");
}
const roomId = await resolveRoomAlias(matrix_res, room);
const txnId = `${Math.random()}`;
const resp = await fetch(
`${matrix_res.baseUrl}/_matrix/client/v3/rooms/${encodeURIComponent(
roomId,
)}/send/m.room.message/${txnId}`,
{
method: "PUT",
headers: {
Accept: "application/json",
Authorization: `Bearer ${matrix_res.token}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
body,
msgtype: "m.text",
}),
},
);
if (!resp.ok) {
throw Error(`Failed to send message: Error HTTP${resp.status}`);
}
const eventId = (await resp.json())["event_id"];
if (typeof eventId !== "string") {
throw Error(
`Faulty Matrix server implementation: Server didn't provide event_id for this message.`,
);
}
return eventId;
}
/**
* Resolves a room alias to a room id.
* This is basically like resolving a domain name to an IP address.
*/
async function resolveRoomAlias(
matrix_res: Matrix,
room: string,
): Promise<string> {
// Is it already a room ID?
if (room.startsWith("!")) {
return room;
}
const resp = await fetch(
`${
matrix_res.baseUrl
}/_matrix/client/v3/directory/room/${encodeURIComponent(room)}`,
{
headers: {
Accept: "application/json",
...(matrix_res.token && {
Authorization: `Bearer ${matrix_res.token}`,
}),
},
},
);
if (!resp.ok) {
throw Error(`Failed to resolve room alias: Error HTTP${resp.status}`);
}
const roomId = (await resp.json())["room_id"];
if (typeof roomId !== "string") {
throw Error(
`Faulty Matrix server implementation: Server didn't provide room_id for this alias.`,
);
}
return roomId;
}
Submitted by admin 497 days ago
type Matrix = {
baseUrl: string;
token: string;
};
export async function main(
matrix_res: Matrix,
room: string,
body: string,
) {
if (!matrix_res.token) {
throw Error("Sending a message requires an access token.");
}
const roomId = await resolveRoomAlias(matrix_res, room);
const txnId = `${Math.random()}`;
const resp = await fetch(
`${matrix_res.baseUrl}/_matrix/client/v3/rooms/${encodeURIComponent(roomId)}/send/m.room.message/${txnId}`,
{
method: "PUT",
headers: {
"Accept": "application/json",
"Authorization": `Bearer ${matrix_res.token}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
body,
msgtype: "m.text",
}),
},
);
if (!resp.ok) {
throw Error(`Failed to send message: Error HTTP${resp.status}`);
}
const eventId = (await resp.json())["event_id"];
if (typeof eventId !== "string") {
throw Error(`Faulty Matrix server implementation: Server didn't provide event_id for this message.`);
}
return eventId;
}
/**
* Resolves a room alias to a room id.
* This is basically like resolving a domain name to an IP address.
*/
async function resolveRoomAlias(
matrix_res: Matrix,
room: string,
): Promise<string> {
// Is it already a room ID?
if (room.startsWith("!")) {
return room;
}
const resp = await fetch(
`${matrix_res.baseUrl}/_matrix/client/v3/directory/room/${encodeURIComponent(room)
}`,
{
headers: {
"Accept": "application/json",
...(matrix_res.token && {
"Authorization": `Bearer ${matrix_res.token}`,
}),
},
},
);
if (!resp.ok) {
throw Error(`Failed to resolve room alias: Error HTTP${resp.status}`);
}
const roomId = (await resp.json())["room_id"];
if (typeof roomId !== "string") {
throw Error(`Faulty Matrix server implementation: Server didn't provide room_id for this alias.`);
}
return roomId;
}
Submitted by admin 500 days ago
import * as wmill from "https://deno.land/x/windmill@v1.85.0/mod.ts";
export async function main(
matrix_res: wmill.Resource<"matrix">,
room: string,
body: string,
) {
if (!matrix_res.token) {
throw Error("Sending a message requires an access token.");
}
const roomId = await resolveRoomAlias(matrix_res, room);
const txnId = `${Math.random()}`;
const resp = await fetch(
`${matrix_res.baseUrl}/_matrix/client/v3/rooms/${encodeURIComponent(roomId)}/send/m.room.message/${txnId}`,
{
method: "PUT",
headers: {
"Accept": "application/json",
"Authorization": `Bearer ${matrix_res.token}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
body,
msgtype: "m.text",
}),
},
);
if (!resp.ok) {
throw Error(`Failed to send message: Error HTTP${resp.status}`);
}
const eventId = (await resp.json())["event_id"];
if (typeof eventId !== "string") {
throw Error(`Faulty Matrix server implementation: Server didn't provide event_id for this message.`);
}
return eventId;
}
/**
* Resolves a room alias to a room id.
* This is basically like resolving a domain name to an IP address.
*/
async function resolveRoomAlias(
matrix_res: wmill.Resource<"matrix">,
room: string,
): Promise<string> {
// Is it already a room ID?
if (room.startsWith("!")) {
return room;
}
const resp = await fetch(
`${matrix_res.baseUrl}/_matrix/client/v3/directory/room/${encodeURIComponent(room)
}`,
{
headers: {
"Accept": "application/json",
...(matrix_res.token && {
"Authorization": `Bearer ${matrix_res.token}`,
}),
},
},
);
if (!resp.ok) {
throw Error(`Failed to resolve room alias: Error HTTP${resp.status}`);
}
const roomId = (await resp.json())["room_id"];
if (typeof roomId !== "string") {
throw Error(`Faulty Matrix server implementation: Server didn't provide room_id for this alias.`);
}
return roomId;
}
Submitted by adam186 628 days ago
import * as wmill from "https://deno.land/x/windmill@v1.70.1/mod.ts";
export async function main(
matrix_res: wmill.Resource<"matrix">,
room: string,
body: string,
) {
if (!matrix_res.token) {
throw Error("Sending a message requires an access token.");
}
const roomId = await resolveRoomAlias(matrix_res, room);
const txnId = `${Math.random()}`;
const resp = await fetch(
`${matrix_res.baseUrl}/_matrix/client/v3/rooms/${encodeURIComponent(roomId)}/send/m.room.message/${txnId}`,
{
method: "PUT",
headers: {
"Accept": "application/json",
"Authorization": `Bearer ${matrix_res.token}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
body,
msgtype: "m.text",
}),
},
);
if (!resp.ok) {
throw Error(`Failed to send message: Error HTTP${resp.status}`);
}
const eventId = (await resp.json())["event_id"];
if (typeof eventId !== "string") {
throw Error(`Faulty Matrix server implementation: Server didn't provide event_id for this message.`);
}
return eventId;
}
/**
* Resolves a room alias to a room id.
* This is basically like resolving a domain name to an IP address.
*/
async function resolveRoomAlias(
matrix_res: wmill.Resource<"matrix">,
room: string,
): Promise<string> {
// Is it already a room ID?
if (room.startsWith("!")) {
return room;
}
const resp = await fetch(
`${matrix_res.baseUrl}/_matrix/client/v3/directory/room/${encodeURIComponent(room)
}`,
{
headers: {
"Accept": "application/json",
...(matrix_res.token && {
"Authorization": `Bearer ${matrix_res.token}`,
}),
},
},
);
if (!resp.ok) {
throw Error(`Failed to resolve room alias: Error HTTP${resp.status}`);
}
const roomId = (await resp.json())["room_id"];
if (typeof roomId !== "string") {
throw Error(`Faulty Matrix server implementation: Server didn't provide room_id for this alias.`);
}
return roomId;
}
Submitted by adam186 663 days ago
import * as wmill from "https://deno.land/x/windmill@v1.29.0/mod.ts";
export async function main(
matrix_res: wmill.Resource<"matrix">,
room: string,
body: string,
) {
if (!matrix_res.token) {
throw Error("Sending a message requires an access token.");
}
const roomId = await resolveRoomAlias(matrix_res, room);
const txnId = `${Math.random()}`;
const resp = await fetch(
`${matrix_res.baseUrl}/_matrix/client/v3/rooms/${encodeURIComponent(roomId)}/send/m.room.message/${txnId}`,
{
method: "PUT",
headers: {
"Accept": "application/json",
"Authorization": `Bearer ${matrix_res.token}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
body,
msgtype: "m.text",
}),
},
);
if (!resp.ok) {
throw Error(`Failed to send message: Error HTTP${resp.status}`);
}
const eventId = (await resp.json())["event_id"];
if (typeof eventId !== "string") {
throw Error(`Faulty Matrix server implementation: Server didn't provide event_id for this message.`);
}
return eventId;
}
/**
* Resolves a room alias to a room id.
* This is basically like resolving a domain name to an IP address.
*/
async function resolveRoomAlias(
matrix_res: wmill.Resource<"matrix">,
room: string,
): Promise<string> {
// Is it already a room ID?
if (room.startsWith("!")) {
return room;
}
const resp = await fetch(
`${matrix_res.baseUrl}/_matrix/client/v3/directory/room/${encodeURIComponent(room)
}`,
{
headers: {
"Accept": "application/json",
...(matrix_res.token && {
"Authorization": `Bearer ${matrix_res.token}`,
}),
},
},
);
if (!resp.ok) {
throw Error(`Failed to resolve room alias: Error HTTP${resp.status}`);
}
const roomId = (await resp.json())["room_id"];
if (typeof roomId !== "string") {
throw Error(`Faulty Matrix server implementation: Server didn't provide room_id for this alias.`);
}
return roomId;
}
Submitted by jaller94 813 days ago
import * as wmill from "https://deno.land/x/windmill@v1.29.0/mod.ts";
export async function main(
matrix_res: wmill.Resource<"matrix">,
room: string,
body: string,
) {
if (!matrix_res.token) {
throw Error("Sending a message requires an access token.");
}
const roomId = await resolveRoomAlias(matrix_res, room);
const txnId = `${Math.random()}`;
const resp = await fetch(
`${matrix_res.baseUrl}/_matrix/client/v3/rooms/${encodeURIComponent(roomId)
}/send/m.room.message/${txnId}`,
{
method: "PUT",
headers: {
"Accept": "application/json",
"Authorization": `Bearer ${matrix_res.token}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
body,
msgtype: "m.text",
}),
},
);
if (!resp.ok) {
throw Error(`Failed to send message: Error HTTP${resp.status}`);
}
const eventId = (await resp.json())["event_id"];
if (typeof eventId !== "string") {
throw Error(`Faulty Matrix server implementation: Server didn't provide event_id for this message.`);
}
return eventId;
}
/**
* Resolves a room alias to a room id.
* This is basically like resolving a domain name to an IP address.
*/
async function resolveRoomAlias(
matrix_res: wmill.Resource<"matrix">,
room: string,
): Promise<string> {
// Is it already a room ID?
if (room.startsWith("!")) {
return room;
}
const resp = await fetch(
`${matrix_res.baseUrl}/_matrix/client/v3/directory/room/${encodeURIComponent(room)
}`,
{
headers: {
"Accept": "application/json",
...(matrix_res.token && {
"Authorization": `Bearer ${matrix_res.token}`,
}),
},
},
);
if (!resp.ok) {
throw Error(`Failed to resolve room alias: Error HTTP${resp.status}`);
}
const roomId = (await resp.json())["room_id"];
if (typeof roomId !== "string") {
throw Error(`Faulty Matrix server implementation: Server didn't provide room_id for this alias.`);
}
return roomId;
}
Submitted by jaller94 813 days ago
import * as wmill from "https://deno.land/x/windmill@v1.29.0/mod.ts";
export async function main(
matrix_res: wmill.Resource<"matrix">,
room: string,
body: string,
) {
if (!matrix_res.accessToken) {
throw Error("Sending a message requires an access token.");
}
const roomId = await resolveRoomAlias(matrix_res, room);
const txnId = `${Math.random()}`;
const resp = await fetch(
`${matrix_res.homeserverUrl}/_matrix/client/v3/rooms/${encodeURIComponent(roomId)
}/send/m.room.message/${txnId}`,
{
method: "PUT",
headers: {
"Accept": "application/json",
"Authorization": `Bearer ${matrix_res.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
body,
msgtype: "m.text",
}),
},
);
if (!resp.ok) {
throw Error(`Failed to send message: Error HTTP${resp.status}`);
}
const eventId = (await resp.json())["event_id"];
if (typeof eventId !== "string") {
throw Error(`Faulty Matrix server implementation: Server didn't provide event_id for this message.`);
}
return eventId;
}
/**
* Resolves a room alias to a room id.
* This is basically like resolving a domain name to an IP address.
*/
async function resolveRoomAlias(
matrix_res: wmill.Resource<"matrix">,
room: string,
): Promise<string> {
// Is it already a room ID?
if (room.startsWith("!")) {
return room;
}
const resp = await fetch(
`${matrix_res.homeserverUrl}/_matrix/client/v3/directory/room/${encodeURIComponent(room)
}`,
{
headers: {
"Accept": "application/json",
...(matrix_res.accessToken && {
"Authorization": `Bearer ${matrix_res.accessToken}`,
}),
},
},
);
if (!resp.ok) {
throw Error(`Failed to resolve room alias: Error HTTP${resp.status}`);
}
const roomId = (await resp.json())["room_id"];
if (typeof roomId !== "string") {
throw Error(`Faulty Matrix server implementation: Server didn't provide room_id for this alias.`);
}
return roomId;
}
Submitted by jaller94 865 days ago