Created
March 15, 2017 01:15
-
-
Save jonchurch/afaa4b4ae7286e9f5a0199d48c675428 to your computer and use it in GitHub Desktop.
Example of two ways to chain questions in botkit v0.5.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.createConversation(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') | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the nice tips :)
for a working example, remember to add
convo.activate();
Have fun and thanks again