Add simple intents
This commit is contained in:
53
bot/facebook/conversation.js
Normal file
53
bot/facebook/conversation.js
Normal file
@@ -0,0 +1,53 @@
|
||||
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)
|
||||
|
||||
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)
|
||||
}
|
||||
@@ -5,9 +5,9 @@ const FB = require('fb')
|
||||
FB.setAccessToken(process.env.FB_ACCESS_TOKEN)
|
||||
|
||||
const fbParser = require('claudia-bot-builder/lib/facebook/parse')
|
||||
const fbReply = require('claudia-bot-builder/lib/facebook/reply')
|
||||
const fbValidate = require('claudia-bot-builder/lib/facebook/validate-integrity')
|
||||
const fbTemplate = require('claudia-bot-builder/lib/facebook/format-message')
|
||||
|
||||
const conversation = require('./conversation')
|
||||
|
||||
/**
|
||||
* In-Memory cache for FB graph results
|
||||
@@ -62,11 +62,12 @@ const respond = async (req, res) => {
|
||||
// Cache recipients
|
||||
recipients[message.sender] = recipient
|
||||
|
||||
const replyMessage = new fbTemplate.Text(
|
||||
`${recipient.first_name} said: "${message.text}"`
|
||||
).get()
|
||||
|
||||
fbReply(recipient.id, replyMessage, process.env.FB_ACCESS_TOKEN)
|
||||
try {
|
||||
await conversation(message, recipient)
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
res.end(err.message)
|
||||
}
|
||||
|
||||
res.end()
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user