defineBot) ·
channels ·
chat and sender ids ·
identity per transport ·
storage namespace ·
SQLite store ·
user data (ctx.user)
Prerequisites
- Node.js 22.12 or newer and an empty folder with
"type": "module"in itspackage.json(Getting started). - A Telegram bot token from @BotFather and a phone with WhatsApp for the bot’s number.
The complete bot
How it works
- One factory, many bots.
defineBotreturns a function that builds a bot around the provider it receives. The start block calls it twice, once per channel, so both bots run the same handlers, flows and commands. The simulator calls it once with its own provider. - One database file. Each bot gets its own
SQLiteStoreinstance on the samebot.db. Do not pass one store instance to two bots:bot.stop()closes its store, and the other bot would lose it. Everything a bot writes is namespaced by its bot id, which defaults to the provider id (baileys,telegram), so the two bots never read each other’s rows. Setsession,botIdortenantIdon theBotwhen you run several bots of the same channel (storage namespace). - Ids are opaque. Each channel picks its own id format. Store and compare them as they arrive; never parse a phone number out of them.
- Ids are stable. The provider resolves aliases before your code sees a
message: on WhatsApp the same person arrives with the same
ctx.sendereven when WhatsApp switches between their phone number and their LID. Soctx.user, conversations and access lists keep working. - Separate profiles. The same person on WhatsApp and on Telegram is two users with two profiles. To link them, store your own mapping (for example a code the user sends from both channels).
- Stopping. On
Ctrl+Cboth bots stop, which closes their connections and their database handles.
whoami prints for the same person on each channel:
In a DM the chat is the user; in a group the chat is the group and the sender
is the member who wrote.
Try it
The simulator runs one bot with its in-memory provider (mock). The
database is the same bot.db, so :reset (a fresh bot from the factory)
still finds the profile:
Run it on both channels
Put the token in.env (TELEGRAM_TOKEN=123456:ABC-your-token), then:
signup on one channel and whoami on the other: the name is
only on the first one.
Next steps
- Channels: what each transport supports, and the Cloud API (
@botmesh/meta) as a third provider - Running N replicas: several processes for the same bot
- Group bot: what the group ids above are used for