#!/usr/bin/env python3 """A Chatwoot agent bot that answers with exe.dev's LLM gateway. Chatwoot sends agent bot events here (https://www.chatwoot.com/docs/product/others/agent-bots). For each new customer message in a conversation the bot still owns (status "pending"), it asks the model for a reply, using the instructions in BOT_INSTRUCTIONS and what was said so far, and posts it. When the model answers HANDOFF, or the customer asks for a person, the bot says so and opens the conversation for your human agents. No API key: the model is reached through https://llm.int.exe.xyz (https://exe.dev/docs/integrations-llm). Listen only on the Compose network, never on a public port. Standard library only. CHATWOOT_URL Chatwoot's internal address (default http://rails:3000) CHATWOOT_BOT_TOKEN the agent bot's access token CHATWOOT_BOT_SECRET optional: the bot's secret, to check each request's signature LLM_URL chat completions URL (default https://llm.int.exe.xyz/v1/chat/completions) LLM_MODEL model (default gpt-5.4-mini) BOT_INSTRUCTIONS file with your instructions (default /bot/instructions.md) BOT_PORT port (default 5005) """ import hashlib, hmac, json, os, re, sys, threading, time, urllib.request from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer CHATWOOT = os.environ.get("CHATWOOT_URL", "http://rails:3000").rstrip("/") TOKEN = os.environ["CHATWOOT_BOT_TOKEN"] SECRET = os.environ.get("CHATWOOT_BOT_SECRET", "") LLM_URL = os.environ.get("LLM_URL", "https://llm.int.exe.xyz/v1/chat/completions") MODEL = os.environ.get("LLM_MODEL", "gpt-5.4-mini") INSTRUCTIONS = os.environ.get("BOT_INSTRUCTIONS", "/bot/instructions.md") PORT = int(os.environ.get("BOT_PORT", "5005")) HUMAN = re.compile(r"\b(human|person|agent|someone real|real person)\b", re.I) RULES = """ Answer in the customer's language, in a few short sentences. If you can't answer from these instructions, or the customer wants a person, reply with exactly HANDOFF and nothing else.""" def log(*args): print(*args, file=sys.stderr, flush=True) def call(method, url, body=None, headers=None, timeout=60): data = json.dumps(body).encode() if body is not None else None req = urllib.request.Request(url, data=data, method=method, headers={"Content-Type": "application/json", **(headers or {})}) with urllib.request.urlopen(req, timeout=timeout) as r: return json.loads(r.read() or b"null") def chatwoot(method, path, body=None): return call(method, CHATWOOT + path, body, {"api_access_token": TOKEN}) # Bots can't read a conversation's history through the API, so the bot # remembers what was said while it owned each conversation (in memory: # a restart forgets it, which only costs the model some context). HISTORY, LOCK = {}, threading.Lock() def remember(conv, role, content): with LOCK: HISTORY.setdefault(conv, []).append({"role": role, "content": content}) HISTORY[conv] = HISTORY[conv][-20:] return list(HISTORY[conv]) def answer(account, conv, text): messages = remember(conv, "user", text) try: with open(INSTRUCTIONS) as f: system = f.read() + RULES except OSError: system = "You are a helpful support assistant." + RULES if HUMAN.search(text): reply = "HANDOFF" else: r = call("POST", LLM_URL, {"model": MODEL, "messages": [{"role": "system", "content": system}] + messages}, timeout=120) reply = (r["choices"][0]["message"]["content"] or "").strip() or "HANDOFF" base = f"/api/v1/accounts/{account}/conversations/{conv}" if reply == "HANDOFF": chatwoot("POST", base + "/messages", {"content": "Let me get a person to help. Someone will reply here soon.", "message_type": "outgoing"}) chatwoot("POST", base + "/toggle_status", {"status": "open"}) log(f"conversation {conv}: handed off") else: chatwoot("POST", base + "/messages", {"content": reply, "message_type": "outgoing"}) remember(conv, "assistant", reply) log(f"conversation {conv}: answered") def signed(headers, body): """Chatwoot signs sha256=HMAC(secret, ".").""" ts = headers.get("X-Chatwoot-Timestamp", "") sig = headers.get("X-Chatwoot-Signature", "") if not ts.isdigit() or abs(time.time() - int(ts)) > 300: return False want = hmac.new(SECRET.encode(), ts.encode() + b"." + body, hashlib.sha256).hexdigest() return hmac.compare_digest(sig, "sha256=" + want) class Handler(BaseHTTPRequestHandler): def do_POST(self): body = self.rfile.read(int(self.headers.get("Content-Length") or 0)) if SECRET and not signed(self.headers, body): self.send_response(401) self.end_headers() return self.send_response(200) self.end_headers() try: ev = json.loads(body) except ValueError: return conv = ev.get("conversation") or {} if (ev.get("event") == "message_created" and ev.get("message_type") == "incoming" and conv.get("status") == "pending" and ev.get("content")): account = ev["account"]["id"] threading.Thread(target=self.safe_answer, args=(account, conv["id"], ev["content"])).start() @staticmethod def safe_answer(account, conv, text): try: answer(account, conv, text) except Exception as e: log(f"conversation {conv}: {e}") def log_message(self, *args): pass if __name__ == "__main__": log(f"chatwoot bot on :{PORT}, model {MODEL}") ThreadingHTTPServer(("0.0.0.0", PORT), Handler).serve_forever()