0.0.2 • Published 6 years ago

matrix-js-basic-bot v0.0.2

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

matrix-js-basic-bot

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.sendNotice(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