> ## Documentation Index
> Fetch the complete documentation index at: https://botmesh.afaya.com.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Send Messages

> Build a bot that shows every way to answer a text: keywords, regex, quotes, reactions, sender info and multi-line replies.

**Level:** Beginner · **Tags:** WhatsApp, Telegram, Triggers

**Features shown:** [trigger forms](https://botmesh.afaya.com.co/guide/concepts.md#when-simple-responses) ·
[reply helpers](https://botmesh.afaya.com.co/guide/messages.md#reply-helpers-inside-a-handler) ·
[`ctx.react()`](https://botmesh.afaya.com.co/guide/messages#react) ·
[`ctx.senderInfo`, `ctx.sender`, `ctx.isGroup`](https://botmesh.afaya.com.co/guide/concepts#the-ctx-object) ·
[catch-all](https://botmesh.afaya.com.co/guide/concepts.md#message-event-vs-onmessage)

## Prerequisites

Node.js 22.12 or newer and a folder with `"type": "module"` (see
[Getting started](https://botmesh.afaya.com.co/guide/getting-started.md#requirements)). Then install:

```bash theme={null}
npm install @botmesh/core @botmesh/baileys
```

## The complete bot

```js theme={null}
// bot.js
import { Bot, defineBot, isMainModule } from '@botmesh/core'
import { BaileysProvider } from '@botmesh/baileys'

const createBot = defineBot((provider) => {
  const bot = new Bot({ provider })

  // One keyword, or any of a list (whole message, case and accents ignored)
  bot.when('hi', (ctx) => ctx.reply('👋 Hello!'))
  bot.when(['bye', 'goodbye', 'see you'], (ctx) => ctx.reply('👋 See you later!'))

  // The word anywhere in the message
  bot.when({ pattern: 'invoice', match: 'contains' }, (ctx) => ctx.reply('🧾 I see you mentioned an invoice'))

  // Regular expressions
  bot.when(/[\w.]+@[\w.]+\.\w+/, (ctx) => ctx.reply('📧 That looks like an email'))
  bot.when(/\d{4,}/, (ctx) => ctx.reply(`🔢 Detected a long number in "${ctx.body}"`))

  // Quote the user's message, or react to it
  bot.when('quote', (ctx) => ctx.quote('Replying to your exact message'))
  bot.when('good bot', (ctx) => ctx.react('👍'))

  // Who wrote, and where
  bot.when('who', (ctx) =>
    ctx.reply(`You are ${ctx.senderInfo.displayName ?? 'someone'} (${ctx.sender}) in a ${ctx.isGroup ? 'group' : 'private chat'}.`),
  )

  // Multi-line text with WhatsApp-style formatting
  bot.when('info', (ctx) => ctx.reply(['ℹ️ *Bot info*', '', '• Engine: botmesh', '• Works on: WhatsApp, Telegram'].join('\n')))

  bot.onMessage((ctx) => ctx.reply('🤔 I did not recognize that. Try: hi, quote, who, info.'))

  return bot
})

export default createBot

// `node bot.js` connects to WhatsApp; `npx botmesh dev bot.js` skips this block
if (isMainModule(import.meta)) {
  const bot = await createBot(new BaileysProvider({ session: 'messages-bot' }))
  bot.on('ready', () => console.log('✅ Connected. Send "hi" from another phone.'))
  await bot.start()
}
```

## Sample transcript

Captured with `npx botmesh dev bot.js --name Ana`:

```text theme={null}
user › Hi
bot › 👋 Hello!
user › see you
bot › 👋 See you later!
user › can you resend my invoice?
bot › 🧾 I see you mentioned an invoice
user › ana@example.com
bot › 📧 That looks like an email
user › my code is 123456
bot › 🔢 Detected a long number in "my code is 123456"
user › quote
bot › Replying to your exact message
user › good bot
bot › [reaction: 👍]
user › who
bot › You are Ana (user) in a private chat.
user › info
bot › ℹ️ *Bot info*

      • Engine: botmesh
      • Works on: WhatsApp, Telegram
user › hello there
bot › 🤔 I did not recognize that. Try: hi, quote, who, info.
```

## How it works

* **Exact by default.** `bot.when('hi')` matches the whole message after
  normalizing case, accents and spaces: "Hi" matches, "hi there" does not.
* **`match: 'contains'`** (also `'startsWith'` and `'endsWith'`) matches part
  of the message; a **regex** matches anywhere in it.
* **Specificity decides** when several triggers match: exact text beats
  regex, regex beats `contains` (see [Precedence](https://botmesh.afaya.com.co/guide/triggers.md#precedence)).
* **`ctx.quote(text)`** replies quoting the incoming message;
  **`ctx.react(emoji)`** reacts to it (channels without reactions skip it).
* **`ctx.senderInfo.displayName`** is the sender's profile name when the
  transport reports one; `ctx.sender` is their stable id, the same in a DM and
  in a group.
* Baileys has no native buttons: for a quick choice use a
  [poll](https://botmesh.afaya.com.co/examples/poll-bot.md) or a numbered [menu](https://botmesh.afaya.com.co/examples/menu-bot.md).

## Run it on WhatsApp or Telegram

`node bot.js` prints a QR: scan it in WhatsApp → Linked devices. For
Telegram, install `@botmesh/telegram` and swap the provider as shown in
[Or connect Telegram](https://botmesh.afaya.com.co/guide/getting-started.md#or-connect-telegram); the
factory does not change.

## Next steps

* [Media replies](https://botmesh.afaya.com.co/examples/media.md): photos, files, locations and contacts
* [FAQ bot](https://botmesh.afaya.com.co/examples/faq-bot.md): keyword answers with a fallback
* [Messages & media](https://botmesh.afaya.com.co/guide/messages.md): typing indicators, edits, long texts


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.