Created
January 13, 2020 10:04
-
-
Save Anshul0305/405e050cf438975baec8a746ffda9991 to your computer and use it in GitHub Desktop.
Connecting Dialogflow with External APIs
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
'use strict'; | |
const axios = require('axios'); | |
const functions = require('firebase-functions'); | |
const {WebhookClient} = require('dialogflow-fulfillment'); | |
const {Card, Suggestion} = require('dialogflow-fulfillment'); | |
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements | |
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => { | |
const agent = new WebhookClient({ request, response }); | |
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers)); | |
console.log('Dialogflow Request body: ' + JSON.stringify(request.body)); | |
function welcome(agent) { | |
agent.add(`Welcome to my agent!`); | |
} | |
function fallback(agent) { | |
agent.add(`I didn't understand`); | |
agent.add(`I'm sorry, can you try again?`); | |
} | |
function rhymingWordHandler(agent){ | |
const word = agent.parameters.word; | |
agent.add(`Here are the rhyming words for ${word}`); | |
return axios.get(`https://api.datamuse.com/words?rel_rhy=${word}`) | |
.then((result) => { | |
result.data.map(wordObj => { | |
agent.add(wordObj.word); | |
}); | |
}); | |
} | |
function loopingHandler(agent){ | |
return axios.get(`https://ws.smn.gob.ar/alerts/type/AL`) | |
.then((result) => { | |
result.data.map(wordobj => { | |
let zones = ""; | |
const zone_length = Object.keys(wordobj.zones).length; | |
for(var i = 0; i < zone_length; i++){ | |
zones += `${wordobj.zones[i]} - `; | |
} | |
agent.add(`\n*Titulo:* `+ wordobj.title + `\n *Fecha:* ` + wordobj.date + `\n *Hora:* ` + wordobj.hour+ `\n *Estado:* ` + wordobj.status + `\n *Descripción:* ` + wordobj.description +` \n *zonas:* `+ zones +`-->`); | |
}); | |
}); | |
} | |
// Run the proper function handler based on the matched Dialogflow intent name | |
let intentMap = new Map(); | |
intentMap.set('Default Welcome Intent', welcome); | |
intentMap.set('Default Fallback Intent', fallback); | |
intentMap.set('RhymingWord', rhymingWordHandler); | |
intentMap.set('looping test', loopingHandler); | |
agent.handleRequest(intentMap); | |
}); |
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
{ | |
"name": "dialogflowFirebaseFulfillment", | |
"description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase", | |
"version": "0.0.1", | |
"private": true, | |
"license": "Apache Version 2.0", | |
"author": "Google Inc.", | |
"engines": { | |
"node": "8" | |
}, | |
"scripts": { | |
"start": "firebase serve --only functions:dialogflowFirebaseFulfillment", | |
"deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment" | |
}, | |
"dependencies": { | |
"actions-on-google": "^2.2.0", | |
"firebase-admin": "^5.13.1", | |
"firebase-functions": "^2.0.2", | |
"dialogflow": "^0.6.0", | |
"dialogflow-fulfillment": "^0.5.0", | |
"axios": "0.18.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment