From b3bc949e01d13950232306c417d56c91ed62da62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Markovi=C4=87?= Date: Sun, 22 Apr 2018 22:02:53 +0200 Subject: [PATCH] Separate intents to separate modules --- bot/facebook/conversation.js | 53 ++-------------------------- bot/facebook/intents/common/index.js | 8 +++++ bot/facebook/intents/debug.js | 14 ++++++++ bot/facebook/intents/default.js | 16 +++++++++ bot/facebook/intents/drink.js | 17 +++++++++ bot/facebook/intents/index.js | 20 +++++++++++ bot/facebook/intents/start.js | 14 ++++++++ 7 files changed, 92 insertions(+), 50 deletions(-) create mode 100644 bot/facebook/intents/common/index.js create mode 100644 bot/facebook/intents/debug.js create mode 100644 bot/facebook/intents/default.js create mode 100644 bot/facebook/intents/drink.js create mode 100644 bot/facebook/intents/index.js create mode 100644 bot/facebook/intents/start.js diff --git a/bot/facebook/conversation.js b/bot/facebook/conversation.js index 1fede93..03347be 100644 --- a/bot/facebook/conversation.js +++ b/bot/facebook/conversation.js @@ -1,53 +1,6 @@ -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) +const intents = require('./intents') 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) + const run = intents.get(message) + run(message, recipient) } diff --git a/bot/facebook/intents/common/index.js b/bot/facebook/intents/common/index.js new file mode 100644 index 0000000..5dfc5b5 --- /dev/null +++ b/bot/facebook/intents/common/index.js @@ -0,0 +1,8 @@ +const fbReply = require('claudia-bot-builder/lib/facebook/reply') + +const send = (recipientId, message) => + fbReply(recipientId, message, process.env.FB_ACCESS_TOKEN) + +module.exports = { + send, +} diff --git a/bot/facebook/intents/debug.js b/bot/facebook/intents/debug.js new file mode 100644 index 0000000..7b3708c --- /dev/null +++ b/bot/facebook/intents/debug.js @@ -0,0 +1,14 @@ +const fbTemplate = require('claudia-bot-builder/lib/facebook/format-message') +const {send} = require('./common') + +const run = (msg, recipient) => { + const reply = new fbTemplate.Text(` + This is a \`Text\` message + `).get() + send(recipient.id, reply) +} + +module.exports = { + keywords: ['debug'], + run, +} diff --git a/bot/facebook/intents/default.js b/bot/facebook/intents/default.js new file mode 100644 index 0000000..cae24b7 --- /dev/null +++ b/bot/facebook/intents/default.js @@ -0,0 +1,16 @@ +const fbTemplate = require('claudia-bot-builder/lib/facebook/format-message') +const {send} = require('./common') + +const 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) +} + +module.exports = { + keywords: [], + run, +} diff --git a/bot/facebook/intents/drink.js b/bot/facebook/intents/drink.js new file mode 100644 index 0000000..3279df9 --- /dev/null +++ b/bot/facebook/intents/drink.js @@ -0,0 +1,17 @@ +const fbTemplate = require('claudia-bot-builder/lib/facebook/format-message') +const {send} = require('./common') + +const run = (msg, recipient) => { + const reply = new fbTemplate.Text(` + So, you'd like to drink? + `) + .addQuickReply('YES!', 'YES') + .addQuickReply('No, not particulary', 'NO') + .get() + send(recipient.id, reply) +} + +module.exports = { + keywords: ['drink', 'dring', 'water', 'gimmme'], + run, +} diff --git a/bot/facebook/intents/index.js b/bot/facebook/intents/index.js new file mode 100644 index 0000000..6b5fdf9 --- /dev/null +++ b/bot/facebook/intents/index.js @@ -0,0 +1,20 @@ +const normalize = str => str.toLowerCase() + +const intents = { + default: require('./default'), + start: require('./start'), + drink: require('./drink'), + debug: require('./debug'), +} + +const get = msg => { + const text = normalize(msg.text) + const intent = Object.keys(intents).find( + k => intents[k].keywords.indexOf(text) !== -1 + ) + return intent ? intents[intent].run : intents.default.run +} + +module.exports = { + get, +} diff --git a/bot/facebook/intents/start.js b/bot/facebook/intents/start.js new file mode 100644 index 0000000..0aaabb8 --- /dev/null +++ b/bot/facebook/intents/start.js @@ -0,0 +1,14 @@ +const fbTemplate = require('claudia-bot-builder/lib/facebook/format-message') +const {send} = require('./common') + +const 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) +} + +module.exports = { + keywords: ['start', 'menu', 'help'], + run, +}