A simple conversation bot with ability of reading the dialogue scripts from JSON files.
The packages are publish on Github NPM repository.
npm install @dplocki/generic-conversation-bot
The bot (class Bot
) is a state machine. The state contain the logic of reaction on received message
(from user). The reaction is collection of actions
. User input, provide also memory
cache.
State is an object. Each state need to implements two methods: *beforeMessage()
and *analyse(message)
.
Both return collection of actions (as generator).
The beforeMessage
is called when bot is change its internal state. The analyse
method is called when Bot receive the new message.
class State {
*beforeMessage() {
}
*analyse(message) {
}
}
- ChooseState - Similar in work as
switch
code - IfElse - Similar in work as
if else
code - MoveNextState - Displays the given text, no matter the input will call all provided actions
- WaitForActivationState - The starting state, bot will ignore all input until given phrase (like: "hi") will be given
Each action is function. The action is taking two parameters:
bot
- the Bot objectmessage
- the whole message object received by the Bot
The return value of action is ignored.
function action(bot, message) {
}
For simplicity the the action builders are store in the actions.js
file. Those are functions returning actions functions.
response(text)
- Sets the response of BotstaySilence()
- Removes all responsesetState(state)
- Sets the given state as next oneendConversation()
- Resets the BotjumpToState(stateName)
- Sets the state from states list by its nameremember(key, value)
- Saves the value inmemory
on providedkey
rememberInputAs(key)
- Saves the input (user message, only text) inmemory
on providedkey
increaseRemembered(key)
- Increase the value frommemory
onkey
by one (if value is not numeric, will be replace by 1)decreaseRemembered(key)
- Decrease the value frommemory
onkey
by one (if value is not numeric, will be replace by -1)
The Bot state machine can be loaded from JSON file. The file needs to be parsed first.
The parser is provided by ParserBuilder
object. You can create parser containing additional states, actions or preparser actions.
const states = new ParserBuilder()
.addCustomActions(require('./actions'))
.addCustomStates(require('./states'))
.addPreParsers(require('./preParsers'));
.parse(
JSON.parse(fs.readFileSync('input.json', 'utf8'))
);
The json file contain an array of objects. Each of it need to have two attributes: type
and name
.
[
{
"type": "type_of_state",
"name": "name_of_state"
}
]
The parsing process transform the file into the object, where key is name of state.
The preparser are called before the parsing process, for given pre-states. To call them you need provide two attribute: pre_parser
and pre_states
.
[
{
"pre_parser": "pre_parser_name",
"pre_states": [
{ },
{ }
]
}
]
The pre_parser
value indicate which preparser needs to be called on given pre_states
. The result: the collection of ready-to-parsing-states, will be input for parsing.
function preprocessor(state, index, allStates) {
return state;
};
Both example bot can use BitClient
. The function use the Node readline
module for terminal usage.
All require installation of package.
A linear conversation. The bot will ask about name and use it back.
The example demonstrate the memory usage.
A linear conversation. The bot will ask few simple question and save the results showing the summary on the end.
The example demonstrate own additional (the SummaryState
class) state.
A nonlinear exam conversation. The bot will ask few simple question but user can skip and returning to them letter. Bot will summaries the result on end.
The example demonstrate preparser.
The unit test are based on Mocha.
npm test