Last active
March 2, 2020 18:28
-
-
Save savelee/0453fa5b009de4b18eaa9ab16bf2a112 to your computer and use it in GitHub Desktop.
Dialogflow & Sanity Headless CMS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const functions = require('firebase-functions'); | |
const sanityClient = require('@sanity/client'); | |
const { | |
WebhookClient, | |
Payload | |
} = require('dialogflow-fulfillment'); | |
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements | |
const client = sanityClient({ | |
projectId: 'TODO', //TODO your project id here | |
dataset: 'production', | |
token: '', // or leave blank to be anonymous user | |
useCdn: false // `false` if you want to ensure fresh data | |
}); | |
function responses(agent){ | |
var response = ""; | |
if(agent.locale == 'fr'){ | |
response = finalResults.responseFr; | |
} else { | |
response = finalResults.responseEn; | |
} | |
console.log(finalResults); | |
console.log(response); | |
agent.add(response); | |
} | |
var finalResults = ""; | |
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => { | |
let agent = new WebhookClient({ request, response }); | |
const params = { | |
id: agent.intent, | |
paramUserAction: agent.parameters.paramUserAction, | |
paramSystem: agent.parameters.paramSystem, | |
language: agent.locale | |
}; | |
console.log(params); | |
const query = `*[_type == 'simpleresponse' && | |
id == $id | |
&& paramUserAction == $paramUserAction | |
&& paramSystem == $paramSystem]`; | |
client.fetch(query, params).then(json => { | |
json.forEach(result => { | |
finalResults = result; | |
}); | |
let intentMap = new Map(); | |
intentMap.set('333', responses); | |
// list all the other intents | |
agent.handleRequest(intentMap); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment