sbot-conversation
A conversation implement for Hubot.
Features
- Conversation for hubot
- Conversation manager
Installation
npm install sbot-conversation
Example
const initManager = ;const DYNAMIC_SCHEMA_MOCK = onCompleteMessage: 'Create user successfully!! Thanks for reporting this.' type: 'dynamic' steps: question: 'Start create a user \nPlease enter your user name.' answer: type: 'text' validation: 'description': 'full name' 'type': 'string' 'minLength': 8 required: true question: 'Please enter your user email.' answer: type: 'text' validation: 'description': 'email address' 'type': 'string' 'format': 'email' 'maxLength': 64 required: true question: 'Please enter gender enum[female, male, unspecified]' answer: type: 'choice' options: match: 'unspecified' match: 'male' match: 'female' required: false ; const JSON_SCHEMA_MOCK = 'type': 'object' 'required': 'name' 'properties': 'name': 'description': 'full name' 'type': 'string' 'minLength': 8 ; module { let switchBoard = ; robot; robot;};
Usage
Create a conversation manager instance
Return a conversation manager instance.(singleton is recommended).
- robot:
Hubot.Robot
- type: (optional) 'user' or 'room', default 'user'.It defines if this conversation is with the whole room or with a particular user only. If the message comes from a user (or a room) that we're having a conversation with, it will be processed as the next step in an ongoing Dialog.
- callback: (optional) The callback should be return a
Boolean
, when the return value istrue
and there is a active conversation of the user (or the room), it will be processed as the next step in an ongoing Dialog. - singleton:
Boolean
,(optional) defaulttrue
. Enable the singleton.
Example
let switchBoard = ;
Create a conversation
There are there patterns to create a conversation.
First pattern: Init a json schema
Example
const JSON_SCHEMA_MOCK = 'type': 'object' 'required': 'name' 'email' 'properties': 'name': 'description': 'full name' 'type': 'string' 'minLength': 8 'email': 'description': 'email address' 'type': 'string' 'format': 'email' 'maxLength': 64 'employeeNum': 'description': 'employee Number' 'type': 'integer' 'minimum': 100 'maximum': 600 'gender': 'description': 'gender' 'type': 'enum' 'default': 'unspecified' 'enum': 'unspecified' 'male' 'female' ; module { let switchBoard = ; robot;};
How to define a json schema, please referJSON Schema
type
is required and must be a string 'object'.
Second pattern: Init a dynamic message model
Example
const DYNAMIC_SCHEMA_MOCK = onCompleteMessage: 'Create user successfully!! Thanks for reporting this.' type: 'dynamic' steps: question: 'Start create a user \nPlease enter your user name.' answer: type: 'text' validation: 'description': 'full name' 'type': 'string' 'minLength': 8 required: true question: 'Please enter your user email.' answer: type: 'text' validation: 'description': 'email address' 'type': 'string' 'format': 'email' 'maxLength': 64 required: true question: 'Please enter employee Num.' answer: type: 'text' validation: 'description': 'employee Number' 'type': 'integer' 'minimum': 100 'maximum': 600 required: false question: 'Please enter gender enum[female, male, unspecified]' answer: type: 'choice' options: match: 'unspecified' match: 'male' match: 'female' required: false ; module { let switchBoard = ; robot;};
How to define a dynamic message model:
- onCompleteMessage:
String
// reply sent to the user when the conversation is done (optional) - skipKeyword:
String
// default 'skip', a keyword that can be used to skip non-required questions (optional) - skipMessage:
String
// a message that can be appended to any non-required questions (optional) - type: "dynamic" // conversation schema type must be 'dynamic' (required)
- steps:
Array
, define properties.
steps: question: String // question to ask the user (required) answer: type: String // could be 'choice', 'text' (required) options: // add the options object if the `type` of answer is `choice` match: String // what robot should listen to - can be a regex validation: Object // validate input, refer json shcema (optional) required: Boolean
Third pattern: custom
Example
let conversation = switchBoardstartmsg 'create user(custom)' const function1 = { conversation message conversation conversation} const function2 = { conversation message conversation conversation} const function3 = { conversation message conversation conversation} const function4 = { conversation message conversation;} const function5 = { conversation; message} msgconversationconversationconversation
robot.receiveMiddleware integration
Example
const _ = ;const Dialog = ; module { if !_ true; return { let msg = contextresponse; let robot = msgrobot; robotlogger; let dialog = robot; if ! return ; let existsConversation = dialog; if existsConversation let receiverUserId = dialog; let conversation = dialog; conversation; return ; };}; //call the robot.receiveMiddleware method to register a conversationMiddleware in your scriptrobot;
Conversation manage
Example
module { let switchBoard = ; let existsConversation = switchBoard; if !existsConversation return msg; let userId = switchBoard; let currentConversation = switchBoard; let conversations = switchBoard;};
More Conversation manage Examples.
API
Conversation Manager API
initManager()
Return a conversation manager instance.(singleton is recommended).
- robot:
Hubot.Robot
- type: (optional) 'user' or 'room', default 'user'.It defines if this conversation is with the whole room or with a particular user only. If the message comes from a user (or a room) that we're having a conversation with, it will be processed as the next step in an ongoing Dialog.
- callback: (optional) The callback should be return a
Boolean
, when the return value istrue
and there is a active conversation of the user (or the room), it will be processed as the next step in an ongoing Dialog. - singleton:
Boolean
,(optional) defaulttrue
. Enable the singleton.
initSchema()
Returns a new conversation schema object.
- schemaName: instance of hubot's Robot.
- schema: json schema or dynamic message model.
initSchema
used for json schema pattern
or dynamic message model pattern
only.
start()
Returns a new conversation object, with a default expire time 1m.
- msg: An incoming message heard / responded to by the robot. eg:
robot
- conversationName: conversation name.
- schema: schema object (optional), used for
json schema pattern
ordynamic message model pattern
only. - expireTime:
Number
, (optional) expire time (ms).
robot
More Conversation Manager API.
Conversation API
Conversation is an instance of an EventEmitter, It will emit an end
event with conversation.allAnswers
when the flow is done.
It has two other events expire
and close
.
conversation;
addChoice()
Adds a listener choice to this Dialog. If a message is received that matches the choice regex, the handler will be executed. only for custom pattern.
- regex: a regular expresion that will be aplied to the incoming message from the receive function
- handler: function(message), A function that is executed against a successfully matched message. The match property of the original
updateQuestion()
Update last question. only for custom pattern.
- value:
String
- question
updateAnswers()
Update all answers. only for custom pattern.
- value:
String
- answer
close()
Emit an close event with all all answers, and close the current conversation. only for custom pattern.