Contents

Bots and apps

The same agent in your own app, a Telegram, Discord or Slack bot, or a workflow, over the API from Studio: what to build and what is left to you.

The widget is one way to put your agent in front of people. From Studio, the same agent answers over the API: in your own app, in a bot on Telegram, Discord or Slack, or as a step in a workflow. It answers from the same sources and instructions. An answer costs credits exactly as one in the widget does, and the conversation appears in Activity under the channel api.

ChatterLab has no ready-made channel for Telegram, Discord or Slack. You run the bot on a server of yours, and it passes each message on to the agent. The Slack integration, which is still being tested, tells a channel of yours about new leads, tickets and answers that fell short. It does not answer questions there.

What you need

  • Studio or higher. On Free and Starter no key can be made, and a key is refused with api_not_in_plan while the workspace is on a plan below Studio.
  • A key under Workspace settings, Developers, API keys. Choose Chat under What it may do, and the one agent under Which agents. Such a key asks questions and does nothing else, so a leaked key cannot read conversations or change the agent.
  • A server that holds the key. Never put the key in an app people install or a page they open: anyone could read it there and spend your credits. A mobile app sends its questions to your server, and your server asks the agent.

One question, one answer

Every bot comes down to one call: take the message, send it to POST /agents/{id}/chat, and send back answer.

// Node 18 or later. CHATTERLAB_KEY holds a key with the Chat scope.
const API = "https://chatterlab.ai/api/v1";

async function ask(agentId, message, conversationId, userId) {
  const res = await fetch(`${API}/agents/${agentId}/chat`, {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.CHATTERLAB_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ message, conversationId, userId }),
  });
  const body = await res.json();
  if (!res.ok) throw new Error(body.error.code);
  return body.data; // answer, conversationId, confidence, grounding, sources, actions
}
  • Keep the conversation. The first reply carries a conversationId. Store it per chat and per person, and send it with the next message: the agent then remembers the last 30 messages. Without it, every message starts a new conversation.
  • Say who asks. userId takes letters, digits and dashes, up to 64, such as tg-123456789. Activity shows it beside the conversation, so that you can tell people apart. Never put an e-mail address in it.
  • Language. The agent answers in the language of the question. Send language, such as "nl", for a bot that should always answer in one language.
  • Markdown. answer is written in Markdown, with lists, bold and links. Telegram and Slack each have a markup of their own, so convert it, or send plain text with the marks taken out.

The API reference lists every field, the reply and how to stream it.

Telegram, Discord and Slack

PlatformWhere a message arrivesIts textuserId fromKeep conversationId per chat and person
Telegramthe update Telegram posts to your bot's webhookmessage.textmessage.from.idmessage.chat.id and message.from.id
Discordthe messageCreate event in discord.jsmessage.contentmessage.author.idmessage.channelId and message.author.id
Slackthe message event of the Events API, or app.message in Boltevent.textevent.userevent.channel and event.user

A Telegram bot behind a webhook, using ask from above:

// Point Telegram at it once:
// https://api.telegram.org/bot<token>/setWebhook?url=https://your.server/telegram&secret_token=<secret>
import express from "express";

const app = express();
app.use(express.json());
const conversations = new Map(); // "chat:person" -> conversationId; keep it in a database in production

app.post("/telegram", async (req, res) => {
  // Only Telegram knows the secret, so nobody else can make the bot spend credits.
  const secret = process.env.TELEGRAM_SECRET;
  if (!secret || req.get("X-Telegram-Bot-Api-Secret-Token") !== secret) return res.sendStatus(401);
  res.sendStatus(200);
  const msg = req.body.message;
  if (!msg?.text || !msg.from) return;
  const key = `${msg.chat.id}:${msg.from.id}`; // one conversation per person, in a group too
  let text;
  try {
    const reply = await ask(process.env.AGENT_ID, msg.text, conversations.get(key), `tg-${msg.from.id}`);
    conversations.set(key, reply.conversationId);
    text = reply.answer;
  } catch {
    text = "I cannot answer right now. You can reach us at hello@example.com.";
  }
  // A Telegram message holds up to 4,096 characters.
  for (let i = 0; i < text.length; i += 4096) {
    await fetch(`https://api.telegram.org/bot${process.env.TELEGRAM_TOKEN}/sendMessage`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ chat_id: msg.chat.id, text: text.slice(i, i + 4096) }),
    }).catch((error) => console.error(error)); // a failed send must not stop the server
  }
});

app.listen(3000);

In a group, one conversation per chat would mix everybody's questions under the first asker's userId, which is why the example keeps one per chat and person.

What the platforms ask of you:

  • A Discord bot reads messages that do not mention it only with the Message Content intent, switched on in Discord's Developer Portal and asked for in the client (GatewayIntentBits.MessageContent, beside Guilds and GuildMessages).
  • A Discord bot also receives its own replies as messageCreate. Skip a message whose message.author.bot is true, or the bot answers itself and spends credits each time.
  • A message takes up to 2,000 characters on Discord and 4,096 on Telegram, so split a longer answer, as the example does.

When the agent cannot answer

  • A 402 is a matter of the plan or a limit: the credits have run out (credits_exhausted), the agent reached the limit its agency set (credit_limit_reached), the agent is beyond what the plan covers (agent_paused), the plan does not include the agent's model (model_not_allowed) or the plan is below Studio (api_not_in_plan). Retrying does not help. Reply with a line of your own, such as where to reach your team.
  • A 429 with rate_limited: the key asked more than 120 times in a minute. The limit is per key, so everyone who uses the bot shares it. Wait the seconds in Retry-After.
  • A low confidence, or a grounding of unsupported: your content did not cover the question. A bot can offer a person instead of the answer. What they mean

What the widget does and a bot does not

  • Telling people it is an AI. The widget says so. In your own bot or app that is your job, in the bot's description or its first message. The AI Act
  • The contact form and the ticket card. The widget shows them; the API does not. Buttons do come along: when you do not stream, actions lists the actions the agent chose, each with a label and a url, for your bot to show as links.
  • Files. A question over the API is text. A photo or document somebody sends your bot is not passed on to the agent.

Workflows

A workflow asks the agent the same way.

  • In Zapier, the Ask Agent action of our Zapier integration does it without code.
  • In n8n, Make or any tool that can send an HTTP request, send a POST to /agents/{id}/chat with the key in the Authorization header and the question as message.

The other way round, webhooks send what happens in the widget and the other channels to your systems as it happens: a new lead, a ticket, an ended conversation, every answer. They come with Studio and up too. A conversation over the API sends only answer.flagged, the event for a weak answer: your code holds every answer already, and a Zap that asked again on each one would never stop. To have an AI assistant set up and improve your agents, rather than answer people, use MCP.