DialogFlow context is always empty in google assistant - node.js

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);
});

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.

How to trigger an sending email to human using Dialogflow ES Fulfillment

I am using the fulfillment and nodemailer in my editor and would like to send an email to my personal email address. When I try to integrate to Dialogflow Messenger Beta version, my chatbot will not understand the response.
My output:
User: "I would like to send an email to the support service."
Bot: "Sorry. I do not understand your question."
Correct output:
User: "I would like to send an email to the support service."
Bot: "What is your name?"
User: "Henry"
Bot: "What is your email?"
User: "henry65#gmail.com"
Bot: "Thanks Henry. We will get back to you as soon as possible."
index.js
// See https://github.com/dialogflow/dialogflow-fulfillment-nodejs
// for Dialogflow fulfillment library docs, samples, and to report issues
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const nodemailer = require("nodemailer");
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'james641#gmail.com',
pass: 'test#1567'
}
});
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 sendEmailHandler(agent){
const { email, name } = agent.parameters;
const question = agent.parameters.question;
const mailOptions = {
from: "James", // sender address
to: email, // list of receivers
subject: "Email from Chatbot", // Subject line
html: "<p>Name:</p> " + name + "<p>Email:</p>" + email + "<p>Questions:</p>" + question
};
transporter.sendMail(mailOptions, function (err, info) {
if(err)
{
console.log(err);
}
});
}
// // Uncomment and edit to make your own intent handler
// // uncomment `intentMap.set('your intent name here', yourFunctionHandler);`
// // below to get this function to be run when a Dialogflow intent is matched
// function yourFunctionHandler(agent) {
// agent.add(`This message is from Dialogflow's Cloud Functions for Firebase editor!`);
// agent.add(new Card({
// title: `Title: this is a card title`,
// imageUrl: 'https://developers.google.com/actions/images/badges/XPM_BADGING_GoogleAssistant_VER.png',
// text: `This is the body text of a card. You can even use line\n breaks and emoji! šŸ’`,
// buttonText: 'This is a button',
// buttonUrl: 'https://assistant.google.com/'
// })
// );
// agent.add(new Suggestion(`Quick Reply`));
// agent.add(new Suggestion(`Suggestion`));
// agent.setContext({ name: 'weather', lifespan: 2, parameters: { city: 'Rome' }});
// }
// // Uncomment and edit to make your own Google Assistant intent handler
// // uncomment `intentMap.set('your intent name here', googleAssistantHandler);`
// // below to get this function to be run when a Dialogflow intent is matched
// function googleAssistantHandler(agent) {
// let conv = agent.conv(); // Get Actions on Google library conv instance
// conv.ask('Hello from the Actions on Google client library!') // Use Actions on Google library
// agent.add(conv); // Add Actions on Google library responses to your agent's response
// }
// // See https://github.com/dialogflow/fulfillment-actions-library-nodejs
// // for a complete Dialogflow fulfillment library Actions on Google client library v2 integration sample
// 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('sendEmail_intent', sendEmailHandler);
// intentMap.set('your intent name here', yourFunctionHandler);
// intentMap.set('your intent name here', googleAssistantHandler);
agent.handleRequest(intentMap);
});
I'm still new to Dialogflow so I'm not that savvy with the webhooks and fulfillments. Please help me.
One approach to achieving your use case is to activate the Default fallback intent and enable the Fulfilment of that intent. However, you must ensure that you collect the email address at the fallback intent.
On Dialogflow webhook is triggered by the default fallback, which provides the question
and sessionID, which is then saved in a database.
Then fallback method requests an email address.
Lastly, the user provides an email and intent fires another webhook, which updates the
original record with the email address and sends the request to the inbox.
To avoid this hassle, you can integrate Dialoglfow with any third-party platform, such as Kommunicate, and send email notifications directly from the dashboard. When the default fallback intent gets triggered or when a human agent is assigned to a conversation, an email will be automatically sent.
PS: I work for Kommunicate.

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.

Google Action Webhook Implementation

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).

Resources