Last active
April 9, 2021 10:21
-
-
Save savelee/15a56cd06e3efe80474e4cd80b901910 to your computer and use it in GitHub Desktop.
Actions on Google SDK
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
{ | |
"actions": [ | |
{ | |
"description": "Default Welcome Intent", | |
"name": "MAIN", | |
"fulfillment": { | |
"conversationName": "welcome" | |
}, | |
"intent": { | |
"name": "actions.intent.MAIN", | |
"trigger": { | |
"queryPatterns":["talk to CCAIDemo"] | |
} | |
} | |
}, | |
{ | |
"description": "Dialogflow Intents", | |
"name": "TEXT", | |
"fulfillment": { | |
"conversationName": "dialogflow_intent" | |
}, | |
"intent": { | |
"name": "actions.intent.TEXT", | |
"trigger": { | |
"queryPatterns": [] | |
} | |
} | |
} | |
], | |
"conversations": { | |
"welcome": { | |
"name": "welcome", | |
"url": "https://5d9233546d75.ngrok.io/aog/" | |
}, | |
"dialogflow_intent": { | |
"name": "dialogflow_intent", | |
"url": "https://5d9233546d75.ngrok.io/aog/" | |
} | |
}, | |
"locale": "en" | |
} | |
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
// Import the appropriate service and chosen wrappers | |
const { actionssdk } = require('actions-on-google'); | |
const dialogflow = require('@google-cloud/dialogflow'); | |
const uuid = require('uuid'); | |
// Create an app instance | |
const app = actionssdk() | |
// Register handlers for Actions SDK intents | |
app.intent('actions.intent.MAIN', async (conv) => { | |
// Detect Dialogflow Intent based on Welcome Event | |
var queryInput = { | |
event: { | |
name: 'WELCOME', | |
languageCode: 'en' | |
} | |
}; | |
var response = await dialogflowDetection(queryInput); | |
// Let the Google Assistant speak it out | |
conv.ask(response.response.queryResult.fulfillmentText); | |
}) | |
app.intent('actions.intent.TEXT', (conv, input) => { | |
// Pass all the text input (transcribed from Google Assistant Speech to Text) | |
// To Dialogflow, to do an intent detection on text. | |
var queryInput = { | |
text: { | |
text: input, | |
languageCode: 'en' | |
} | |
}; | |
var response = await dialogflowDetection(queryInput); | |
// Let the Google Assistant speak it out | |
conv.ask(response.response.queryResult.fulfillmentText); | |
}); | |
async function dialogflowDetection(){ | |
const sessionId = uuid.v4(); | |
// Create a new session | |
const sessionClient = new dialogflow.SessionsClient(); | |
const sessionPath = sessionClient.projectAgentSessionPath(projectId, sessionId); | |
const request = { | |
session: this.sessionPath, | |
queryInput: qInput, | |
queryParams: null | |
}; | |
const [response] = await this.sessionClient.detectIntent(request); | |
return response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment