Separate intents to separate modules
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
8
bot/facebook/intents/common/index.js
Normal file
8
bot/facebook/intents/common/index.js
Normal file
@@ -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,
|
||||
}
|
||||
14
bot/facebook/intents/debug.js
Normal file
14
bot/facebook/intents/debug.js
Normal file
@@ -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,
|
||||
}
|
||||
16
bot/facebook/intents/default.js
Normal file
16
bot/facebook/intents/default.js
Normal file
@@ -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,
|
||||
}
|
||||
17
bot/facebook/intents/drink.js
Normal file
17
bot/facebook/intents/drink.js
Normal file
@@ -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,
|
||||
}
|
||||
20
bot/facebook/intents/index.js
Normal file
20
bot/facebook/intents/index.js
Normal file
@@ -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,
|
||||
}
|
||||
14
bot/facebook/intents/start.js
Normal file
14
bot/facebook/intents/start.js
Normal file
@@ -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,
|
||||
}
|
||||
Reference in New Issue
Block a user