Skip to main content
Level: Intermediate · Tags: WhatsApp, Telegram, Groups Features shown: per-participant conversations · scope('group') · mentions (ctx.mentions) · groupMode · commands with arguments · predicate and contains triggers

Prerequisites

  • Node.js 22.12 or newer and an empty folder with "type": "module" in its package.json (Getting started).

The complete bot

How it works

  1. Group defaults. Every group message reaches the bot, and in a group each participant has their own conversation. ctx.from is the group (the chat), ctx.sender the person who wrote.
  2. One quiz per person. Ana and Bob can be halfway through the quiz at the same time: each answer goes to its sender’s conversation. .scope('group') keeps the quiz out of DMs. { pattern: 'quiz', match: 'contains' } also starts it from @bot quiz, and the flow’s id (quiz) comes from that pattern. Scores are kept per group in a Map.
  3. Commands for everyone. /rules and /scores answer with the data of the group they are sent in.
  4. Admin-only commands. The bot checks ctx.sender against ADMINS. These are the bot’s admins, not the group’s: botmesh does not read a group’s admin list, so keep yours in configuration. /id (from slashCommandsPlugin) shows the id of the chat it is sent in: in a DM with the bot that is your user id; in a group, the group id.
  5. Mentions. ctx.mentions lists the users a message mentions, and provider.selfId is the bot’s own id once connected. The catch-all checks them, and it only sees what no other handler took, so @bot quiz still starts the quiz.
  6. Quiet in groups. In a group the catch-all replies only to a mention, so the bot does not answer every message of the group; in a DM it explains how to use it.

Try it

:as bob Bob switches to another member of the same group; the bot is mentioned as @bot.

Variation: only when mentioned

To make the bot ignore every group message that does not mention it, set groupMode ('commands' keeps only commands):
The filter applies to every group message, answers to a running conversation included, so with this mode a player has to mention the bot in each answer, and the mention is part of the answer text. Commands need a mention too (/rules @bot; on Telegram, /rules@YourBot counts as one):
That is why this example keeps the default groupMode: 'all' and stays quiet with a DM-only catch-all instead.

Run it on WhatsApp or Telegram

Set ADMINS to your id (send /id to the bot in a DM to see it), start the bot with node bot.js, scan the QR from WhatsApp → Linked devices and add the bot’s number to a group. On Telegram, install @botmesh/telegram, swap the provider as in Or connect Telegram and add the bot to a group. With BotFather’s default privacy mode a Telegram bot only receives commands, mentions and replies to its own messages in groups; turn it off (/setprivacy) for the quiz answers to arrive.

Next steps