Edits history of script submission #95 for ' Post an unencrypted message (matrix)'

  • deno
    One script reply has been approved by the moderators
    Ap­pro­ved
    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 hugo697 378 days ago

  • deno
    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 1011 days ago

  • deno
    
    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 1014 days ago

  • deno
    import * as wmill from "https://deno.land/x/[email protected]/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 1142 days ago

  • deno
    import * as wmill from "https://deno.land/x/[email protected]/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 1177 days ago

  • deno
    import * as wmill from "https://deno.land/x/[email protected]/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 1327 days ago

  • deno
    import * as wmill from "https://deno.land/x/[email protected]/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 1327 days ago

  • deno
    import * as wmill from "https://deno.land/x/[email protected]/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 1379 days ago