Note that this requires billing to be activated if the website is not a google resource.
Note that the async/await and particularly the async in red
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const fetch = require("node-fetch");
//
// Web Fetch function
//
async function getData(url) {
try {
const res = await fetch(url) ;
const txt = await res.text() ;
console.log("TEXT: " + txt) ;
return txt ;
} catch(err) {
console.log("ERR: " + err) ;
throw err.replace(url, "website") ;
}
}
//
// Exported Functions
//
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });// My Function Handler
async function myfunction(agent) {
var url="https://url.to.fetch/" ;
await getData(url).then((txt) => {
console.log("DATA: " + txt) ;
process txt
agent.add(...) ;
}).catch((err) => {
console.log("FAILED: " + err) ;
agent.add("Error - " + err);
}) ;
}
//
// Setup
//
let intentMap = new Map();
//intentMap.set('Default Welcome Intent', welcome);
//intentMap.set('Default Fallback Intent', fallback);
intentMap.set('forecast', forecast) ;
agent.handleRequest(intentMap);
});
No comments:
Post a Comment