Simplify your code for chatbot without headache.
version 0.1.7 and above no longer use context
variable, if you want update version from 0.1.6 and below, please check variable naming
Copy this code to your command line.
npm i context-chatbot
and add this code to your project.
import { Ctx } from "context-chatbot";
- first, register context to module.
Ctx.registerContext('welcome', (id, payload)=>{
console.log('hello ' + id)
console.log(payload)
})
or register from array.
Ctx.registerArrayContext([
{
state: 'welcome',
callback: (id, payload)=>{
console.log('welcome' + id)
console.log(payload)
}
}
])
- then, set state for id (person/number/etc).
Ctx.setState('6281955551111', 'welcome')
- finally, put this line code to your bot file.
Ctx.Context('6281955551111', 'this is payload')
// output:
// 'welcome 6281955551111'
// 'this is payload'
This function allow you to restricted response from user (time out), if the time is up, then the state will change to the desired state
Ctx.registerArrayContext([
{
state: 'welcome',
callback: (id, payload, timeOut)=>{
console.log('welcome' + id)
console.log(payload)
//Dont forget to clear timeout
clearTimeOut(timeOut)
}
}
])
//if there is no input for 5 seconds, state will change from 'welcome' to 'base'
Ctx.setState(id, "welcome", 5000, "base");
if you dont know where to place setState function to change state, maybe this can help you
Ctx.setMiddleware((id, payload, next) => {
if (payload.message.includes("!login")) {
Ctx.setState(id, "login_ask_username");
}
next();
});
Create a context with named state
state: string
callback: Function
id: string
payload: any
timeOut: optional
setTimeOut function for Ctx with timer
Create a context from array of object
arr: Array of object
state: string
callback: Function
id: string
payload: any
timeOut: optional
setTimeOut function for context with timer
change the state of an id
id: string
state: string
timer: miliseconds number (Optional)
set time out context responsebackTo: string (Optional)
go to target state if timer is timeout
determine context of an id (if state id not found, 'base' status will be added automatically. Make sure to add 'base' context first, but you can change the default state, check API below)
id: string
state: any
set default state if state of id not found
state: string
set middleware before enter the context
midFunc: Function
id: string
payload: any
next: Function
coming soon~