Skip to main content
Matrix integration provides end-to-end encrypted messaging through the Matrix protocol, compatible with Element and other Matrix clients.

Features

  • ✅ End-to-end encryption (E2EE) support
  • ✅ Matrix homeserver compatibility
  • ✅ User ID-based access control
  • ✅ Group chat with configurable policies
  • ✅ Media attachment support with size limits
  • ✅ Room mention handling

Prerequisites

Install Matrix dependencies:
pip install nanobot-ai[matrix]

Setup

1

Create or choose Matrix account

  1. Create a dedicated Matrix account for your bot
  2. You can use any homeserver (e.g., matrix.org, or your own)
  3. Example: @nanobot:matrix.org
  4. Confirm you can log in with Element client
2

Get credentials

You need three pieces of information:
  • userId: Your Matrix user ID (e.g., @nanobot:matrix.org)
  • accessToken: Authentication token
  • deviceId: Device identifier (recommended for session persistence)
Option 1: Using Element Web
  1. Log in to Element Web
  2. Settings → Help & About
  3. Scroll to bottom → Access Token
  4. Click to reveal token (starts with syt_)
Option 2: Using homeserver API
curl -X POST "https://matrix.org/_matrix/client/v3/login" \
  -H "Content-Type: application/json" \
  -d '{
    "type": "m.login.password",
    "identifier": {"type": "m.id.user", "user": "nanobot"},
    "password": "your-password",
    "device_id": "NANOBOT01"
  }'
3

Get your User ID

Your Matrix user ID is shown in Element settings:
  1. Open Element
  2. Click your profile
  3. Copy your User ID (format: @youruser:matrix.org)
4

Configure nanobot

Edit ~/.nanobot/config.json:
{
  "channels": {
    "matrix": {
      "enabled": true,
      "homeserver": "https://matrix.org",
      "userId": "@nanobot:matrix.org",
      "accessToken": "syt_xxx",
      "deviceId": "NANOBOT01",
      "e2eeEnabled": true,
      "allowFrom": ["@your_user:matrix.org"],
      "groupPolicy": "open",
      "groupAllowFrom": [],
      "allowRoomMentions": false,
      "maxMediaBytes": 20971520
    }
  }
}
5

Start the gateway

nanobot gateway
You should see:
Matrix channel connected
E2EE enabled
6

Test the connection

  1. Open Element and find your bot user
  2. Start a direct message
  3. Send a message
  4. Your bot should respond!

Configuration Options

enabled
boolean
required
Enable the Matrix channel
homeserver
string
required
Matrix homeserver URLExamples:
  • https://matrix.org
  • https://your-homeserver.com
userId
string
required
Matrix user ID for the bot (format: @username:homeserver)
accessToken
string
required
Access token for authentication (format: syt_xxx)
Keep this private. Never commit to version control.
deviceId
string
required
Device identifier (recommended for session persistence)
Keep a persistent matrix-store and stable deviceId — encrypted session state is lost if these change across restarts.
e2eeEnabled
boolean
default:"true"
Enable end-to-end encryption supportSet to false for plaintext-only mode (not recommended).
allowFrom
array
default:"[]"
List of Matrix user IDs allowed to interact with the botExamples:
  • Single user: ["@alice:matrix.org"]
  • Multiple: ["@alice:matrix.org", "@bob:matrix.org"]
  • All users: [] (empty array)
groupPolicy
string
default:"open"
Controls how the bot responds in rooms:
  • "open" (default) — Respond to all messages
  • "mention" — Only respond when mentioned
  • "allowlist" — Only respond in specific rooms
groupAllowFrom
array
default:"[]"
Room IDs to allow (used when groupPolicy is "allowlist")Example: ["!roomid1:matrix.org", "!roomid2:matrix.org"]
allowRoomMentions
boolean
default:"false"
Accept @room mentions in mention modeIf true, bot responds to @room mentions in addition to direct mentions.
maxMediaBytes
number
default:"20971520"
Maximum attachment size in bytes (default: 20MB)Set to 0 to block all media uploads.

End-to-End Encryption

Matrix supports end-to-end encryption for secure messaging.

Enabled by default

{
  "e2eeEnabled": true
}
  • Bot can send/receive encrypted messages
  • Requires matrix-store for session state
  • Supports device verification

Plaintext only

{
  "e2eeEnabled": false
}
  • Bot only works in unencrypted rooms
  • Simpler setup, no encryption overhead
  • Not recommended for sensitive conversations

Important notes

  • Keep deviceId stable across restarts
  • Keep matrix-store directory persistent
  • Lost encryption state = can’t decrypt old messages
  • Verify devices in Element for trusted communication

Group Chat Policies

Open mode (default)

{
  "groupPolicy": "open"
}
Bot responds to all messages in all rooms.

Mention mode

{
  "groupPolicy": "mention",
  "allowRoomMentions": false
}
Bot only responds when directly mentioned (e.g., @nanobot:matrix.org).With @room support:
{
  "groupPolicy": "mention",
  "allowRoomMentions": true
}
Bot responds to both direct mentions and @room.

Allowlist mode

{
  "groupPolicy": "allowlist",
  "groupAllowFrom": ["!abc123:matrix.org"]
}
Bot only responds in specified rooms.

Troubleshooting

Error: ModuleNotFoundError: No module named 'nio'Solution: Install Matrix dependencies
pip install nanobot-ai[matrix]
  1. Check user ID is in allowFrom (or use empty array for all)
  2. Check groupPolicy settings for room behavior
  3. Verify access token is valid
  4. Check homeserver is reachable
  5. Look for errors in logs: nanobot gateway -v
If you see encryption-related errors:
  1. Lost session state:
    • Check matrix-store directory exists and is writable
    • Ensure deviceId hasn’t changed
    • May need to re-verify device in Element
  2. Unable to decrypt:
    • Verify bot account in Element
    • Check E2EE is enabled: "e2eeEnabled": true
    • Clear encryption state and restart
  3. Start fresh:
    rm -rf ~/.nanobot/matrix-store
    nanobot gateway
    
    Then re-verify device in Element.
  1. Verify homeserver URL is correct (include https://)
  2. Check homeserver is online and accessible
  3. Verify access token is valid (not expired/revoked)
  4. Check firewall allows outbound HTTPS
  5. Try alternative homeserver if using matrix.org (can be slow)
  1. Check file size is under maxMediaBytes limit
  2. Verify homeserver allows media uploads
  3. Check bot has permission to upload to room
  4. Increase limit if needed:
    {
      "maxMediaBytes": 52428800
    }
    
    (50MB in bytes)
  1. Verify gateway is running: nanobot gateway
  2. Check access token hasn’t expired
  3. Verify homeserver status
  4. Check network connectivity
  5. Try logging out and back in to get new token

Complete Example

Full configuration with all options:
{
  "channels": {
    "matrix": {
      "enabled": true,
      "homeserver": "https://matrix.org",
      "userId": "@nanobot:matrix.org",
      "accessToken": "syt_abcdefghijklmnopqrstuvwxyz123456",
      "deviceId": "NANOBOT01",
      "e2eeEnabled": true,
      "allowFrom": ["@alice:matrix.org", "@bob:matrix.org"],
      "groupPolicy": "mention",
      "groupAllowFrom": [],
      "allowRoomMentions": false,
      "maxMediaBytes": 20971520
    }
  }
}

Configuration Overview

Learn about channel configuration

Security Settings

Set up access control

Build docs developers (and LLMs) love