Skip to content

Instantly share code, notes, and snippets.

@jonchurch
Last active February 1, 2019 03:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonchurch/410ea5b86cc3f251397452bfbfad7bfc to your computer and use it in GitHub Desktop.
Save jonchurch/410ea5b86cc3f251397452bfbfad7bfc to your computer and use it in GitHub Desktop.
Example of two ways to chain questions in botkit v0.5.1
// drop this file into your skills folder if using a botkit-starter kit
// or just copy paste whats inside this function into your bot script
module.exports = function(controller) {
// Chaining multiple questions together using callbacks
// You have to call convo.next() in each callback in order to keep the conversation flowing
controller.hears('qq', 'message_received', function(bot, message) {
bot.startConversation(message, function(err, convo) {
convo.say('Lets get to know each other a little bit!')
convo.ask('Which is more offensive? Book burning or flag burning?', function(res, convo) {
convo.next()
convo.ask('How often do you keep your promises?', function(res, convo) {
convo.next()
convo.ask('Which is bigger? The Sun or the Earth?', function(res, convo) {
convo.say('Thank you, that is all for now')
convo.next()
})
})
})
})
})
// Method using threads and addQuestion
// Helps you avoid callback hell
// Docs for addQuestion https://github.com/howdyai/botkit/blob/master/readme.md#convoaddquestion
// Don't forget to pass an empty object after the callback and before the thread you're adding the question to!
controller.hears('thread', 'message_received', function(bot, message) {
bot.startConversation(message, function(err, convo) {
convo.addMessage('Charmed to meet you, lets get to know one another!')
convo.addQuestion('How much do you like robots?', function(res, convo) {
convo.gotoThread('q2')
}, {}, 'default')
convo.addQuestion('Do you like your job?', function(res, convo) {
convo.gotoThread('q3')
}, {}, 'q2')
convo.addQuestion('How much glucose and energy does your body generate per hour?', function(res, convo) {
convo.gotoThread('end')
}, {}, 'q3')
convo.addMessage('Okay thank you very much for the valuable info, human.', 'end')
})
})
}
@jonchurch
Copy link
Author

This is a simple example of asking multiple, non-branching questions in botkit.
We aren't storing any of the responses in this example, but you could use convo.setVar('varName', res.text) to set the variable inside the conversation's scope. Otherwise you can do whatever you please with the responses!

@ouadie-lahdioui
Copy link

Which one you recommend to build some complex conversations ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment