Add hello world code
This commit is contained in:
parent
cb9d548c5b
commit
85ee0ec4b6
61
main.ts
Normal file
61
main.ts
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
import * as config from "./config.json";
|
||||||
|
|
||||||
|
const sdk = require("matrix-bot-sdk");
|
||||||
|
|
||||||
|
const {
|
||||||
|
homeserverUrl,
|
||||||
|
accessToken,
|
||||||
|
storageLocation,
|
||||||
|
cryptoFolderLocation,
|
||||||
|
} = config;
|
||||||
|
|
||||||
|
const {
|
||||||
|
MatrixClient,
|
||||||
|
SimpleFsStorageProvider,
|
||||||
|
RustSdkCryptoStorageProvider,
|
||||||
|
AutojoinRoomsMixin
|
||||||
|
} = sdk;
|
||||||
|
|
||||||
|
// In order to make sure the bot doesn't lose its state between restarts, we'll give it a place to cache
|
||||||
|
// any information it needs to. You can implement your own storage provider if you like, but a JSON file
|
||||||
|
// will work fine for this example.
|
||||||
|
const storage = new SimpleFsStorageProvider(storageLocation);
|
||||||
|
const cryptoProvider = new RustSdkCryptoStorageProvider(cryptoFolderLocation);
|
||||||
|
|
||||||
|
const client = new MatrixClient(homeserverUrl, accessToken, storage, cryptoProvider);
|
||||||
|
AutojoinRoomsMixin.setupOnClient(client);
|
||||||
|
|
||||||
|
// Before we start the bot, register our command handler
|
||||||
|
client.on("room.message", handleCommand);
|
||||||
|
|
||||||
|
// Now that everything is set up, start the bot. This will start the sync loop and run until killed.
|
||||||
|
client.start().then(() => {
|
||||||
|
console.log("Bot started!")
|
||||||
|
});
|
||||||
|
|
||||||
|
interface Event {
|
||||||
|
content: {
|
||||||
|
msgtype: string,
|
||||||
|
body: string,
|
||||||
|
};
|
||||||
|
sender: string,
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is the command handler we registered a few lines up
|
||||||
|
async function handleCommand(roomId: string, event: Event) {
|
||||||
|
console.log("event:", event);
|
||||||
|
// Don't handle unhelpful events (ones that aren't text messages, are redacted, or sent by us)
|
||||||
|
if (event.content?.msgtype !== 'm.text') return;
|
||||||
|
if (event.sender === await client.getUserId()) return;
|
||||||
|
|
||||||
|
// Check to ensure that the `!hello` command is being run
|
||||||
|
const body = event.content.body;
|
||||||
|
if (!body?.startsWith("!hello")) return;
|
||||||
|
|
||||||
|
// Now that we've passed all the checks, we can actually act upon the command
|
||||||
|
await client.setTyping(roomId, true);
|
||||||
|
setTimeout(async () => {
|
||||||
|
await client.setTyping(roomId, false);
|
||||||
|
await client.replyNotice(roomId, event, "Hello world!");
|
||||||
|
}, 1500);
|
||||||
|
}
|
Loading…
Reference in a new issue