Add keyword
addKeyword
is the starting point of a conversation flow, just need to pass an array of keywords,
when someone text those keywords in the chatbot, the function will trigger the predefined dialog you have set up.
In the following example is stablished a conversation flow that received the keywords 'hello' and 'hi',
then the chatbot will send a message using addAnswer
with the text 'Hi, welcome to my amazing shop, how can I help you?'
and 'The shop offers: shoes, shirts, pants, and hats'.
import { addKeyword } from '@builderbot/bot';
const mainFlow = addKeyword(['hello', 'hi'])
.addAnswer(['Hi, welcome to my amazing shop', 'how can I help you?'])
.addAnswer(['The shop has: ', 'shoes, shirts, pants, and hats'])
The conversation flow can be limited to a specific keyword, in example the word 'purchase' to trigger the next dialog
import { addKeyword } from '@builderbot/bot';
const mainFlow = addKeyword('buy')
.addAnswer(['Great! ', 'What do you want to buy?'])
Regex
The chatbot supports regex validation, in the following example an object is passed as the second parameter to the addKeyword function, the object has a property regex set to true and the keywords property is a regex pattern.
import { addKeyword } from '@builderbot/bot';
const REGEX_GMAIL_EMAIL = /(\w+)@gmail\.com/g;
const mainFlow = addKeyword(REGEX_GMAIL_EMAIL,
{ regex: true })
.addAnswer(`Thanks for the gmail address`)
Sensitive
The chatbot detects an specific keyword in any part of a message, but passing the sensitive
property as true
the chatbot will trigger the dialog if the user write down the exact same keyword.
import { addKeyword } from '@builderbot/bot';
const mainFlow = addKeyword('buy', { sensitive: true })
.addAnswer(['Great! ', 'What do you want to buy?'])
Add answer
addAnswer
is used to send a message to the user, it can be a simple text or a file attached.
In the following example is stablished a conversation flow that received the keyword 'hello',
then the chatbot will send a message using addAnswer
with the text 'Hi, welcome to my amazing shop, how can I help you?'
import { addKeyword, addAnswer } from '@builderbot/bot';
const mainFlow = addKeyword('hello')
.addAnswer('Hi, welcome to my amazing shop, how can I help you?')
There are different settings for sending messages, some functions such as sending files, images, video, delays, line breaks: line breaks, consecutive, delay , callback
Consecutive Messages
The chatbot can send multiple messages just adding the addAnswer function one after another. In the following example the chatbot will send (3) messages:
Hi
, Welcome to my amazing shop
, how can I help you?
in that order.
import { addKeyword, addAnswer } from '@builderbot/bot';
const mainFlow = addKeyword('hello')
.addAnswer('Hi')
.addAnswer('Welcome to my amazing shop')
.addAnswer('how can I help you?')
Message with line breaks
Whenever you need to send a message with line breaks, you can use an array of string as you can see in the following example.
import { addKeyword, addAnswer } from '@builderbot/bot';
const mainFlow = addKeyword('hello')
.addAnswer([
'Hi', 'Welcome to my amazing shop',
'how can I help you?'
])
Message with delay
The chatbot can send a message with a delay
, just add the addAnswer function with the delay property set to the amount of milliseconds you want to wait before sending the message.
import { addKeyword, addAnswer } from '@builderbot/bot';
const mainFlow = addKeyword('hello')
.addAnswer('This message will after 2 seconds',
{ delay: 2000 }
)
Message with callback
When using callback functions in an addAnswers the operation prioritizes the sending of the message and then the execution of the function.
import { addKeyword, addAnswer } from '@builderbot/bot';
const mainFlow = addKeyword('hello')
.addAnswer('Hi!, Do you know 4+4?', null, async (_, {flowDynamic}) => {
const sum = 4 + 4
await flowDynamic(`Total: ${sum}`)
})
.addAction(async (_, {flowDynamic}) => {
await flowDynamic(`Other message`)
})
Add Action
addAction
is used to define specific actions as a response when a whatsapp message has been received,
this function allows to trigger conversation flows based on the user's input and define how the chatbot should act.
In the folowing example you can see how you can trigger a function when the user sends a message with the keyword 'buy'.
import { addKeyword, addAction } from '@builderbot/bot';
const mainFlow = addKeyword('buy')
.addAnswer('Great! What do you want to buy?')
.addAction(async (_, { flowDynamic }): void => {
return console.log('The user wants to buy something');
})
You can use the addAction
function with capture
as you can see in the example below
import { addKeyword, addAction } from '@builderbot/bot';
const mainFlow = addKeyword(['Hello', 'Hi'])
.addAction(async (_, { flowDynamic }): void => {
return flowDynamic('Hi! how can I help you?');
})
.addAction({ capture: true }, async (ctx, { flowDynamic, state }): void => {
await state.udpate({ name: ctx.body})
return console.flowDynamic(`The user said: ${ctx.body}`);
})