Skip to content

Instantly share code, notes, and snippets.

@savelee
Last active March 2, 2020 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save savelee/0453fa5b009de4b18eaa9ab16bf2a112 to your computer and use it in GitHub Desktop.
Save savelee/0453fa5b009de4b18eaa9ab16bf2a112 to your computer and use it in GitHub Desktop.
Dialogflow & Sanity Headless CMS
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