Skip to content

Instantly share code, notes, and snippets.

@savelee
Last active April 9, 2021 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save savelee/15a56cd06e3efe80474e4cd80b901910 to your computer and use it in GitHub Desktop.
Save savelee/15a56cd06e3efe80474e4cd80b901910 to your computer and use it in GitHub Desktop.
Actions on Google SDK
{
"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"
}
// 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