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?']
Configuration options for the message. URL or path to media file (image, video, audio, or document).
Array of button objects. Each button has a body property with the button text.
When true, captures the user’s next response and passes it to the callback function.
Delay in milliseconds before sending this message.
Deprecated. Time in milliseconds for idle timeout.
Custom reference ID for this answer (for internal use).
Callback function executed when this answer is triggered. Receives context and bot methods. Message context containing:
body: User’s message text
from: User’s phone number/ID
name: User’s name (if available)
Additional provider-specific data
Bot control methods:
flowDynamic: Send dynamic messages
gotoFlow: Navigate to another flow
endFlow: End the current conversation
fallBack: Go back to previous step
state: Manage user-specific state
globalState: Manage global bot state
provider: Access provider instance
database: Access database instance
Child flow(s) to execute after this answer. Can be a single flow or array of flows.
Return Value
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?' )
const { addKeyword } = require ( '@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'
])
const flow = addKeyword ( 'info' )
. addAnswer ([
'Company Information:' ,
'Name: BuilderBot Inc.' ,
'Email: [email protected] ' ,
'Hours: Mon-Fri 9AM-5PM'
])
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' }
)
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' }
)
const flow = addKeyword ( 'menu' )
. addAnswer (
'Please select an option:' ,
{
buttons: [
{ body: 'View Products' },
{ body: 'Check Order' },
{ body: 'Contact Support' }
]
}
)
const flow = addKeyword ( 'menu' )
. addAnswer (
'Please select an option:' ,
{
buttons: [
{ body: 'View Products' },
{ body: 'Check Order' },
{ body: 'Contact Support' }
]
}
)
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 } ` )
}
)
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 }
)
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
)
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 } ` )
}
)
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