Create

Creating a bot is as simple as running the following command and following the instructions

pnpm create builderbot@latest

Use the space key to select and the enter key to confirm. The CLI performs a preliminary check of the Node and operating system version, informing you if it meets the requirements or providing you with relevant information. In addition to generating a base project for you to simply start up


Requirements

Make sure you have installed Node version 20 or higher, below you can see an example to check the version of node you are using.

Node Version

node -v
v20.10.0

It is recommended to have GIT installed for proper operation. If you are using Linux or MacOc you probably already have GIT installed by default.

Git Version

git -v
git version XXXX

Base Example

In this example we can see the basis of a simple bot which responds to the keywords sent by a user, the words are: info, hello, hi. You can see how to create the bot and implement the flows.

import { createBot, createProvider, createFlow, addKeyword, MemoryDB } from '@builderbot/bot'
import { BaileysProvider } from '@builderbot/provider-baileys'

/** send static messages */
const welcomeFlow = addKeyword<BaileysProvider, MemoryDB>(['hello', 'hi']).addAnswer('Ey! welcome')

/** send dynamic message from db or other sources */
const infoFlow = addKeyword<BaileysProvider, MemoryDB>('info')
    .addAction(async (ctx, { flowDynamic }) => {
        await flowDynamic(`Welcome ${ctx.name}`)
    })

/** send media files */
const mediaFlow = addKeyword<BaileysProvider, MemoryDB>('image')
    .addAnswer(`Send Image A`, { media: 'https://i.imgur.com/AsvWfUX.png' })
    .addAction(async (ctx, { flowDynamic }) => {
        await flowDynamic(`Welcome ${ctx.name}`)
        await flowDynamic(
            [
                {
                    body: 'Send Image B',
                    media: 'https://i.imgur.com/w0RtKnN.png'
                }
            ]
        )
    })

/** initialization bot */
const main = async () => {

    const adapterDB = new MemoryDB()
    const adapterFlow = createFlow([welcomeFlow, infoFlow, mediaFlow])
    const adapterProvider = createProvider(BaileysProvider)

    adapterProvider.initHttpServer(3000)

    await createBot({
        flow: adapterFlow,
        provider: adapterProvider,
        database: adapterDB,
    })
}

main()


Resources

Modularize

Learn how to modularise flows so that you can have a more maintainable bot.

Send Message

How to send a message via HTTP to start conversations, you can send multimedia as well.

Dockerizer

A good practice is to dockerise your bots to make them more maintainable and effective.

Events

Learning about events will make us more fluent when creating chatbots.