Rocket.Chat Bot with Local AI
What this article covers
- Building a bot on your Rocket.Chat server with Ollama.
- Two approaches: Hubot adapter (classic) or Realtime API (more flexible).
- Creating a bot user, processing WebSocket events, sending replies.
- Omnichannel features: Rocket.Chat as an AI-powered customer service platform.
- Extensions and comparison to Mattermost.
Introduction
Rocket.Chat is the second major self-hostable team chat platform alongside Mattermost: open source (Community Edition), with a distinctive feature: Omnichannel. Rocket.Chat can integrate WhatsApp Business, Telegram, email, and live chat from your website as channels, so a bot can respond across multiple channels simultaneously.
For AI bots, there are two paths: the classic Hubot adapter (GitHub’s original bot standard) or the Realtime API (DDP/WebSocket) with custom code. We’ll cover both.
Typical use cases
- Customer service AI: Use Omnichannel to funnel website chat, WhatsApp, and email into a single queue, with AI handling first-level responses.
- Internal team bot: Like Mattermost: knowledge bot, alerts, onboarding.
- Live chat on your website: Rocket.Chat Livechat widget plus Ollama equals self-hosted AI chat for your site.
- Community platform: Public channels with AI-assisted moderation.
Prerequisites
- Rocket.Chat server (Docker:
rocket.chatimage plus MongoDB) - Admin access
- Ollama with a model
- Node.js (Hubot) or Python (Realtime API)
Step 1: Rocket.Chat server (Docker)
services:
rocketchat:
image: rocket.chat:latest
environment:
MONGO_URL: mongodb://mongodb:27017/rocketchat?replicaSet=rs0
MONGO_OPLOG_URL: mongodb://mongodb:27017/local?replicaSet=rs0
ROOT_URL: https://chat.deine-domain.de
PORT: 3000
DEPLOY_METHOD: docker
depends_on: [mongodb]
ports: ["3000:3000"]
restart: always
mongodb:
image: mongo:7
command: mongod --replSet rs0 --oplogSize 128
volumes: [mongo_data:/data/db]
restart: always
volumes:
mongo_data:
Note: MongoDB requires replica set mode (for change streams): add --replSet rs0 as shown above, then run mongosh --eval "rs.initiate()" once.
Step 2: Create a bot user
Administration → Users → New User:
- Username:
ki-bot, Display Name “KI Assistant” - Assign the bot role (required)
- Set email and password
The bot user logs in with username/password or a Personal Access Token (Administration → Accounts → enable “Allow Personal Access Tokens”).
Approach A: Realtime API with Python (recommended)
Rocket.Chat’s real-time API is DDP (Distributed Data Protocol), a WebSocket layer. You can use the rocketchat-API library plus custom streaming, or call WebSocket directly. Easier options include libraries like rocketchat.py; here’s the direct DDP principle:
import asyncio
import json
import os
import websockets
import ollama
from dotenv import load_dotenv
load_dotenv()
RC_URL = os.environ["RC_URL"] # wss://chat.deine-domain.de/websocket
RC_USER = os.environ["RC_USER"]
RC_PASS = os.environ["RC_PASS"]
MODEL = os.environ.get("OLLAMA_MODEL", "llama3.1:8b")
ollama_client = ollama.Client(host=os.environ["OLLAMA_URL"])
seq = 0
def msg(method, params, msg_id=None):
global seq
seq += 1
return json.dumps({
"msg": "method", "method": method, "params": params,
"id": msg_id or str(seq)
})
async def main():
async with websockets.connect(RC_URL) as ws:
# 1. Connect
await ws.send(json.dumps({"msg": "connect",
"version": "1", "support": ["1"]}))
await ws.recv()
# 2. Login (Password)
await ws.send(msg("login", [{
"user": {"username": RC_USER},
"password": {"digest": sha256(RC_PASS), "algorithm": "sha-256"}
}], "login"))
login_resp = json.loads(await ws.recv())
auth_token = login_resp["result"]["token"]
# 3. Subscribe to new messages (stream-room-messages)
await ws.send(json.dumps({
"msg": "sub", "id": "sub1", "name": "stream-room-messages",
"params": ["__my_messages__", False]
}))
async for raw in ws:
evt = json.loads(raw)
if evt.get("msg") == "changed":
for arg in evt["fields"]["args"]:
m = arg if isinstance(arg, dict) else arg[0]
if m.get("u", {}).get("username") == RC_USER:
continue
text = m.get("msg", "")
if text.startswith("!ki"):
prompt = text[3:].strip()
resp = ollama_client.chat(model=MODEL, messages=[
{"role": "system", "content": "Kurz antworten, Deutsch."},
{"role": "user", "content": prompt}])
await ws.send(msg("sendMessage", [{
"rid": m["rid"],
"msg": resp["message"]["content"][:16000],
}]))
from hashlib import sha256
def sha256(s): return sha256(s.encode()).hexdigest()
asyncio.run(main())
The DDP protocol is somewhat verbose; for production, use a library like python-rocketchat or rocket-python.
Approach B: Hubot (classic, quick start)
Hubot is GitHub’s original chatbot standard; Rocket.Chat has an official adapter:
npm install -g yo generator-hubot
mkdir ki-bot && cd ki-bot
yo hubot --adapter rocketchat
.env:
ROCKETCHAT_URL=http://localhost:3000
ROCKETCHAT_USER=ki-bot
ROCKETCHAT_PASSWORD=bot-passwort
ROCKETCHAT_ROOM=general
LISTEN_ON_ALL_PUBLIC=true
RESPOND_TO_DM=true
scripts/ki.coffee (Hubot uses CoffeeScript) or simpler: an external script that calls Ollama. Hubot is handy if you want quick hubot ki <question>-style commands; for more complex bots, the Realtime API offers more flexibility.
Extensions
Omnichannel: Bot answers website live chat
The killer use case: Administration → Omnichannel → enable Livechat → place the widget snippet on your website. Visitors write in the widget, land as a room in Rocket.Chat, and your bot (Realtime API) responds with local AI. Self-hosted AI chat without external vendors.
// Livechat widget on your website
<script>
(function(w,d,s,u){ /* Rocket.Chat Livechat snippet */
})(window, document, 'script', 'https://chat.deine-domain.de/livechat');
</script>
RAG and moderation
Same patterns as Mattermost: embeddings to Qdrant, inject context into prompts, spam classification for public channels.
Triggers for agent workflows
Bot command !aufgabe <description> triggers an n8n workflow, result returns to the channel. Connects your bot with n8n automation.
Security
- Minimal bot role: The bot user gets only the
botrole, never admin. - Tokens: Use Personal Access Token instead of password; revokable without a password reset.
- HTTPS: In production, place TLS behind a reverse proxy (Caddy/Nginx).
- Channel isolation: Invite the bot only to channels it should access.
Common Pitfalls
- MongoDB Replica Set: Rocket.Chat won’t start correctly without a ReplSet. Don’t forget
rs.initiate(). - DDP is complex: If your custom WebSocket code struggles, use a library or switch to a Hubot adapter.
- Bot replies to itself: Check the username against
RC_USER(see code above). - Community Edition limits: Some Enterprise features (e.g., advanced Omnichannel options) require a license. Core Livechat is free.
Mattermost vs. Rocket.Chat at a Glance
| Aspect | Mattermost | Rocket.Chat |
|---|---|---|
| Stack | Go + Postgres | Node.js + MongoDB |
| Omnichannel | No | Yes (website chat, WhatsApp, email) |
| Bot API | REST + WebSocket, clean | DDP/WebSocket, clunky + Hubot |
| Resource usage | Lean | MongoDB consumes more RAM |
| Best for | Internal teams | Customer service + Omnichannel |
Further Reading
- IRC-Coding.de: In-depth programming tutorials on WebSocket, Node.js, and bot frameworks.
- Mattermost Bot: The lighter alternative.
- Nextcloud Talk Bot: If you’re already using Nextcloud.
- Chatwoot: Dedicated customer service platform.
- Docker: Deployment basics.
Key Takeaways:
- Rocket.Chat is a self-hosted chat platform with Omnichannel support (website, WhatsApp, email).
- Build bots via the Realtime API (DDP/WebSocket) or a classic Hubot adapter.
- Killer feature: live chat widget for your own website plus local AI equals self-hosted support chat.
- For pure team communication, Mattermost is leaner; for customer service, Rocket.Chat is stronger.
- MongoDB with Replica Set is required, making setup more involved.
FAQ
Which Rocket.Chat Edition?
Hubot or Realtime API?
Why does Rocket.Chat need MongoDB?
Can I use the bot as a website Livechat?
Rocket.Chat or Mattermost?
Can Rocket.Chat integrate WhatsApp?
What resources do I need?
Sources and Further Reading
- Rocket.Chat: Official website.
- Rocket.Chat Realtime API: DDP documentation.
- Hubot Rocket.Chat Adapter: Hubot integration.
- IRC-Coding.de: Programming tutorials.


