1
0

Improve chat flow with answers

This commit is contained in:
2018-04-23 17:28:42 +02:00
parent b3bc949e01
commit 49b91c0a36
10 changed files with 226 additions and 54 deletions

View File

@@ -1,20 +1,40 @@
const flatten = require('flatten')
const load = intents => flatten(intents.map(name => require(`./${name}`)))
const intents = load(['default', 'start', 'drink', 'debug'])
const normalize = str => str.toLowerCase()
const intents = {
default: require('./default'),
start: require('./start'),
drink: require('./drink'),
debug: require('./debug'),
}
const matchIntent = str =>
intents
.map(i => (i.keywords.indexOf(normalize(str)) > -1 ? i.run : false))
.find(f => f !== false)
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
const defaultIntent = intents.find(i => i.id === 'default').run
let intent = matchIntent(msg.text)
if (!intent) {
if (msg.quick_reply) {
intent = matchIntent(msg.quick_reply.payload)
}
}
return intent || defaultIntent
}
const run = (msg, recipient) => {
const debugIntent = intents.find(i => i.id === 'debug').run
const intent = get(msg)
if (intent === debugIntent) {
return debugIntent(msg, recipient, intents)
}
return intent(msg, recipient)
}
module.exports = {
get,
run,
}