Matrix Based Selfhosted Chatbot

Introduction

I deployed a selfhosted chatbot in order to assist me on handling local workflows, while I am remote from home (eg.: office, coffeshop, library). I found that matrix chat app and implementing a bot on top of it is currently the most private, secure and robust solution. Because Matrix is prefered communications application for government-grade institutions, such that France government decided to selfhost their own Matrix instances and replace usage of WhatsApp or Telegram for official purposes. There are many services I selfhost and occasionally I need to reach them remotely. Some of those are job scheduler, bookmark/video archiver, backup system, etc. In this post I document my process of deploying * a Matrix server and * its bot on my home server.

(1) write compose file

version: '3.3'

services:
  app:
    image: matrixdotorg/synapse
    restart: unless-stopped
    ports:
      - [external-port]:[internal-port]
    volumes:
      - /path/on/host/machine/for/matrix/data:/data

(2) generate new config file

docker run -it --rm -v /path/on/host/machine/for/matrix/data:/data \
	-e SYNAPSE_SERVER_NAME=matrix.DOMAIN_NAME.com \
	-e SYNAPSE_REPORT_STATS=yes \
	matrixdotorg/synapse:latest generate
  • Running this command genenrates homeserver.yml file for my instance.
  • Update DOMAIN_NAME with my own address with sub-domain.
  • Finally you should see generated config files in your data dir:

matrix-image-1.png

(3) expose service to remote

  • create new site via nginx
sudo nano /etc/nginx/sites-available/matrix.conf
  • write reverse proxy configurations
server {
  server_name SUB_DOMAIN_NAME.DOMAIN_NAME.com;
  location / {
    proxy_pass http://localhost:[external-port];
    }
}
  • register new site and enable it
sudo ln -s /etc/nginx/sites-available/matrix.conf /etc/nginx/sites-enabled/matrix.conf

(4) create certificate with certbot

  • call certbot of Let’s Encrypt to generate a new certificate
sudo certbot --nginx -d SUB_DOMAIN_NAME.DOMAIN_NAME.com

(5) connect to your instance via client

  • Once your matrix synapse server is up and running, you should connect to it via a client
  • Element seems appropriate as a decentralized client.
  • Navigate to Element on your Browser and click: Sign In -> Homeserver -> Edit
    • matrix-image-2.png | 500

(6) skip federation configuration

  • Federation enables one’s selfhosted instance to connect other server instances and chat with users signed on those instances.
  • Since my own chatbot assistant does not require interaction with users on other instances, here I am not implementing federation.

(7) create user

  • Create a user to sign in through client.
  • User registration on the home server instance should be enabled. Add the line below into homeserver.yaml config file:
enable_registration: true
  • Enabling registering without verification is not secure. Matrix will warn about it and recommends implementing mail verification.
enable_registration_without_verification: false

matrix-image-3.png

(8) simple chatbot service

  • There exists templates to build chatbot on Matrix. I prefered using eno-bot to build on top of it.
  • write compose file:
version: '3'

services:
  matrix-eno-bot:
    container_name: matrix-eno-bot
    image: 'matrix-eno-bot:latest'
    build: ./host/path/to/repo/dir
    restart: unless-stopped
    volumes:
      - ./path/to/persistent/host/dir:/bot
  • Default usage is clearly explained on the README.md of maintainers repository: matrix-image-4.png