Incoming and outgoing messages

Issue

Sometimes we will need to listen to the events of incoming or outgoing messages, this is often necessary when we are connecting our chatbot with third party UI applications.


Possible Solution

In the example below we show a code which you can execute and observe the messages in the console. The objective is to capture the incoming and outgoing message from the bot.

app.ts

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

const PORT = process.env.PORT ?? 3008

const welcomeFlow = addKeyword('hello!').addAnswer('Welcome!')

const main = async () => {
    const adapterFlow = createFlow([welcomeFlow])

    const adapterProvider = createProvider(Provider)
    const adapterDB = new Database()

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

    bot.httpServer(+PORT)

    adapterProvider.on('message', ({ body, from }) => {
        console.log(`Message Payload:`, { body, from })
    })

    bot.on('send_message', ({ answer, from }) => {
        console.log(`Send Message Payload:`, { answer, from })
    })
}

main()

Guides

My first chatbot

Learn how build your first chatbot in few minutes

Read more

Concepts

Understand the essential concepts for building bots

Read more

Add Functions

The key to learning how to write flows is add-functions.

Read more

Plugins

Unlimitate and start implementing the community plugins.

Read more

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.