|
| 1 | +const functions = require('firebase-functions'); |
| 2 | +const admin = require('firebase-admin'); |
| 3 | +// To avoid deployment errors, do not call admin.initializeApp() in your code |
| 4 | + |
| 5 | +const {Storage} = require('@google-cloud/storage'); |
| 6 | +const {SessionsClient} = require('@google-cloud/dialogflow-cx').v3beta1; |
| 7 | + |
| 8 | +credentials = { |
| 9 | + "type": "service_account", |
| 10 | + "project_id": "duet-ai-roadshow-415022", |
| 11 | + "private_key_id": "*", |
| 12 | + "private_key": "*", |
| 13 | + "client_email": "goog-sc-aiml-image-process-847@duet-ai-roadshow-415022.iam.gserviceaccount.com", |
| 14 | + "client_id": "108558154597929683744", |
| 15 | + "auth_uri": "https://accounts.google.com/o/oauth2/auth", |
| 16 | + "token_uri": "https://oauth2.googleapis.com/token", |
| 17 | + "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", |
| 18 | + "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/goog-sc-aiml-image-process-847%40duet-ai-roadshow-415022.iam.gserviceaccount.com", |
| 19 | + "universe_domain": "googleapis.com" |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Example for regional endpoint: |
| 24 | + * const location = 'us-central1' |
| 25 | + * const client = new SessionsClient({apiEndpoint: 'us-central1-dialogflow.googleapis.com', credentials: credentials}) |
| 26 | + */ |
| 27 | +const client = new SessionsClient({apiEndpoint: 'us-central1-dialogflow.googleapis.com'}); |
| 28 | + |
| 29 | +// onCall(async (data, context) |
| 30 | +exports.getTrivia = functions.region('uscentral-1').runWith({memory: '128MB'}).https.onRequest(async (req, res) => { |
| 31 | + // CORS handling |
| 32 | + res.set('Access-Control-Allow-Origin', "*"); |
| 33 | + res.set('Access-Control-Allow-Methods', 'GET, POST'); |
| 34 | + res.setHeader( |
| 35 | + "Access-Control-Allow-Headers", |
| 36 | + "X-Requested-With,content-type" |
| 37 | + ); |
| 38 | + |
| 39 | + let token = req.body.token || req.query.token; |
| 40 | + let slToken = process.env.SL_TOKEN || "*"; |
| 41 | + if (token !== slToken) { |
| 42 | + res.status(401).send("Invalid token"); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + let projectId = process.env.PROJECT_ID || "duet-ai-roadshow-415022"; |
| 47 | + |
| 48 | + // GOOGLE_APPLICATION_CREDENTIALS ??? |
| 49 | + // https://stackoverflow.com/questions/52635632/setting-google-application-credentials-in-current-shell-session-via-node-js |
| 50 | + const storage = new Storage({ |
| 51 | + projectId: projectId, |
| 52 | + credentials |
| 53 | + }); |
| 54 | + |
| 55 | + let locationId = req.body.location_id || req.query.location_id || process.env.LOCATION_ID || 'us-central1'; |
| 56 | + let agentId = req.body.agent_id || req.query.agent_id || process.env.AGENT_ID; |
| 57 | + let languageCode = req.body.language_code || req.query.language_code || process.env.LANGUAGE_CODE || 'en' |
| 58 | + let sessionId = req.body.session_id || req.query.session_id; |
| 59 | + const sessionPath = client.projectLocationAgentSessionPath( |
| 60 | + projectId, |
| 61 | + locationId, |
| 62 | + agentId, |
| 63 | + sessionId |
| 64 | + ); |
| 65 | + |
| 66 | + let query = req.body.query || req.query.query || ""; |
| 67 | + |
| 68 | + const request = { |
| 69 | + session: sessionPath, |
| 70 | + queryInput: { |
| 71 | + text: { |
| 72 | + text: query, |
| 73 | + }, |
| 74 | + languageCode, |
| 75 | + }, |
| 76 | + }; |
| 77 | + const [response] = await client.detectIntent(request); |
| 78 | + let fullResponse = ""; |
| 79 | + for (const message of response.queryResult.responseMessages) { |
| 80 | + if (message.text) { |
| 81 | + fullResponse += message.text.text; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + res.json(JSON.parse(fullResponse)); |
| 86 | +}); |
0 commit comments