turt2live / matrix-bot-sdk

TypeScript/JavaScript SDK for Matrix bots
MIT License
199 stars 69 forks source link

Cannot decrypt messages - cant find room key #347

Closed mwinterstorm closed 1 year ago

mwinterstorm commented 1 year ago

Describe the bug I have set up a simple bot following example here for encryption for bots, however the bot cannot decrypt any messages sent to it. Giving an error that it cannot find the room key. Using v0.6.6 of matrix-bot-sdk. Bot code below... Works fine for messages in unencrypted room. Please let me know if you need any further info. Thanks!

To Reproduce Steps to reproduce the behavior:

  1. On receipt of any encrypted messages throws error that cannot find room key

Expected behavior Should decrypt message

Log snippet

MatrixClientLite Decryption error on !uRosSrKTPVJbRRqZXp:[-----------].com$0ZYqIDhq1V3YKCwZxWueT6ESIijJCfdhybwCo8KKzPo [Error: Can't find the room key to decrypt the event] {
  code: 'GenericFailure'
}

Additional context Here is bot code. Only minimally changed from example.

import express, { Express, Request, Response , Application } from 'express';
import dotenv from 'dotenv';
dotenv.config({ path: `.env.${process.env.NODE_ENV}`});
import { MatrixClient, SimpleFsStorageProvider, AutojoinRoomsMixin, RustSdkCryptoStorageProvider} from 'matrix-bot-sdk';

const homeserverUrl = process.env.MATRIX_BASE_URL || "";
const accessToken = process.env.BOT_TOKEN || "";
const storage = new SimpleFsStorageProvider("catbot.json");
const cryptoProvider = new RustSdkCryptoStorageProvider("./crypt");
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("meow! catBot started!"));

// This is the command handler we registered a few lines up
async function handleCommand(roomId: string, event: any) {
  console.log(event);
  console.log(roomId);

  // 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
  console.log("here");

  await client.replyNotice(roomId, event, "Hello world!");
}

const app: Application = express();
const port = process.env.PORT || 5508;

app.get('/', (req: Request, res: Response) => {  
  res.json( {meow: 'meow! catbot is a-ok!',});
});

app.listen(port, () => {
  console.log(`meow! catbot is listening at http://localhost:${port}`);
});
turt2live commented 1 year ago

If the access token for the bot was retrieved with something like Element, it will need a new one generated from the login API manually.

mwinterstorm commented 1 year ago

Cheers. That will be it. Thanks for the quick response!

mwinterstorm commented 1 year ago

Confirmed working - thanks for the help! My issue was I had disabled password login (as using SSO), re enabling password login and using the login here to login and get access token works.