Skip to content

Sending Text Messages

The simplest and most common operation when building WhatsApp bots is sending text responses. Leaves Guardian provides dedicated helpers and full support for WhatsApp text formatting, user mentions, and quoted messages.


The client.sendText() Helper

sendText() is the recommended high-level helper for quickly sending plain text to a user or group:

javascript
// Signature: client.sendText(jid, text, options)
await client.sendText('628123456789@s.whatsapp.net', 'Hello from Leaves Guardian!');

Basic Example: Echo Bot

javascript
client.on('message', async (msg) => {
  if (msg.sender.isMe) return;

  if (msg.text.startsWith('!echo ')) {
    const responseText = msg.text.slice(6);
    await client.sendText(msg.chat.id, responseText);
  }
});

WhatsApp Text Formatting

Leaves Guardian passes standard WhatsApp markdown directly to the messaging layer:

StyleSyntaxOutput Example
Bold*text*text
Italics_text_text
~Strikethrough~~text~~text~
Monospace```text``` or `text`text
> Quote Block> textBlockquote
• Bullet List• itemBullet item

Example with Formatting

javascript
const formattedMessage = `
🌟 *Server Status Report*
-------------------------
• *Uptime*: _99.98%_
• *Memory*: \`142 MB / 512 MB\`
• *Status*: ~DEGRADED~ *OPERATIONAL*

> Generated by Leaves Guardian
`.trim();

await client.sendText(msg.chat.id, formattedMessage);

Quoting Messages (Replies)

To quote an incoming message in your response, pass the original message (or its raw key) in the options.quoted parameter:

javascript
client.on('message', async (msg) => {
  if (msg.sender.isMe) return;

  if (msg.text === '!ping') {
    // Reply directly quoting the user's message
    await client.sendText(msg.chat.id, '🏓 Pong!', {
      quoted: msg.raw || msg
    });
  }
});

Mentioning Users

To mention one or more users in a group chat, include their JIDs in the mentions option and format @phone in the message text:

javascript
client.on('message', async (msg) => {
  if (!msg.chat.isGroup) return;

  if (msg.text === '!tagme') {
    const targetJid = msg.sender.id;
    const phone = targetJid.split('@')[0];

    await client.sendText(msg.chat.id, `👋 Hello @${phone}!`, {
      mentions: [targetJid]
    });
  }
});

Released under the MIT License.