|
| 1 | +// |
| 2 | +// Stores a user choice in botkit 'users' storage, so that the value can be retreived later |
| 3 | +// |
| 4 | +module.exports = function (controller) { |
| 5 | + |
| 6 | + controller.hears([/^storage$/], 'direct_message,direct_mention', function (bot, message) { |
| 7 | + |
| 8 | + // Check if a User preference already exists |
| 9 | + var userId = message.original_message.personId; |
| 10 | + controller.storage.users.get(userId, function (err, data) { |
| 11 | + if (err) { |
| 12 | + bot.reply(message, 'could not access storage, err: ' + err.message, function (err, message) { |
| 13 | + bot.reply(message, 'sorry, I am not feeling well \uF613! try again later...'); |
| 14 | + }); |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + // User preference found |
| 19 | + if (data) { |
| 20 | + // Show user preference |
| 21 | + showUserPreference(controller, bot, message, userId, data.value); |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + // Ask for favorite color |
| 26 | + askForFavoriteColor(controller, bot, message, userId); |
| 27 | + }); |
| 28 | + }); |
| 29 | +} |
| 30 | + |
| 31 | +function showUserPreference(controller, bot, message, userId, color) { |
| 32 | + bot.startConversation(message, function (err, convo) { |
| 33 | + |
| 34 | + convo.sayFirst(`Hey, I know you <@personId:${userId}>!<br/> '${color}' is your favorite color.`); |
| 35 | + |
| 36 | + convo.ask("Should I remove your preference?", [ |
| 37 | + { |
| 38 | + pattern: "^yes|ya|da|si|oui$", |
| 39 | + callback: function (response, convo) { |
| 40 | + |
| 41 | + // Remove user preferences |
| 42 | + controller.storage.users.remove(userId, function (err) { |
| 43 | + if (err) { |
| 44 | + convo.say(message, 'sorry, could not access storage, err: ' + err.message); |
| 45 | + convo.repeat(); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + convo.say("Successfully reset your color preference."); |
| 50 | + convo.next(); |
| 51 | + }); |
| 52 | + |
| 53 | + }, |
| 54 | + }, |
| 55 | + { |
| 56 | + default: true, |
| 57 | + callback: function (response, convo) { |
| 58 | + convo.say("Got it, leaving your color preference as is."); |
| 59 | + convo.next(); |
| 60 | + } |
| 61 | + } |
| 62 | + ]); |
| 63 | + }); |
| 64 | +} |
| 65 | + |
| 66 | +function askForFavoriteColor(controller, bot, message, userId) { |
| 67 | + bot.startConversation(message, function (err, convo) { |
| 68 | + |
| 69 | + convo.ask("What is your favorite color?", [ |
| 70 | + { |
| 71 | + pattern: "^blue|green|pink|red|yellow$", |
| 72 | + callback: function (response, convo) { |
| 73 | + |
| 74 | + // Store color as user preference |
| 75 | + var pickedColor = convo.extractResponse('answer'); |
| 76 | + var userPreference = { id: userId, value: pickedColor }; |
| 77 | + controller.storage.users.save(userPreference, function (err) { |
| 78 | + if (err) { |
| 79 | + convo.say(message, 'sorry, could not access storage, err: ' + err.message); |
| 80 | + convo.next(); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + convo.transitionTo("success", `_stored user preference_`); |
| 85 | + }); |
| 86 | + |
| 87 | + }, |
| 88 | + }, |
| 89 | + { |
| 90 | + default: true, |
| 91 | + callback: function (response, convo) { |
| 92 | + convo.say("Sorry, I don't know this color. Try another one..."); |
| 93 | + convo.repeat(); |
| 94 | + convo.next(); |
| 95 | + } |
| 96 | + } |
| 97 | + ], { key: "answer" }); |
| 98 | + |
| 99 | + // Success thread |
| 100 | + convo.addMessage( |
| 101 | + "Cool, I love '{{responses.answer}}' too", |
| 102 | + "success"); |
| 103 | + }); |
| 104 | +} |
0 commit comments