Created
July 7, 2018 04:01
-
-
Save lastday154/b7404143c3b0e1114d38dc5087bc1774 to your computer and use it in GitHub Desktop.
dialogflow
This file contains hidden or 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
| 'use strict'; | |
| const { intentsClient, sessionClient } = require('./config'); | |
| const { jsonToCsv, mergeIntents } = require('./dialogflowHelper'); | |
| const projectId = process.env.DIALOGFLOW_PROJECT_ID; | |
| const detectIntent = (query, languageCode = 'en-Us') => { | |
| // Define session path | |
| const sessionId = Math.random().toString(); | |
| const sessionPath = sessionClient.sessionPath(projectId, sessionId); | |
| // The text query request. | |
| const request = { | |
| session: sessionPath, | |
| queryInput: { | |
| text: { | |
| text: query, | |
| languageCode | |
| } | |
| } | |
| }; | |
| // Send request and log result | |
| return sessionClient | |
| .detectIntent(request) | |
| .then((responses) => responses[0].queryResult) | |
| .catch((err) => { | |
| console.error('ERROR:', err); | |
| }); | |
| }; | |
| const updateIntents = (intents) => { | |
| if (intents.length === 0) { | |
| return; | |
| } | |
| const agentPath = intentsClient.projectAgentPath(projectId); | |
| intentsClient | |
| .batchUpdateIntents({ | |
| parent: agentPath, | |
| intentBatchInline: { | |
| intents | |
| } | |
| }) | |
| .then((responses) => { | |
| const operation = responses[0]; | |
| return operation.promise(); | |
| }) | |
| .then((responses) => { | |
| // The final result of the operation. | |
| const result = responses[0]; | |
| // console.log(result); | |
| }) | |
| .catch((err) => { | |
| console.log(err); | |
| }); | |
| }; | |
| const listIntents = () => { | |
| const projectAgentPath = intentsClient.projectAgentPath(projectId); | |
| const request = { | |
| parent: projectAgentPath | |
| }; | |
| // Send the request for listing intents. | |
| return intentsClient | |
| .listIntents(request) | |
| .then((responses) => responses[0]) | |
| .catch((err) => { | |
| console.error('Failed to list intents:', err); | |
| }); | |
| }; | |
| const getIntent = (intent) => { | |
| const request = { name: intent.name, intentView: 'INTENT_VIEW_FULL' }; | |
| // Send the request for retrieving the intent. | |
| return intentsClient | |
| .getIntent(request) | |
| .then((responses) => responses[0]) | |
| .catch((err) => { | |
| console.error(`Failed to get intent ${intent.displayName}:`, err); | |
| }); | |
| }; | |
| const deleteIntent = (intent) => { | |
| const request = { name: intent.name }; | |
| // Send the request for retrieving the intent. | |
| return intentsClient | |
| .deleteIntent(request) | |
| .then(() => { | |
| console.log(`Intent ${intent.displayName} deleted`); | |
| }) | |
| .catch((err) => { | |
| console.error(`Failed to delete intent ${intent.displayName}:`, err); | |
| }); | |
| }; | |
| const exportIntents = async () => { | |
| try { | |
| const intents = await listIntents(); | |
| const intentPromises = intents.map((intent) => getIntent(intent)); | |
| const intentsData = await Promise.all(intentPromises); | |
| return jsonToCsv(intentsData); | |
| } catch (err) { | |
| console.error('Failed to export intents:', err); | |
| } | |
| }; | |
| const clearIntents = () => | |
| // Send the request for listing intents. | |
| listIntents(projectId) | |
| .then((intents) => Promise.all(intents.map((intent) => deleteIntent(intent)))) | |
| .catch((err) => { | |
| console.error('Failed to list intents:', err); | |
| }); | |
| /** | |
| * | |
| * @param {array} rows | |
| * array contains data in csv file | |
| * Format: [{ question: "how are you', intent: 'age' }] | |
| * @returns {Promise} - The promise which resolves to an array of intents | |
| * [Intent]{@link google.cloud.dialogflow.v2.Intent}. | |
| */ | |
| async function csvToIntent(rows) { | |
| const intentPromises = rows.map(async (row) => { | |
| const result = await detectIntent(row.question); | |
| const intent = await getIntent(result.intent); | |
| const { trainingPhrases } = intent; | |
| trainingPhrases.push({ | |
| type: 'EXAMPLE', | |
| parts: [{ text: row.question }] | |
| }); | |
| intent.trainingPhrases = trainingPhrases; | |
| return intent; | |
| }); | |
| const intents = await Promise.all(intentPromises); | |
| return mergeIntents(intents); | |
| } | |
| module.exports = { | |
| updateIntents, | |
| detectIntent, | |
| clearIntents, | |
| getIntent, | |
| listIntents, | |
| exportIntents, | |
| csvToIntent | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment