Skip to main content

Overview

The createFlow function creates a flow instance that manages all conversation flows for your bot. Flows are created using addKeyword() and can be nested and chained together.

Function Signature

const createFlow = (args: TFlow[]): FlowClass

Parameters

args
TFlow[]
required
Array of flow objects created with addKeyword(). Each flow represents a conversation path or trigger.

Return Value

flowInstance
FlowClass
Returns a FlowClass instance containing all registered conversation flows.

Usage Examples

import { createFlow, addKeyword } from '@builderbot/bot'

const welcomeFlow = addKeyword(['hello', 'hi'])
  .addAnswer('Welcome! How can I help you?')

const helpFlow = addKeyword('help')
  .addAnswer('Here are the available commands...')

const adapterFlow = createFlow([welcomeFlow, helpFlow])

Empty Flow

You can create an empty flow and add flows dynamically:
import { createFlow } from '@builderbot/bot'

// Create empty flow
const adapterFlow = createFlow([])

Complex Flow with Multiple Triggers

import { createFlow, addKeyword } from '@builderbot/bot'

const orderFlow = addKeyword(['order', 'buy', 'purchase'])
  .addAnswer('What would you like to order?', { capture: true },
    async (ctx, { flowDynamic }) => {
      await flowDynamic(`You want to order: ${ctx.body}`)
    }
  )

const trackingFlow = addKeyword(['track', 'status'])
  .addAnswer('Please provide your order number:', { capture: true },
    async (ctx, { flowDynamic }) => {
      await flowDynamic(`Tracking order: ${ctx.body}`)
    }
  )

const supportFlow = addKeyword(['support', 'help', 'issue'])
  .addAnswer('How can we assist you today?')

const adapterFlow = createFlow([
  orderFlow,
  trackingFlow,
  supportFlow
])

Notes

  • Flows are independent conversation paths that are triggered by keywords
  • Multiple flows can be active simultaneously for different users
  • The order of flows in the array doesn’t affect their priority
  • Each flow maintains its own state and execution context

Build docs developers (and LLMs) love