Last active
November 18, 2018 08:06
-
-
Save senyoltw/df75b56c3d782340b659638c59fa38a6 to your computer and use it in GitHub Desktop.
dialogflow_get_intent with kintone
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)); | |
//事前にkintone-nodejs-sdkをnpmでインストールする | |
const kintone = require('kintone-nodejs-sdk'); | |
//kintoneAPIトークンによる認証・各種設定 | |
let appID = kintoneのアプリのIDをいれてね!; | |
let myDomainName = '自分のkintoneのURLをいれてね!.cybozu.com'; | |
let APIToken = 'kintoneアプリのAPIトークンを発行していれてね!'; // your API Token | |
let kintoneAuth = new kintone.Auth(); | |
kintoneAuth.setApiToken(APIToken); | |
// DialogFlow エージェント・認証の指定 | |
const projectId = 'すきなエージェントの名前をいれてね!'; | |
let keypath = Runtime.getAssets()['すきなエージェントと関連付けられたkeyファイルを指定してね!'].path; | |
process.env.GOOGLE_APPLICATION_CREDENTIALS = keypath; | |
//自分のkintoneに接続 | |
let kintoneConnection = new kintone.Connection(myDomainName, kintoneAuth); | |
// 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}`); | |
//kintoneに送信 | |
//kintoneで作ったアプリのjson構造にしてね! | |
let recordData = { | |
"Customer": { | |
"value": event.from | |
}, | |
'Detail': { | |
'value': result.intent.displayName | |
} | |
}; | |
kintoneRecord.addRecord(appID, recordData) | |
.then((rsp) => { | |
console.log(rsp); | |
}) | |
.catch((err) => { | |
// This SDK return err with KintoneAPIExeption | |
console.log(err.get()); | |
}); | |
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