Skip to content
This repository has been archived by the owner on Sep 15, 2021. It is now read-only.

vsenko/matrix-js-basic-bot

Repository files navigation

matrix-js-basic-bot

Note: this code is outdated and, probably, broken!

Build Status

A basic bot for matrix.org

It can be used to run basic experiments in chat bot development without going deep in matrix protocol details.

Bot is powered by matrix-js-sdk.

Features:

  • on first startup authenticates and obtains access token and device id, stores them to use on subsequent runs
  • automatically joins rooms on invitation
  • automatically leaves empty rooms
  • supports e2e encryption
  • automatically verifies all users devices in the room on an attempt to send a message to the e2e enabled room

Usage

npm install matrix-js-basic-bot
const BasicMatrixBot = require('matrix-js-basic-bot');
const md = require('markdown-it')();

async function onMessage(content, sender) {
  try {
    const output = `**Echo**: ${content.body}`;
    await this.sendHtmlNotice(sender.roomId, output, md.render(output));
  } catch (error) {
    console.log(error);
  }
}

async function run() {
  await servers.connect(serversCredentials);

  const bot = new BasicMatrixBot('myCoolBot', 'StrongPassword', 'https://matrix.org', './localstorage');

  bot.on('error', (error) => {
    console.log(error);
  });

  bot.on('message', onMessage);
  bot.on('e2eMessage', onMessage);

  await bot.start();
}

run()
  .catch((error) => {
    console.log('Unhandled error!');
    console.log(error);
    process.exit(1);
  });

See more examples in examples.

API

docs

License

MIT

About

A basic bot for matrix.org

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published