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()