Open the console

Quickstart

Two steps: first your bot replies in your terminal, in a few minutes and with no account; then it replies in a real chat.

You need:

  • Node.js 22.13 or newer (node --version);
  • a terminal on your computer;
  • for the second step, a Peachy account. Anyone can make a development bot: only you and up to 10 testers can use it, and no verification is needed (see Who can create bots).

The template repository and @peachysocial/bot on npm are available at launch.

1. Get the template#

git clone https://github.com/peachysocial/peachy-bot-template my-bot
cd my-bot
npm install

The template is a working bot: @yourbot /pick pizza, tacos, sushi answers the chat with one of the choices. npm install brings everything it needs, the packages its key transparency checks use and werift, which carries a call's audio and video, included: there's nothing else to install.

2. Try it in your terminal#

npx @peachysocial/bot dev

dev runs bot.ts against an in-memory Peachy: no account, no keys, no network. The SDK's own routing decides which handler answers, as it will in production. Type:

maya> /pick tea, coffee
maya> /help
maya> :add
maya> :say @yourbot /pick pizza, tacos, sushi

A plain line writes in the bot's own chat. :add adds it to a group and :say writes there, and :as Theo writes as someone else. :reply replies to the bot's last reply, and :help lists the rest. Change bot.ts, run dev again, and see the difference.

dev reads .env if there is one, then calls the register(bot) that bot.ts (or src/bot.ts) exports, with the bot alone. If your bot needs something dev doesn't have, such as an API key or a list of people only a real listing names, also export dev(bot): dev and the conformance check call it instead, so it can wire in a stand-in. main.ts, which runs the real bot, never calls it.

3. Make the store key#

npx @peachysocial/bot keygen --out .peachy-store-key
cp .env.example .env

keygen --out writes a new key to a file only you can read (mode 600), and .env points at it (PEACHY_BOT_STORE_KEY_FILE=.peachy-store-key). Everything the bot keeps (its keys, the chats it's in, its recent replies, and a note of who sent each recent message and when, never its words) is encrypted with this key before it's written, and the store refuses to open without it.

Both files are in .gitignore: never commit them. Keep a copy of the key somewhere safe, since without it the store can't be opened. In production, the key goes in your host's secrets, never next to the store.

4. Create the bot in Peachy#

In the app, go to Settings › Organizations and bots › New bot (on Mac, You › Organizations and bots › New bot). On the web it's pchy.app/settings/organizations-and-bots.

  • Who can use it: You and testers. Only you and the testers you add can use it.
  • Owner: you, or an organization you're an admin of. An organization's bot is one of its accounts, counted among its 3 (its first included) once published, and Peachy reviews each account's handle and name before it wears the Verified badge (see Accounts).
  • Name and handle: what people see and type. The handle is permanent for most purposes, so pick carefully.
  • Where messages go, How long it keeps them, Privacy policy link: for the template, "Nowhere: this bot keeps no messages" is true, as long as you leave the keepFailedPayloads option off (below). See Privacy labels.

You don't need to type its commands: the bot sends the ones in bot.ts each time it starts, and those are its listing's commands.

Check I agree to the Developer Terms and tap Create. To let friends try it, add their handles under Who can use it on the bot's page (up to 10).

On the bot's page, tap Connect. Peachy shows a one-time code. Then:

npx @peachysocial/bot connect

Paste the code when it asks. You'll see:

Linked ✓ as @yourbot

and the Connect screen in Peachy says Connected. The code is spent; the bot's credentials are now in its encrypted store.

6. Start it#

npm start

7. Talk to it#

Start a new message, choose Find by handle, and type your bot's handle. The first time, Peachy shows a sheet saying where your messages go ("Messages to @yourbot go to you", the owner the listing names) and what your bot says it keeps. Agree, and send:

/pick pizza, tacos, sushi

Your bot shows as typing, and its reply appears under your message.

To use it in a group, open any group whose Bots setting is On (Chat info › Bots; On is the default) and add it: Chat info › Add a bot, or type @yourbot in the message field and choose Add @yourbot?. Leave Post on its own on (what your bot asks for starts on) so it can reply there. Then send @yourbot /pick pizza, tacos, sushi: its reply appears in the thread under your message.

A community can take only the bots its admins approve, and a company's community does from the start. Your organization's own bot is approved in each of its communities that exist then, when an admin makes it or moves it in; in a community made later, or if that didn't land, an admin approves it under Bot approval › Approve a bot, as any other bot.

8. Make it yours#

Open bot.ts. Each handler gets a message, m:

bot.command("plan", { blanks: "<where>", worksInDm: true }, async (m) => {
  await m.reply(`Let's plan ${m.args}!`, { alsoSendToChat: true });
});

bot.on("message", async (m) => {
  // In a group, only what's for us: a reply to a mention always goes.
  if (m.via === "chat" && !m.mentioned) return;
  if (m.text.length > 500) return m.reply("That's a lot! Try something shorter.");
  await m.reply(`You said ${m.text.length} characters' worth.`);
});
  • Your bot gets the messages of every member of a group who added it or said OK to it (m.via === "chat"; a first mention asks them; a member who hasn't sends it nothing from a Peachy app that shows bots, as Who sees bots says), and every message in its own chat (m.via === "dm"). Members whose community hasn't approved your bot send it nothing, mentions included; their community's admins approve it under Bot approval. m.mentioned says the message mentions your bot. m.text is the words as sent.
  • A message that starts with one of your commands (/plan Tahoe, or @yourbot /plan Tahoe in a group) runs that command's handler instead. In a group with more than one bot, a bare /plan runs on the bot added there most recently that has /plan, and @yourbot /plan on yours: m.command is its name ("plan") and m.args what follows ("Tahoe"). In your bot's own chat a command runs only when it has worksInDm, and an empty message or /help gets the list of commands.
  • m.reply(text) replies as a person would: in the message's thread in a group, inline in your bot's own chat. m.reply(text, { alsoSendToChat: true }) also sends it to the group. A reply to a message that mentions your bot always goes. Any other reply in a group needs the post permission; without it m.reply() throws GrantMissingError. A handler that runs again for the same message (after a throw, or a restart) can call reply again safely: it resolves to the reply already sent (duplicate: true) and sends nothing.
  • While a handler runs for a message to your bot, the SDK shows it typing in that chat, and stops when the reply goes. Turn that off with typing: false. People's phones say "@yourbot didn't answer" when no message from your bot arrives within 60 seconds of theirs, typing or not, so reply something short first when the answer takes longer.
  • If a handler keeps failing before it replies, the SDK tells the person "Sorry, I couldn't answer that. Try again in a moment." in your bot's own chat, and reports a dead_lettered error. In a group it sends nothing. Set your own words with the failureReply option, or null for none. A handler that runs past handlerTimeoutMs (2 minutes) fails the same way; pass m.signal to your model's client so it stops too.
  • A failed event keeps no one's words: npx @peachysocial/bot events failed lists its kind and ids, for 72 hours at most. To be able to retry one with events retry, set keepFailedPayloads: true; then your bot keeps the failed messages that long, and its label must say so.
  • m.reply(text, { files: ["chart.png"] }) sends files with the reply. chat.post() and dm.post() take files too. A file is a path, the bytes, or { data, contentType, name }.
  • bot.dm(m.from) is your bot's chat with that person, for writing to them later (a reminder): const { dm, reason } = await bot.dm(person), then dm.post(text, { key }). To someone who hasn't written to your bot, it lands as a message request, and starting it counts toward the stranger cap (bot.dm() throws RateLimitedError past it). For anyone your bot can't reach, dm is null and reason says why, as chat.invite() does.
  • m.from is who sent it: their name and @handle, and actorId, the account. Compare actorId, never the handle: handles change hands.
  • m.thread holds the one reply of your bot's that someone is replying to, if any (never the whole chain). Keep what a conversation needs under m.id, and look it up on the next turn by m.thread[0].messageId.

The commands you register in code are your listing's commands: the SDK sends them to Peachy when the bot starts, and the console shows them read-only. A typed /name is treated like a picked command.

Commands can change any time. bot.command() while the bot runs, or bot.removeCommand(), updates the listing at once (await bot.commandsSynced() if you need to know it landed). People's phones refresh the list within an hour, so a new command shows up in the picker within the hour, while a typed one reaches it right away. A /name you never registered reaches your message handlers as words (m.text starts with it), so made-up-on-the-fly commands can be handled there.

Test without a network: npm test runs the bot against @peachysocial/bot/testing, the same in-memory Peachy dev uses. Before you ask for verification, run the conformance check: see Check your bot.

9. Deploy#

A bot is a long-running process. It keeps a connection open to Peachy and needs no public URL, so it runs on a Fly.io machine, a Render background worker, a Railway service, or any VM, but not on platforms that run code per request (serverless functions and edge workers, such as AWS Lambda). Run exactly one copy. See Where your bot runs.

A bot that also takes webhooks (posting to a group when GitHub pushes, say) runs the SDK inside its own web server, in the same one process:

const server = createServer(handler).listen(Number(process.env.PORT));
await bot.start();
  • Listen before await bot.start(), and don't make the host's health check wait on it: on a deploy, start() waits up to leaseWaitMs (a minute) for the old instance to stop and let go of the store.
  • In the handler, find each chat by the Chat.id you saved (await bot.chat(id)) and chat.post(). Answer the webhook only once the posts went out, so its sender retries a failure: a post made outside a handler isn't queued or retried for you.
  • Keep the webhook's secret in your host's secrets, beside the store key.
  • Still one instance, and never scaled to zero: on Fly.io, add an [http_service] with auto_stop_machines = "off" and min_machines_running = 1; on Render, make it a web service with numInstances: 1 instead of a background worker.

Create a second bot for production, and link it only on the server: set its Connect code as the secret PEACHY_CONNECT_CODE for the first start, and remove it once the logs say Linked ✓. That bot is a development bot too, open to you and up to 10 testers, until an admin of a verified organization publishes it. A person can have 3 development bots, so these two use two of them (see Limits). Never restore its store from a backup, and back up its store key: see Keep the store, back up the key. The template's README walks through Fly.io and Render, rotating the store key, and resetting a bot's keys.

When it's ready for everyone, publish it: see Development bots.