Instantly share code, notes, and snippets.

Embed
What would you like to do?
dialogflow_get_intent.js
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