Hubot-Conversation
Have a multi-message chat with your bot.
external-scripts.
Reminder, before you start: This does not need to be added toHow it works:
-
Make a new
Conversation
instance aware of your robot:var switchBoard = robot type;
This will register a custom listener allowing the instance to check all incoming messages. Type parameter can take one of two values: user
(default) or room
. 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.
- Given an starting message, create a new Dialog instance and give the dialog choices.
robot
The switchBoard will listen to the next message from the same user (or room) and try to match it to any of the available choices. After a match has been found. It will clear the choices, and end the dialog.
The bot will forget about your dialog after a default timeout of 30 seconds.
Use
On your hubot script:
var Conversation = ;module { var switchBoard = robot; robot; robot; robot;};
This will give you the following interactions:
hubot> hubot clean the househubot> Shell: Sure, where should I start? Kitchen or Bathroomhubot> Kitchenhubot> Shell: On it boss!hubot> hubot clean the househubot> Shell: Sure, where should I start? Kitchen or Bathroomhubot> Bathroomhubot> Shell: Do I really have to?hubot> yeshubot> Shell: Fine, Mom!
hubot> hubot jumphubot> Shell: Sure, How many times?hubot> 3hubot> ready to jump 3 3* Jumps* Jumps* Jumps
hubot> hubot what's the mission?hubot> Shell: Your have 5 seconds to accept your mission, or this message will self-destruct//5 seconds later* Boom
API
Conversation
startDialog(msg, [timeout])
Returns a new Dialog object, with a default timeout of 30000ms.
msg: An incoming message heard / responded to by the robot eg:
robot
timeout: optional time in ms before the conversaion expires.
The Dialog object has a default timeout
function property which can be overriden to specify the timeout behavior
talkingTo(id)
Returns the Dialog being held with a given user
id: The Id of the user with whom a dialog is taking place.
Dialog
A multiple-choice message handler
receive(msg)
Process an incoming message to see if any choice matches it.
msg A hubot message
addChoice(regex, handler)
Adds a listener choice to this Dialog. This works just like hubots hear
listener.
If a message is received that matches the choice regex
, the handler will be executed.
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
message is replaced by the match against this regex, so you can retrieve matched groups.
getChoices()
Returns the array of choices
resetChoices()
Clears the choices.
dialogTimeout -> function(message){ }
Default timeout handler. You can override this property with your own function.