Google Action Webhook Implementation - node.js

so I just started working on a project. I came across two different implementation of webhook for Google action agent. Can someone please explain, what is the difference between both?
Also which one is more extensible.
First one uses actions-on-google library,
'use strict';
// Imports
// =================================================================================================
const { dialogflow } = require('actions-on-google');
const functions = require('firebase-functions');
// Constants
// =================================================================================================
// Instantiate the Dialogflow client with debug logging enabled.
const app = dialogflow({ debug: true });
// Intents
// =================================================================================================
app.intent('welcome.intent', (conv) => {
conv.ask('Hello from webhook');
});
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);
and second one uses dialogflow-fulfillment,
`'use strict'`;
const functions = require('firebase-functions');
const { WebhookClient } = require('dialogflow-fulfillment');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
function welcome(agent) {
agent.add(`Hello from webhook agent`);
}
let intentMap = new Map();
intentMap.set('welcome.intent', welcome);
agent.handleRequest(intentMap);
});

Both libraries are valid and supported by Google. Which one you use depends on what your goals are.
The actions-on-google library is best if your goal is only to develop Actions. It has support for some of the more advanced functions supported by the AoG platform, but does not support the other platforms that Dialogflow supports.
The dialogflow-fulfillment library is best if you want to support multiple bot platforms using Dialogflow (possibly including the Actions on Google platform).

Related

Cloud Functions Deployment error dialogflow

I was trying to deploy function in dialogflow but it shows an error.
Code:-
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
function welcome(agent) {
agent.add(`Welcome to my agent!`);
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
// intentMap.set('your intent name here', yourFunctionHandler);
// intentMap.set('your intent name here', googleAssistantHandler);
agent.handleRequest(intentMap);
});
Error:- Error happened during Cloud Functions Deployment
Can you please tell me how to solve this problem.
I think there is trouble with creating Cloud Functions Environment.
Cloud you please follow these steps below?
Enable Cloud Build API
Go to your project connected your Dialogflow ES in Google Cloud.
( The way is here. Select gear mark next to agent name in Dialogflow console. At "General" tab you can see "GOOGLE PROJECT". Click the Project ID link then you can move to Google Cloud.)
Search "Cloud Build API" at the search bar. Move to Cloud Build API page. "Enable" Cloud Build API.
If the page shows "API Enabled", then this finished.
Redeploy the Cloud Functions.
At Dialogflow Console, redeploy the source.
I hope this works for you.

Dialogflow - a very basic question about fulfilment intent not showing using NodeJS

I have spent one hour searching so I gave up. This question is a little silly.
My intent is to have a specific reply based on parameter received. For example, if a user says “I’m angry because of my boss” then the reply will be “boss is the worst” but if it’s about “wife” then it will say “remember that you promise to love her forever, your mistake”
Anyway, based on my reading I should be using app.intent but I’m not sure how to use it yet
My current question is, this fulfilment is returning empty
const functions = require('firebase-functions');
const {
dialogflow
} = require('actions-on-google');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const app = dialogflow();
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
function angerEmotionCapture(agent) {
let conv = agent.conv();
conv.ask('Hello from the Actions on Google client library!');
agent.add(conv);
}
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
intentMap.set('angerEmotionCapture', angerEmotionCapture);
// intentMap.set('your intent name here', googleAssistantHandler);
agent.handleRequest(intentMap);
});

How to get the current location of a user in Dialogflow chatbot?

I am trying to get the user location in Dialogflow, but so far I am not able to get it.
I have found information showing how to get the location in Dialogflow using Google assistance like this article. The thing is that I just want to do it with a chatbot and get the response there. I have tried as well the code below but still not working:
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const {Permission} = require('actions-on-google');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
function requestPermission(agent) {
agent.requestSource = agent.ACTIONS_ON_GOOGLE;
let conv = agent.conv();
console.log('conv '+conv);
conv.ask(new Permission({
context: 'to locate you',
permissions: 'DEVICE_PRECISE_LOCATION',
}));
agent.add(conv); // Please add this
}
function welcome(agent) {
agent.add(`Welcome to my agent!`);
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
intentMap.set('request_permission', requestPermission);
agent.handleRequest(intentMap);
});
I added an intent call "request_permission". I will appreciate any help or recomendation.
There is no general solution in Dialogflow to get the location. Not all systems that work with Dialogflow have a way to transmit location automatically, and others use different means via messages.
The solution for Actions on Google just works with the Google Assistant.

Dialogflow Detect Intent Fulfillment

Hi i have created a dialogflow nodejs backend which detects intents using the client library for nodejs.
const sessionPath = this.sessionClient.sessionPath(this.configService.get('dialogFlowProjectId'), sessionId);
const request = {
session: sessionPath,
queryInput: {
text: {
text: query,
languageCode: "en-US"
}
}
};
// Send request and log result
Logger.log(request);
const responses = await this.sessionClient.detectIntent(request);
this works fine but I also want to trigger a fulfillment for certain intents.
I have setup a webhook url - this works fine when you use the chat in the dialogflow console. But when I use the method that I have created to send the request to dialogflow the webhook doesn't get called and goes to fallback intent. I'm calling the same intent through the dialogflow console chat and through my own API.
How can I trigger the webhook call when I use the client library API?
First, remember that you don't "call an Intent", either through the test console or through the API. What you can do is send query text that should be matched by an Intent.
If you have set the Intent so that it should call the Fulfillment webhook, then this should happen no matter the source, as long as the Intent itself is triggered.
That the Fallback Intent is getting triggered instead suggests that something about the query text isn't matching the Intent you think it should be matching. I would check to make sure the text you're sending in query should match the training phrases for the Intent in question, that you have no Input Contexts, and that your agent is set for the "en-US" language code.
For routing multiple intents through a webhook you're going to want to have a handler.js file with an express router in it...
const def = require('./intents/default')
const compression = require('compression')
const serverless = require('serverless-http')
const bodyParser = require('body-parser')
const { WebhookClient } = require('dialogflow-fulfillment')
const express = require('express')
const app = express()
// register middleware
app.use(bodyParser.json({ strict: false }))
app.use(function (err, req, res, next) {
res.status(500)
res.send(err)
})
app.use(compression())
app.post('/', async function (req, res) {
// Instantiate agent
const agent = new WebhookClient({ request: req, response: res })
const intentMap = new Map()
intentMap.set('Default Welcome Intent', def.defaultWelcome)
await agent.handleRequest(intentMap)
})
module.exports.server = serverless(app)
As you can see, this handler.js is using serverless-http and express.
Your intent.js function could look something like this ...
module.exports.defaultWelcome = async function DefaultWelcome (agent) {
const responses = [
'Hi! How are you doing?',
'Hello! How can I help you?',
'Good day! What can I do for you today?',
'Hello, welcoming to the Dining Bot. Would you like to know what\'s being served? Example: \'What\'s for lunch at cronkhite?\'',
'Greetings from Dining Bot, what can I do for you?(Hint, you can ask things like: \'What\'s for breakfast at Annenberg etc..\' or \'What time is breakfast at Anennberg?\')',
'greetings',
'hey',
'long time no see',
'hello',
"lovely day isn't it"
]
const index = Math.floor((Math.random() * responses.length) + 1)
console.log(responses[index].text)
agent.add(responses[index].text)
}
This gives you the bare bones for routing multiple request through your webhook. Hope this helps.

DialogFlow context is always empty in google assistant

I've made a simple chatbot on dialogflow by referencing the dialogflow documentation which is based on Dialogflow's fulfillment library. The bot works good on integration with Google Assistant.
However when I started using context (getting and setting context) in the node.js code, the bot doesn't work on google assistant.
This line agent.context.get('iotcontext'); is null in google assistant but works good in dialogflow test console.
Following is my code:
'use strict';
var https = require ('https');
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
function welcome(agent) {
agent.add(`Welcome to my agent!`);
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
function myIntentHandler(agent) {
console.log("Function myIntentHandler Run");
const codeContext=agent.context.get("iotcontext");
agent.add("Date spoken by user: "+codeContext.parameters["date.original"]);
}
// Run the proper function handler based on the matched Dialogflow intent name
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
// intentMap.set('your intent name here', yourFunctionHandler);
intentMap.set('IoTIntent', myIntentHandler);
agent.handleRequest(intentMap);
});

Resources