Skip to main content
The sendMessage method allows you to send messages directly to users through the bot’s provider (WhatsApp, Telegram, etc.). This method is particularly useful when you need to send messages programmatically outside of the normal flow context, such as from HTTP endpoints or scheduled tasks.

Method Signature

sendMessage<K = any>(userId: string, message: any, args?: any): Promise<K>
userId
string
required
The recipient’s identifier (phone number for WhatsApp, user ID for other platforms)
message
string
required
The text message to send to the user
args
object
Additional options for the message
return
Promise<K>
Returns a promise that resolves with the provider’s response after sending the message

Usage Examples

Basic Text Message

adapterProvider.server.post(
  '/v1/messages',
  handleCtx(async (bot, req, res) => {
    const { number, message } = req.body
    await bot.sendMessage(number, message)
    return res.end('sent')
  })
)

Message with Media

adapterProvider.server.post(
  '/v1/messages',
  handleCtx(async (bot, req, res) => {
    const { number, message, urlMedia } = req.body
    await bot.sendMessage(number, message, { 
      media: urlMedia ?? null 
    })
    return res.end('sent')
  })
)

Sending Image from Local Path

import { join } from 'path'

await bot.sendMessage('1234567890', 'Check out this image!', {
  media: join(process.cwd(), 'assets', 'sample.png')
})

Sending Multiple Media Types

// Send image
await bot.sendMessage(userId, 'Here is an image', {
  media: 'https://example.com/image.jpg'
})

// Send video
await bot.sendMessage(userId, 'Here is a video', {
  media: 'https://example.com/video.mp4'
})

// Send audio
await bot.sendMessage(userId, 'Here is an audio', {
  media: 'https://cdn.example.com/audio.mp3'
})

// Send document
await bot.sendMessage(userId, 'Here is a document', {
  media: 'https://example.com/document.pdf'
})

Important Notes

  • The sendMessage method is available on the bot object in HTTP endpoints via handleCtx
  • The method bypasses the normal flow logic and sends messages directly through the provider
  • Media files can be provided as URLs or local file paths
  • The actual return type depends on the provider being used (WhatsApp, Telegram, etc.)
  • Messages sent with this method are not tracked in the conversation flow history

See Also

Build docs developers (and LLMs) love