scripted-chat v0.1.0
Scripted Chat
A simple code for defining scripted chat using Javascript
Installation
yarn add scripted-chator
npm i scripted-chatGet Started
import { ScriptedChatState } from 'scripted-chat'
const scriptControls = new ScriptedChatState(config)Before moving on to the config object and the other ScriptedChatState methods, learn the basics of Step and Script.
Step
const step = {
id: 'start',
message: 'Hi, this is my first message. How are you?',
next: 'user_name',
input: 'text',
}id: step identifiermessage: message defined for user interactionnext: next step id.- Required:
startandend
- Required:
input: input typestext,email,number,date
Script
Is the combination of steps
const script = [
{
id: 'start',
message: 'Hi, this is my first message. How are you?',
next: 'user_name',
input: 'text',
},
{
id: 'user_name',
message: "What's your name?",
next: 'end',
input: 'text',
},
{
id: 'end',
message: 'Nice to meet you!',
next: '',
input: '',
},
]Basic config
The most important basic thing about the config is defining the chat steps. You can see other methods later down.
import { ScriptedChatState } from 'scripted-chat'
const script = [
/** steps */
]
const scriptControls = new ScriptedChatState({
script,
})Script Controls
currentStep: current step objectscript: array of stepsresults: an array containing previous resultsgetStep: (id: string) => Step: get a certain step by idgetNextStep: () => Step: get the next step objectsetStep: (id: string) => Step: set a newcurrentStep- In current version, hooks do not work with this method yet
reset:() => void: resetresultsandcurrentStepvalidateAndProceed: (currentStepValues: any[]) => Promise<void>: pass an array of values (user input) to proceed with your scriptreplaceMessageValuesVariables: (message: string) => string: Already implemented insidevalidateAndProceed. Replace message variables with previous results values. (See more about variables in Advanced section)
Advanced
Variables
You can interpolate user input values inside next step messages using {{step_id}}
const script = [
{
id: 'start',
message: 'Hi, this is my first message. How are you?',
next: 'user_name',
input: 'text',
},
{
id: 'user_name',
message: "What's your name?",
next: 'end',
input: 'text',
},
{
id: 'end',
message: 'Nice to meet you, {{user_name}}!',
next: '',
input: '',
},
]Hooks
beforeProceed: (event: HookEvent) => boolean | Promise<boolean>
Use this hook to validade a user input returning a boolean.
If there's no checking, just return true.
const script = [
{
id: 'start',
message: 'Hi, this is my first message. How are you?',
next: 'user_name',
input: 'text',
},
{
id: 'user_name',
message: "What's your name?",
next: 'end',
input: 'text',
beforeProceed(event) {
const { values } = event.result
return !!values[0]?.length
},
},
{
id: 'end',
message: 'Nice to meet you, {{user_name}}!',
next: '',
input: '',
},
]afterProceed: (event: HookEvent) => void | Promise<void>
You can't validate using this hook, but you still can keep track on step change.
HookEvent object
type HookEvent = {
currentStep: Step
nextStep: Step | null
result: Result
results: Result[]
}
type Result = {
step: string
values: (string | undefined)[]
}currentStep: current step objectnextStep: next step objectresult: current step user input resultresults: an array containing previous results