|
| 1 | +/** |
| 2 | + * Copyright 2018, Google, LLC. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +'use strict'; |
| 17 | +async function main( |
| 18 | + projectId = 'YOUR_PROJECT_ID', |
| 19 | + sessionId = 'YOUR_SESSION_ID', |
| 20 | + query = 'YOUR_QUERY', |
| 21 | + languageCode = 'YOUR_LANGUAGE_CODE' |
| 22 | +) { |
| 23 | + // [START dialogflow_detect_intent_with_sentiment_analysis] |
| 24 | + // Imports the Dialogflow client library |
| 25 | + const dialogflow = require('dialogflow').v2; |
| 26 | + |
| 27 | + // Instantiate a DialogFlow client. |
| 28 | + const sessionClient = new dialogflow.SessionsClient(); |
| 29 | + |
| 30 | + /** |
| 31 | + * TODO(developer): Uncomment the following lines before running the sample. |
| 32 | + */ |
| 33 | + // const projectId = 'ID of GCP project associated with your Dialogflow agent'; |
| 34 | + // const sessionId = `user specific ID of session, e.g. 12345`; |
| 35 | + // const query = `phrase(s) to pass to detect, e.g. I'd like to reserve a room for six people`; |
| 36 | + // const languageCode = 'BCP-47 language code, e.g. en-US'; |
| 37 | + |
| 38 | + // Define session path |
| 39 | + const sessionPath = sessionClient.sessionPath(projectId, sessionId); |
| 40 | + |
| 41 | + async function detectIntentandSentiment() { |
| 42 | + // The text query request. |
| 43 | + const request = { |
| 44 | + session: sessionPath, |
| 45 | + queryInput: { |
| 46 | + text: { |
| 47 | + text: query, |
| 48 | + languageCode: languageCode, |
| 49 | + }, |
| 50 | + }, |
| 51 | + queryParams: { |
| 52 | + sentimentAnalysisRequestConfig: { |
| 53 | + analyzeQueryTextSentiment: true, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }; |
| 57 | + |
| 58 | + // Send request and log result |
| 59 | + const responses = await sessionClient.detectIntent(request); |
| 60 | + console.log('Detected intent'); |
| 61 | + const result = responses[0].queryResult; |
| 62 | + console.log(` Query: ${result.queryText}`); |
| 63 | + console.log(` Response: ${result.fulfillmentText}`); |
| 64 | + if (result.intent) { |
| 65 | + console.log(` Intent: ${result.intent.displayName}`); |
| 66 | + } else { |
| 67 | + console.log(` No intent matched.`); |
| 68 | + } |
| 69 | + if (result.sentimentAnalysisResult) { |
| 70 | + console.log(`Detected sentiment`); |
| 71 | + console.log( |
| 72 | + ` Score: ${result.sentimentAnalysisResult.queryTextSentiment.score}` |
| 73 | + ); |
| 74 | + console.log( |
| 75 | + ` Magnitude: ${ |
| 76 | + result.sentimentAnalysisResult.queryTextSentiment.magnitude |
| 77 | + }` |
| 78 | + ); |
| 79 | + } else { |
| 80 | + console.log(`No sentiment Analysis Found`); |
| 81 | + } |
| 82 | + // [END dialogflow_detect_intent_with_sentiment_analysis] |
| 83 | + } |
| 84 | + detectIntentandSentiment(); |
| 85 | +} |
| 86 | +main(...process.argv.slice(2)); |
0 commit comments