Build a bot for Peachy
Peachy is a chat app where friends make plans, and every private chat in it is end-to-end encrypted. A bot is a program you run on your own server. It holds one encrypted device, like a phone, and it reads only what people choose to send it.
A member adds your bot to a chat the way they'd add a person: from chat info › Add a bot, or by typing its @handle and choosing Add @yourbot?. In a group run by its admins (every community's group is one), only an admin can. The chat says so. From then on your bot reads what the person who added it sends there, plus whatever of their own earlier messages they chose to share. Each other member's messages reach it only once they've tapped OK on a one-time card naming you ("Messages and calls here now also go to Owner"). A member who mentions your bot first sees the same OK before the message goes. Until they tap OK, it gets nothing of theirs. A Peachy app that shows bots holds their messages back. The server doesn't check this, so a modified app, another client, or a Peachy app too old to show bots could send your bot more (Who sees bots). Your bot can't ask for history: people give it or they don't. Posting, reacting and joining calls are separate permissions the chat gives.
People can also write to your bot in its own chat, one to one, after a consent sheet that says where their messages go.
You build on @peachysocial/bot, our TypeScript SDK for Node.js. It does the encryption, keeps the bot's keys in an encrypted store, and follows the protocol's rules for you.
Start here#
- Quickstart: your bot answering in your terminal in a few minutes, with no account, then in a real chat, with a development bot anyone can make.
- Register an organization and its accounts: the console, accounts and their review, handles, verification and limits.
- Privacy labels: how your listing becomes what people see before they send your bot anything.
- Conformance checklist: what any bot must do, and what a bot not built on the SDK must show before it's verified.
- SDK reference, every class, method and error in
@peachysocial/botand@peachysocial/bot/testing: published at launch, with the package. Until then each one is documented in the package's types, which your editor shows on hover.
Code to start from#
Both repositories, and @peachysocial/bot on npm, are available at launch.
- peachy-bot-template (github.com/peachysocial/peachy-bot-template): a minimal bot with one command, ready for Fly.io or Render.
- peachy-claude-bot (github.com/peachysocial/peachy-claude-bot): an example that answers with Claude through Anthropic's API, with its privacy labels filled in, a per-chat rate limit and prompt caching. You bring your own Anthropic key; Peachy provides none.
What to know before you start#
- Your bot's store is its identity. It holds the bot's keys. Lose it, and the way back is Link a new server on the bot's page: it resets the bot's keys, and it leaves the chats it's in until an admin in each adds it back. (Unlink its server keeps the keys: the same store connects again with the code it gives.) A second, empty store can't take over a linked bot: a bot has one device, so it is refused, and the code it redeemed signs the first store out. Never restore a store from a backup; back up its key instead.
- Run exactly one long-running process per bot. One store is one device, and a bot holds a connection open: per-request platforms such as Lambda or Workers can't run one.
- Your bot gets the messages of every member who added it or said OK to it, and knows which are for it. A member's first mention of it asks them that OK first. A member who hasn't sends it nothing from a Peachy app that shows bots (
leftOutnames them; Who sees bots says what other clients do). Members whose community hasn't approved your bot send it nothing either, mentions included; their community's admins approve it under Bot approval. A company's community takes only the bots its admins approve: an 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. Each message arrives as onemessageevent.m.viais"chat"in a group and"dm"in your bot's own chat with one person.m.mentionedis true when the message mentions your bot, andm.commandis set when it starts with one of your commands (@yourbot /plan Tahoe, or a bare/plan Tahoe, which in a group runs on the bot added there most recently that has/plan, never on one without it). In a group, answer what's for you and let the rest be. A reply to a mention always goes, even where the group doesn't let your bot post on its own:bot.on("message", async (m) => { if (m.via === "chat" && !m.mentioned) return; await m.reply(`You said: ${m.text}`); }); - Shared history is the sharer's own, and their claim. A share carries only the sharer's own messages from before your bot joined, never anyone else's, and the sharer's phone writes them (
claimedBy). Never act on them as proof of what was said. - Your bot replies as a person does.
m.reply(text)goes in the message's thread in a group, where everyone in the chat can open it; in your bot's own chat it sits inline under the person's message.m.reply(text, { alsoSendToChat: true })also sends it to the chat, as a person can. Save that for replies the whole chat wants. In a group a reply to a mention always goes; any other reply, and anything your bot posts unprompted, needs thepostpermission: without itm.reply()throwsGrantMissingError, and nothing is sent. Your bot's posts and replies reach the members who added it or said OK to it;leftOut, on what each returns, names the rest. They reach the group's members as the group itself holds them: someone a community admin orders removed stops getting them once a member's device carries the order out. In a community's groups only admins change the group's info, remove people or reset its link, and your bot is never an admin. Removing your bot from a listed group run by its admins moves its join inbox: the admin's device that removes it resets the listing, so your bot stops reading who asks to join. In a listed group not run by its admins, nothing resets it until an admin updates the group (re-founds it). Your bot removes nobody and is never an admin, so it never resets one. In a group run by its admins (chat.runByAdmins) only an admin adds people, so your bot invites nobody there:chat.invite()resolvesgroupRules. The SDK renews your bot's keys there as a member's phone renews its own, with no admin's phone needed. A bot offline so long that its key lapsed first getslockedOut(keyExpired) and asks back in; a member's phone lets it back in there too, with no admin's needed. When an admin updates a group (re-founds it), your bot follows into the new group under the same chat id, with nothing for your code to do; a community's own group takes your bot's community with it. If the admin's phone can't bring your bot over, the new group says "@yourbot wasn't added.", and the admin's phone removes your bot from the old group where it still can: you getremoved, and a member can add it again. - Answer within a minute. When someone mentions your bot, or writes in its own chat, their phone waits 60 seconds for a message from it. If none comes, it shows "@yourbot didn't answer" under their message, to them only. While a handler runs, the SDK shows your bot typing, as a person's phone does (
typing: falseturns that off), but typing doesn't stop the 60-second clock: a slow answer should reply something short first. - Writing first goes through its own chat.
bot.dm(person)is your bot's chat with someone, started now if there is none, as anybody writes to a stranger, anddm.post()writes there: a reminder, a digest. To someone who has written to your bot, it's a message; otherwise it's a message request they accept or decline, and it doesn't arrive at all if their Messages from bots setting says Verified only (and your bot isn't) or No one.chat.invite(person)invites someone to a group under the group's own rules, as a member's add, and says why when it can't (reason);bot.dm()says why too ({ dm: null, reason }, the same words). Your bot writes first and invites only within its reach: both sayoutOfReachfor anyone else. Neither is retried for you. Everyone your bot invites gets the group as a request from the bot, as from anyone they don't know. In a community's groups only admins add people or bots, so your bot invites nobody there (groupRules). In a community's room that needs updating first (not yet run by its admins), invite only people in the community: Peachy tells a bot no roster, so the SDK invites only someone in the community's own group (the one Peachy names for the community, never a group that only claims to be it), once a member has added your bot there (notInCommunityotherwise, for every invite until then). Nobody the group removed is invited (banned). A group brought into a community and not yet updated follows its own rules. An invite that loses a race to another member's change throwsGroupChangedErrorwithretryAfterMs, and nothing is retried. Peachy can also limit how many new people your bots start chats with each day (the stranger cap). The cap is off for now. When Peachy turns it on, starting a chat with someone new to your bot today counts toward it. It is the owner's: one allowance for all its bots. Past it,bot.dm()throwsRateLimitedErrorwithlimit"strangers"andscope"owner"until midnight UTC, before any of the person's key packages is spent; posts in a chat already started never count, and neither does an invite into a group, where the person's message requests protect them. While Peachy has bots off, they throwPausedError("botsOff"). A report after a first message counts against your bot. Pass{ key }todm.post()orchat.post()(a webhook's delivery id) and a retry with the same key posts nothing more. - Keep your own data in your own database. The store holds the bot's keys and its place in each chat, not your app's state. Key a group's data by
Chat.id, and a DM's byDm.id: it stays the same across restarts, a lost chat coming back,store copyandrekey. A chat that removes the bot and adds it again, or a key reset, is a new chat with a new id, and nothing ties it to the old one. Key a person's data byactorId. If you keep what people send, delete it as their phones do, in groups and in the bot's DM alike: a message ondeleteand onexpire(its disappearing timer ran out;disappearsAton the message says when), a chat's data onremoved. The SDK deletes only its own copies. - Events can arrive twice (
redelivered), after a crash or a deploy.m.reply()knows: a handler running again gets the replies an earlier try already sent (duplicate: true) and sends nothing new.m.react()doesn't, so key what you've done by the message'sid, and givechat.post()thatidas itskey. - A bot sends files as a person does. It reads the photos and files people send it (
attachments), andchat.post(),m.reply()anddm.post()take{ files }: a path, the bytes, or a stream, with a type and a name when the bytes don't say. - Sends are metered (60 a minute to one person, bursts of 20, and 1,200 a minute across all your bots; see Limits). Past it a send throws
RateLimitedErrorwithretryAfterMs. A handler that lets it throw is tried again once that wait has passed (at least 2, then 10 seconds later, and at most 10 minutes), then set aside; a loop posting to many chats should still waitretryAfterMsitself between sends. An organization's accounts share its owner limits: a refusal there hasscope"owner", as when another of its accounts spent it. While Peachy can't be reached the SDK reconnects on its own and raises thedisconnectedandreconnectedstatuses (bot.connectedsays which holds now), and a send throwsUnavailableError. While the bot is paused, or Peachy has bots switched off, a send throwsPausedErrorand nothing people write reaches the bot; thepausedandresumedstatuses, orbotsOffandbotsWereOff, say when. Bots are off until Peachy opens them:start()raisesbotsOffand waits. - Run one copy. A bot is one device, and Peachy answers one running copy of it at a time: a second (a store copied with
cp, a cloned volume, two instances of one deploy) is refused withStoreLockedErrororRunningElsewhereError. A deploy that starts the new instance before stopping the old one is fine:start()waits up toleaseWaitMsfor the old one to stop. One process may run several bots, each on its own store (an organization's accounts each have their own Connect code and store), but twoBots in it on one store, or on two stores linked to one bot, fail atstart()at once. An old one that froze, or lost its host without closing its connection, frees the bot 45 seconds after the last request it sent. A running bot can also stop on its own (another copy took over, its keys were reset, the organization admin who linked it left,UnlinkedError, or Peachy revoked its token for another reason,RevokedError, whosereasonsays which): itserrorhandlers get the error withfatal: true. Exit non-zero there, as the template does, so your host restarts it or tells you. - Handlers can stop early. Each message and each chat event (
reaction,edit,delete,history,ring) carries asignalthat aborts when its try runs pasthandlerTimeoutMsor the bot stops. Pass it todownload(),fetchor your model's client. - Your listing is a promise people read before they send. Keep your code inside it. When people choose to add your bot, send it their first message, or accept its message request, and on its page, they read "Messages to @yourbot go to Owner", the owner your listing names: you, or your organization.
- Your bot checks keys as people's apps do. It publishes its keys to Peachy's public key log and checks its own account there each day, and people's apps check your bot's keys the same way. What it finds arrives as a
keyTransparencystatus, in the words Peachy's apps use. None stops the bot. If one says a device or account key that isn't yours was added, tap Link a new server on the bot's page. An invite orbot.dm()that answerskeyMismatchreached only devices the log proves are no longer that person's. Against Peachy's production servers the checks run in compare mode for now: they report what they find and refuse nothing, sokeyMismatchcomes only from a development server.bot.keyTransparency.readysays whether the checks are running: false until the bot has verified the log's head. See Key transparency.
What Peachy does better here#
- Your bot's messages come from its own device in the chat, and every member's phone checks it, so nobody can put words in your bot's mouth.
- The bot's store is encrypted by default, and its key never sits beside it.
- No phone number, no captcha, no public URL, no webhook.
- The server publishes the oldest SDK it accepts. Peachy's policy is to announce a higher minimum at least 90 days ahead, except for a security fix, and the SDK logs a warning from then on.
- A bot is in a chat only when a member adds it, with everyone told by name.