1
0

Separate intents to separate modules

This commit is contained in:
2018-04-22 22:02:53 +02:00
parent 0f1d9e7f39
commit b3bc949e01
7 changed files with 92 additions and 50 deletions

View File

@@ -1,53 +1,6 @@
const fbTemplate = require('claudia-bot-builder/lib/facebook/format-message') const intents = require('./intents')
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) => { module.exports = async (message, recipient) => {
const intent = getIntent(normalize(message.text)) const run = intents.get(message)
run(message, recipient)
// Fallthrough
if (!intent) {
return intents.default.run(message, recipient)
}
intents[intent].run(message, recipient)
} }

View 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,
}

View 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,
}

View 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,
}

View 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,
}

View 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,
}

View 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,
}