Improve chat flow with answers
This commit is contained in:
@@ -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,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user