Tech

Getting started with Matrix Bots on NodeJS

by
published on

It took me a couple of hours to get a funtioning Matrix bot, mostly due to not understanding the ecosystem. This will just let you skip a couple of hours and get productive with Matrix quickly.

I am not a Matrix expert, I've less than a day's experience with it. I just want to save some people the pain/time of learning the same things again. There may well be incorrect information here, but this was enough knowledge to get me productive.

  • There are lots of Matrix servers, hosted by different people
  • Some are hosted by matrix.org, some aren't
  • There are lots of Matrix clients
  • Some are made/maintained by matrix.org, some aren't
  • The easiest to get started with is Riot: https://riot.im/app
  • In Matrix, there are Users, Rooms, and Messages
  • Users can create, join, leave, invite others to, and modify rooms (alike most chat systems)
  • Users can send messages to Rooms
  • Users can be members of multiple rooms simultaneously
  • Users are authenticated with a password or authToken 

Setting up a bot user

To create a Bot, we need to choose a server/group of servers to run on.
We're using matrix.org for this.

  1. Open https://riot.im/app
  2. Create a room for our bot to live in (for now)
  3. When prompted, create a username for your bot
  4. Choose a room name
  5. Make a note of the roomId in the URL (this includes the '!' and the ':matrix.org')
  6. Click 'Settings' in the bottom left corner
  7. Scroll down to "ADVANCED"
  8. Make note of your bot's username (including the '@' and ':matrix.org')
  9. Make note of your bot's Access Token
  10. Set a password on the account so it doesn't get deleted
  11. Make your bot

For example code, see https://github.com/ISNIT0/matrix-bot-template

If you have any questions or suggested updates, contact me on Twitter @jreeve0