Created
November 18, 2018 05:34
-
-
Save senyoltw/b3d391e3e88ee17c33943191fc3fd6d1 to your computer and use it in GitHub Desktop.
dialogflow_get_intent.js
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
exports.handler = (context, event, callback) => { | |
// メモ:事前にFunctions->設定->npmでdialogflowを導入しておく必要がある | |
// メモ:gcpよりService account keyのjsonを手に入れAssetsで「プライベート」でアップロードしておく | |
console.log('Event: ' + JSON.stringify(event)); | |
// DialogFlow エージェント・認証の指定 | |
const projectId = 'すきなエージェントの名前をいれてね!'; | |
let keypath = Runtime.getAssets()['すきなエージェントと関連付けられたkeyファイルを指定してね!'].path; | |
process.env.GOOGLE_APPLICATION_CREDENTIALS = keypath; | |
// DialogFlow 変数設定 | |
const sessionId = event.call_sid; | |
const query = event.speech_results; | |
const languageCode = 'ja-JP'; | |
// Instantiate a DialogFlow client. | |
const dialogflow = require('dialogflow'); | |
const sessionClient = new dialogflow.SessionsClient(); | |
// Define session path | |
const sessionPath = sessionClient.sessionPath(projectId, sessionId); | |
// The text query request. | |
const request = { | |
session: sessionPath, | |
queryInput: { | |
text: { | |
text: query, | |
languageCode: languageCode, | |
}, | |
}, | |
}; | |
// Send request and log result | |
sessionClient | |
.detectIntent(request) | |
.then(responses => { | |
const result = responses[0].queryResult; | |
console.log(`Query: ${result.queryText}`); | |
console.log(`Response: ${result.fulfillmentText}`); | |
let status; | |
if (result.allRequiredParamsPresent) { | |
status = 'complete'; | |
} | |
else { | |
status = 'in-progress'; | |
} | |
const intent_response = result.fulfillmentText; | |
const returnJson = {'status': status, 'intent_response': intent_response, 'intent_name': result.intent.displayName}; | |
console.log(JSON.stringify(returnJson)); | |
callback(null, returnJson); | |
}) | |
.catch(err => { | |
callback(err); | |
console.error('ERROR:', err); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment