Skip to content

Instantly share code, notes, and snippets.

@MyWay
Created May 3, 2023 21:06
Show Gist options
  • Save MyWay/39298166f5a5e517fad2b95d7f69d092 to your computer and use it in GitHub Desktop.
Save MyWay/39298166f5a5e517fad2b95d7f69d092 to your computer and use it in GitHub Desktop.
Activepiece Send Matrix Message using Room Alias instead of Room Internal ID
// matrix/src/lib/common/common.ts
import { httpClient, HttpMethod, AuthenticationType, HttpResponse } from "@activepieces/pieces-common";
export async function getRoomId(baseUrl: string, roomAlias: string, accessToken: string): Promise<HttpResponse> {
const response = httpClient.sendRequest({
method: HttpMethod.GET,
url: `${baseUrl}/_matrix/client/r0/directory/room/${encodeURIComponent(roomAlias)}`,
authentication: {
type: AuthenticationType.BEARER_TOKEN,
token: accessToken,
},
});
return response;
}
export async function sendMessage(baseUrl: string, roomId: string, accessToken: string, message: string): Promise<HttpResponse> {
const response = httpClient.sendRequest({
method: HttpMethod.POST,
url: `${baseUrl}/_matrix/client/r0/rooms/` + roomId + "/send/m.room.message",
authentication: {
type: AuthenticationType.BEARER_TOKEN,
token: accessToken,
},
body: {
msgtype: "m.text",
body: message,
}
})
return response;
}
// matrix/src/lib/actions/send-message.ts
import { createAction, Property } from "@activepieces/pieces-framework";
import { AuthenticationType, httpClient, HttpMethod } from "@activepieces/pieces-common";
import { getRoomId, sendMessage as sendMatrixMessage } from '../common/common';
import { send } from 'process';
export const sendMessage = createAction({
name: "send_message",
displayName: "Send Message",
description: "Send a message to a room",
props: {
authentication: Property.CustomAuth({
displayName: "Authentication",
description: `
To obtain access token & Home server:
1. Log in to the account you want to get the access token for on Element.
2. Click on the name in the top left corner of the screen, then select "Settings" from the dropdown menu.
3. In the Settings dialog, click the "Help & About" tab on the left side of the screen.
4. Scroll to the bottom of the page and click on the "click to reveal" part of the "Access Token" section.
5. Copy your access token & Home Server URL and paste them into the fields below.
`,
props: {
base_url: Property.ShortText({
displayName: "Home Server",
required: true,
}),
access_token: Property.SecretText({
displayName: "Access Token",
required: true,
})
},
required: true,
}),
room_alias: Property.ShortText({
displayName: "Room Alias",
description: "Copy it from room settings -> advanced -> room addresses -> main address",
required: true,
}),
message: Property.LongText({
displayName: "Message",
description: "The message to send",
required: true,
}),
},
async run({ propsValue }) {
const baseUrl = propsValue.authentication.base_url.replace(/\/$/, "");
const accessToken = propsValue.authentication.access_token;
const roomId = (await getRoomId(baseUrl, propsValue.room_alias, accessToken)).body.room_id;
return await sendMatrixMessage(baseUrl, roomId, accessToken, propsValue.message);
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment