how to call intent in index js - node.js

How to call below intent in js, how to call "Default Welcome Intent" in the index.js file or in any other js files
const functions = require('firebase-functions');
const { dialogflow } = require('actions-on-google');
const app = dialogflow();
app.intent('Default Welcome Intent', conv => {
conv.ask('How are you?');
});
exports.factsAboutGoogle = functions.https.onRequest(app);

Yes, you need to make sure "Enable webhook call for this intent" is turned on for each Intent that you want to process through your webhook fulfillment.

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

How to manage a customize for returning users in dialogflow fullfilment?

Following the design tips on action-on-google developer platform, I'm trying to give a custom welcome response for users who come back in a Google Assistant Voice app.
https://developers.google.com/actions/design/tips
To proceed, i started by switch My Default Welcome intent in Dialogflow in the Fullfilment mode. Then deploy my code on firebase. It's work well when i ask for a name permission, but when I try to input the 'AppRequest.User lastSeen', my app shut down?
Maybe you know what i missed in my code?
'use strict';
const {
dialogflow,
Permission,
} = require('actions-on-google');
const functions = require('firebase-functions');
const app = dialogflow({debug: true});
app.intent('Default Welcome Intent', (conv) => {
const welcome = (conv) => {
if (conv.user.last.seen) {
conv.ask(`Hey you're back...`);
} else {
conv.ask('Welcome to World Cities Trivia!...');
}
}});
// Set the DialogflowApp object to handle the HTTPS POST request.
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);
It looks like the code in your welcome Intent Handler doesn't actually do anything.
You setup the handler
app.intent('Default Welcome Intent', (conv) => {
...
});
but inside your handler you create another function
const welcome = (conv) => {
if (conv.user.last.seen) {
conv.ask(`Hey you're back...`);
} else {
conv.ask('Welcome to World Cities Trivia!...');
}
}
that never gets called. It isn't clear why you have this inner function, since it isn't necessary. You just need the body of that function in the outer one. So it can look something like this, perhaps:
app.intent('Default Welcome Intent', (conv) => {
if (conv.user.last.seen) {
conv.ask(`Hey you're back...`);
} else {
conv.ask('Welcome to World Cities Trivia!...');
}
});
You should read it from the conv object, it represents a turn in the conversation and exposes the attributes of the individual requests: conv.user.last.

Resources