sdk-ts/docs/bot.ts

32 lines
1 KiB
TypeScript

// An example pong-pong bot
// I might add a higher level bot sdk at some point, since this is relatively low level
import { Client, Event } from "../dist/src/index.js";
// Create the client
const client = new Client({
// baseUrl: "https://homeserver.tld",
// deviceId: "something",
// token: "supersecrettoken",
// userId: "@userid:homeserver.tld",
baseUrl: "http://localhost:6167",
deviceId: "something",
token: "9SWeOqc5g42O0fS3JesE43s1JujCDD8S",
userId: "@asdf:localhost",
});
function handleEvent(event: Event) {
console.log("received event from " + event.sender);
}
client.on("roomInit", (room) => room.timelines.live.on("timelineAppend", handleEvent));
client.on("roomDeinit", (room) => room.timelines.live.on("timelineAppend", handleEvent));
client.lists.subscribe("allrooms", {
ranges: [[0, 9999999]], // fetch all rooms
required_state: [["m.room.name", ""]], // get their names
timeline_limit: 1, // only listen to the last event (this will not trigger timelineAppend)
});
// Start syncing and receiving events
client.start();