Skip to content
BotServBotServ
Rocket.ChatBotAI BotOllamaSelf-HostingHubotOpen Source

Rocket.Chat Bot with Local AI

Build a Rocket.Chat bot with Ollama: self-hosted chat platform, Hubot, Realtime API, Omnichannel, Docker setup.

S

schutzgeist

6 min read
Rocket.Chat Bot with Local AI

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.chat image 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”).

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 bot role, 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

AspectMattermostRocket.Chat
StackGo + PostgresNode.js + MongoDB
OmnichannelNoYes (website chat, WhatsApp, email)
Bot APIREST + WebSocket, cleanDDP/WebSocket, clunky + Hubot
Resource usageLeanMongoDB consumes more RAM
Best forInternal teamsCustomer service + Omnichannel

Further Reading

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?

Community Edition (free, open source) is enough for bots and basic Livechat. Some Omnichannel extensions and Enterprise features require a license.

Hubot or Realtime API?

Use Hubot for quick command bots (!ai question). Use the Realtime API (DDP/WebSocket) for full control: threads, reactions, Omnichannel events. Hubot is faster to set up; the API is more powerful.

Why does Rocket.Chat need MongoDB?

Rocket.Chat runs on Meteor/Node.js and stores everything in MongoDB, using Replica Sets for change streams (real-time updates). This makes the stack heavier than Mattermost (Postgres).

Can I use the bot as a website Livechat?

Yes, that’s the strongest use case. Enable Omnichannel Livechat, embed the widget snippet on your website, have your bot subscribe to Livechat rooms and respond with local AI. No third parties needed.

Rocket.Chat or Mattermost?

Choose Rocket.Chat for customer service and Omnichannel (website, WhatsApp, email from one interface). Choose Mattermost for internal team chat: leaner, Postgres instead of Mongo, cleaner bot API.

Can Rocket.Chat integrate WhatsApp?

Yes, via Omnichannel integration with the WhatsApp Business API (requires a Meta account). Your AI bot can then answer customer inquiries on WhatsApp, running Ollama locally.

What resources do I need?

Rocket.Chat ~1 GB RAM, MongoDB ~1 GB, Ollama ~6-8 GB for an 8B model. A server with 16 GB RAM and a GPU is sufficient for small to medium teams.

Sources and Further Reading

Back to Blog
Share:

Related Posts