Skip to main content

Overview

The addAnswer method adds a response message to a conversation flow. It can include media, buttons, capture user input, execute callbacks, and nest additional flows.

Method Signature

addAnswer(
  answer: string | string[],
  options?: ActionPropertiesKeyword | null,
  cb?: CallbackFunction<P, B> | null,
  nested?: TFlow<P, B> | TFlow<P, B>[] | null
): TFlow<P, B>

Parameters

answer
string | string[]
required
The message text to send. Can be:
  • A single string: 'Hello!'
  • An array of strings (joined with newlines): ['Hello!', 'How are you?']
options
ActionPropertiesKeyword
Configuration options for the message.
cb
CallbackFunction<P, B>
Callback function executed when this answer is triggered. Receives context and bot methods.
nested
TFlow | TFlow[]
Child flow(s) to execute after this answer. Can be a single flow or array of flows.

Return Value

flow
TFlow<P, B>
Returns the flow object, allowing for method chaining with additional addAnswer() or addAction() calls.

Usage Examples

Simple Text Response

import { addKeyword } from '@builderbot/bot'

const flow = addKeyword('hello')
  .addAnswer('Hi! Welcome to our service.')
  .addAnswer('How can I help you today?')

Multi-line Message

const flow = addKeyword('info')
  .addAnswer([
    'Company Information:',
    'Name: BuilderBot Inc.',
    'Email: [email protected]',
    'Hours: Mon-Fri 9AM-5PM'
  ])

With Media

const flow = addKeyword('catalog')
  .addAnswer(
    'Here is our product catalog:',
    { media: 'https://example.com/catalog.pdf' }
  )
  .addAnswer(
    'Check out this product!',
    { media: './assets/product-image.jpg' }
  )

With Buttons

const flow = addKeyword('menu')
  .addAnswer(
    'Please select an option:',
    {
      buttons: [
        { body: 'View Products' },
        { body: 'Check Order' },
        { body: 'Contact Support' }
      ]
    }
  )

Capturing User Input

const flow = addKeyword('register')
  .addAnswer(
    'What is your name?',
    { capture: true },
    async (ctx, { flowDynamic, state }) => {
      await state.update({ name: ctx.body })
      await flowDynamic(`Nice to meet you, ${ctx.body}!`)
    }
  )
  .addAnswer(
    'What is your email?',
    { capture: true },
    async (ctx, { flowDynamic, state }) => {
      const name = state.get('name')
      await state.update({ email: ctx.body })
      await flowDynamic(`Thanks ${name}, we'll contact you at ${ctx.body}`)
    }
  )

With Delay

const flow = addKeyword('order')
  .addAnswer('Processing your order...')
  .addAnswer(
    'Order confirmed!',
    { delay: 2000 } // Wait 2 seconds before sending
  )
  .addAnswer(
    'You will receive a confirmation email shortly.',
    { delay: 1000 }
  )

With Nested Flows

const productsFlow = addKeyword('products')
  .addAnswer('Here are our products...')

const ordersFlow = addKeyword('orders')
  .addAnswer('Here are your orders...')

const menuFlow = addKeyword('menu')
  .addAnswer(
    'Select an option:',
    { capture: true },
    async (ctx, { gotoFlow }) => {
      if (ctx.body === '1') await gotoFlow(productsFlow)
      if (ctx.body === '2') await gotoFlow(ordersFlow)
    },
    [productsFlow, ordersFlow] // Nested flows
  )

API Integration

const weatherFlow = addKeyword('weather')
  .addAnswer(
    'Which city?',
    { capture: true },
    async (ctx, { flowDynamic }) => {
      const city = ctx.body
      const response = await fetch(`https://api.weather.com/v1/${city}`)
      const data = await response.json()
      await flowDynamic(`Weather in ${city}: ${data.temp}°C, ${data.condition}`)
    }
  )

Notes

  • String arrays are automatically joined with newlines (\n)
  • When capture: true, the callback receives the user’s next message
  • Delays are cumulative with the global bot delay setting
  • Media URLs can be HTTP/HTTPS links or local file paths
  • Buttons availability depends on the provider (not all platforms support buttons)
  • Callbacks are async-compatible - use async/await for asynchronous operations

Build docs developers (and LLMs) love