Created
November 5, 2019 02:32
-
-
Save priyankavergadia/e2da8d1b994301aef6b5641468f8bb6a to your computer and use it in GitHub Desktop.
Cloud Function for Dialogflow Vision API Integration for Blog
This file contains hidden or 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 functions = require('firebase-functions'); | |
const {google} = require('googleapis'); | |
const {WebhookClient} = require('dialogflow-fulfillment'); | |
const vision = require('@google-cloud/vision'); | |
/** | |
* TODO(developer): Uncomment the following lines before running the sample. | |
*/ | |
const bucketName = 'YOUR-BUCKET-NAME'; | |
const timeZone = 'America/Los_Angeles'; | |
const timeZoneOffset = '-07:00'; | |
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => { | |
const agent = new WebhookClient({ request, response }); | |
console.log("Parameters", agent.parameters); | |
function applyML(agent){ | |
const filename = agent.parameters.filename; | |
console.log("filename is: ", filename); | |
// call vision API to detect text | |
return callVisionApi(agent, bucketName, filename).then(result => { | |
console.log(`result is ${result}`); | |
agent.add(`file is being processed, here are the results: ${result}`); | |
//agent.add(`file is being processed ${result}`); | |
}).catch((error)=> { | |
agent.add(`error occurred at apply ml function` + error); | |
}); | |
} | |
let intentMap = new Map(); | |
intentMap.set('Explore uploaded image', applyML); | |
agent.handleRequest(intentMap); | |
}); | |
async function callVisionApi(agent, bucketName, fileName){ | |
// [START vision_text_detection_gcs] | |
// Imports the Google Cloud client libraries | |
// Creates a client | |
const client = new vision.ImageAnnotatorClient(); | |
try { | |
// Performs text detection on the gcs file | |
const [result] = await client.landmarkDetection(`gs://${bucketName}/${fileName}`); | |
const detections = result.landmarkAnnotations; | |
var detected = []; | |
detections.forEach(text => { | |
console.log(text.description); | |
detected.push(text.description); | |
}); | |
return detected; | |
} | |
catch(error) { | |
console.log('fetch failed', error); | |
return []; | |
} | |
} |
This file contains hidden or 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": "Dialogflow fulfillment for the bike shop sample", | |
"version": "0.0.1", | |
"private": true, | |
"license": "Apache Version 2.0", | |
"author": "Google Inc.", | |
"engines": { | |
"node": "6" | |
}, | |
"scripts": { | |
"lint": "semistandard --fix \"**/*.js\"", | |
"start": "firebase deploy --only functions", | |
"deploy": "firebase deploy --only functions" | |
}, | |
"dependencies": { | |
"firebase-functions": "2.0.2", | |
"firebase-admin": "^5.13.1", | |
"actions-on-google": "2.2.0", | |
"googleapis": "^27.0.0", | |
"dialogflow-fulfillment": "^0.6.1", | |
"@google-cloud/bigquery": "^1.3.0", | |
"@google-cloud/storage": "^2.0.0", | |
"@google-cloud/vision": "^0.25.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Getting "use 'esversion: 8'" next to async function callVisionApi. But if I do that, I get "Incompatible values for the 'esversion' and 'esnext' linting options"