Skip to main content
Level: Intermediate · Tags: WhatsApp, Telegram, Multi-channel, SQLite Features shown: the bot factory (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 its package.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

  1. One factory, many bots. defineBot returns 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.
  2. One database file. Each bot gets its own SQLiteStore instance on the same bot.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. Set session, botId or tenantId on the Bot when you run several bots of the same channel (storage namespace).
  3. 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.
  4. Ids are stable. The provider resolves aliases before your code sees a message: on WhatsApp the same person arrives with the same ctx.sender even when WhatsApp switches between their phone number and their LID. So ctx.user, conversations and access lists keep working.
  5. 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).
  6. Stopping. On Ctrl+C both bots stop, which closes their connections and their database handles.
What 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:
Scan the QR from WhatsApp → Linked devices, and write to your Telegram bot. Send 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