const fbTemplate = require('claudia-bot-builder/lib/facebook/format-message') const fbReply = require('claudia-bot-builder/lib/facebook/reply') const normalize = str => str.toLowerCase() const send = (recipientId, message) => fbReply(recipientId, message, process.env.FB_ACCESS_TOKEN) const intents = { default: { keywords: [], run(msg, recipient) { const reply = new fbTemplate.Text( `Sorry, ${ recipient.first_name }. I am a young Bot and still learning. Type "Start" to show the start over.` ).get() send(recipient.id, reply) }, }, start: { keywords: ['start', 'menu', 'help'], run(msg, recipient) { const reply = new fbTemplate.Text(` This is your menu. You can reach it by writing Menu, or Help, or Start `).get() send(recipient.id, reply) }, }, drink: { keywords: ['dring', 'water', 'gimmme'], run(msg, recipient) { const reply = new fbTemplate.Text(` So, you'd like to drink? `).get() send(recipient.id, reply) }, }, } const getIntent = msg => Object.keys(intents).find(k => intents[k].keywords.indexOf(msg) !== -1) module.exports = async (message, recipient) => { const intent = getIntent(normalize(message.text)) // Fallthrough if (!intent) { return intents.default.run(message, recipient) } intents[intent].run(message, recipient) }