dialogflow quick reply for Facebook client - node.js

When I'm trying to send a response from Node app to Dialogflow using webhook for Facebook messenger client.
Trying to send a quick reply to the Facebook client, however, it is not working and getting the bellow error.
Error: Reply string required by Suggestion constructor
Any help will be appreciated.
const {Suggestion} = require('dialogflow-fulfillment');
agent.add(new Suggestion().setReply('test reply from NodeApp'));

First, you need to update the version of dialogflow-fulfillment package in the package.json file in the inline editor to ^0.6.1 which is the latest one.
Then, I think you can just send quick replies using the statement:
agent.add(new Suggestion(`sample reply`));
Please remember there should have a text response before the replies for Facebook to accept the response object.
Below is a snippet that may help you better.
const {Suggestion} = require('dialogflow-fulfillment');
agent.add(`This is quick reply.`);
agent.add(new Suggestion(`option 1`));
agent.add(new Suggestion(`option 2`));
The above way will work if you are using the Dialogflow Inline Editor as the fulfillment.
If not (i.e, opt to have your own deployment/development environment), you have to send the quick replies as custom payloads within the fulfillment code. (Here also, you have to upgrade the dialogflow-fulfillment package first)
Here is a sample code snippet:
const {Payload} = require("dialogflow-fulfillment")
var payload = {
"facebook": {
"text": "Welcome to my agent!",
"quick_replies": [
{
"content_type": "text",
"payload": "reply1",
"title": "reply 1"
}
]
}
}
agent.add(new Payload(agent.UNSPECIFIED, payload, {rawPayload: true, sendAsMessage: true}))
Hope these will work for you.

Related

How to add Suggestion Chips through Fulfillment for the Dialogflow Messenger integration?

So I have the Dialogflow Messenger embedded in a website and want to add some Suggestion chips. It's easy through the Custom Payload Response type and they show up just fine.
But how do I add them through fulfillment?
I currently have a custom webhook setup and the idea is to have something like this:
if (x) {
agent.add('blablabla');
agent.add(new Suggestion('One');
} else {
agent.add('blablabla');
agent.add(new Suggestion('Two');
}
new Suggestion doesn't work though, so is there another way of doing this?
I was thinking about something like this:
agent.add(new Payload(
"richContent": [
[
{
"options": [
{
"text": "One"
},
{
"text": "Two"
}
],
"type": "chips"
}
]
]));
Essentially trying to insert the Custom Payload directly into the response JSON, if that makes any sense. But yeah no idea how to actually do it. Anyone know how?
It is unclear to me what you exactly mean by new Suggestion() doesn't work. You mean the suggestion chips do not show in Dialogflow Messenger? Do they show in Dialogflow itself?
Let me share a few points:
As far as I know the structure agent.add(new Suggestion(“One”)); should work. I tried a simple example and it is working fine in Dialogflow UI, with the code:
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));
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
function welcome(agent){
agent.add("What is your favorite animal?");
agent.add(new Suggestion("Dog"));
agent.add(new Suggestion("Cat"));
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
agent.handleRequest(intentMap);
});
If suggestions chips are not rendered even in Dialogflow UI I would suggest trying the previous code to discard any potential issues with your Dialogflow setup. You may need to upgrade some dependencies e.g. "dialogflow-fulfillment": "^0.6.1".
Some integrations, like Google Assistant use the Suggestions library from actions-on-google. See for example official Google Assistant code example. You may try to follow a similar behavior if it fits your use case although I do not think it is the case. As a reference you can check this github issue.

How can i send Quick Replies to Facebook Through Dialogflow Fulfillment

I am trying to send Quick Replies to Facebook users through Dialogflow Fulfillment and I have not been able to achieve that I have tried a lot and have not succeeded Is there any help
Codes i have tried:
#1 :
function QuickReplies(agent) {
agent.add(“Select one”);
agent.add(new Suggestion(“Quick Reply”));
agent.add(new Suggestion(“Suggestion”));
}
#2 :
function QuickReplies(agent)
{
const quickReplies1 = new Suggestion({
title: "What do you want to do?",
reply: "Next",
platform: 'FACEBOOK'
})
quickReplies1.addReply_("Cancel");
agent.add(quickReplies1);
}
So, the first step is to import the Suggestion module as:
const {Suggestion} = require('dialogflow-fulfillment')
Then you can do something like your first function (I do not see any problem in your code).
Finally, you have to associate it with one of your intents:
let intentMap = new Map();
intentMap.set('Your intent's name', QuickReplies);
agent.handleRequest(intentMap);
Keep in mind that even if your intent has other components (text, cards) you can still append quick replies to it.

how to send a basic 'hello world' response from DialogFlow using node SDK / fulfillment lib?

probably the most basic question, but the docs (?) suck.
I want to send a basic text response from an agent.
something like:
router.post('/api/webhook', async (request, response) => {
const agent = new WebhookClient({ request, response })
agent.add('hello world')
// now how do i tell dialogflow to handle the response? none of these work:
response.send(agent)
agent.resolve()
this pretty dumb code seemed to work but now has problems
let intentMap = new Map()
intentMap.set('reply', () => {
agent.add('hello world')
})
agent.intent = 'reply'
agent.handleRequest(intentMap)
I don't want to use the intentMap.set or ideally agent.handleRequest(intentMap)
And I certainly don't want to use google cloud funcs, plain express is fine.
can't really find any docs that aren't shoving google cloud functions down your throat.
abstract docs, no example code
https://dialogflow.com/docs/reference/fulfillment-library/webhook-client#new_webhookclientoptions
client libs - no use
https://github.com/googleapis/nodejs-dialogflow
answer:
https://blog.dialogflow.com/post/fulfillment-library-beta/
function intentHandler(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/assistant.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.handleRequest(intentHandler);
I found one way of doing it which is not too verbose, posting in case anyone finds this
Note it's important to also remove replies for Default Fallback from your GUI code in DF
function intentHandler(agent) {
agent.add('Hello World');
}
agent.handleRequest(intentHandler);

Invoke a Dialogflow event with a specific device source

After trying and trying countless times, I ask for your help to call a Dialogflow event (GoogleHome) with a specific GoogleHome device.
Through nodeJS I managed to successfully call a Dialogflow event and I get the fullfillment response. All perfect, only I have to let my GoogleHome device speak with fullfillment, I do not need a text-only answer.
My goal is to let my GoogleHome device speak first, without the word "Ok, Google" and wait for a response from the user.
I did not find anything on the web, my attempts stop to invoke the Dialogflow event and have a console response.
This is the code i have tried for fullfillment
test: async function () {
console.log("[funcGHTalk|test] CALLED");
const projectId = "[[projectid]]";
const LANGUAGE_CODE = 'it-IT';
let eventName = "[[eventname]]";
const sessionId = uuid.v4();
const sessionClient = new dialogflow.SessionsClient();
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
// The text query request.
const request = {
session: sessionPath,
queryInput: {
event: {
name: eventName,
languageCode: LANGUAGE_CODE
},
},
};
// Send request and log result
const responses = await sessionClient.detectIntent(request);
console.log('Detected intent');
const result = responses[0].queryResult;
console.log(result);
console.log(` Query: ${result.queryText}`);
console.log(` Response: ${result.fulfillmentText}`);
if (result.intent) {
console.log(` Intent: ${result.intent.displayName}`);
} else {
console.log(` No intent matched.`);
}
}
The code you have written is using the Dialogflow Detect Intent API. This is meant to run on consoles and servers to send a message to Dialogflow, which will parse it, determine which Intent it matches, call fulfillment with that information, and return all the results.
You don't need to run this on a Google Home, since the Google Assistant does all this for you.
What I think you're looking for is to develop fulfillment with Actions on Google and the Dialogflow Fulfillment API. This handles things on the other end - after Dialogflow determines what Intent matches what the user has said, and if that Intent has fulfillment enabled, it will send the information to your webhook which is running on a cloud server somewhere. You would then process it, send a reply (either using the actions-on-google library or the dialogflow-fulfillment library is easiest), and it would send it back to the Assistant.
You indicated that you want the Action to "let my GoogleHome device speak first, without the word "Ok, Google" and wait for a response from the user". This is much more complicated, and not really possible to do with the Google Home device right now. Most Actions have the user initiating the conversation with "Ok Google, talk to my test app" or whatever the name of the Action is.
You don't indicate how you expect to trigger the Home to begin talking, but you may wish to look into notifications to see if those fit your model, however notifications don't work with the Home right now, just the Assistant on mobile devices.

Google Docs embed answering form

I want to embed a form in an email that works both in a web browser and the gmail app, using google docs.
I have been able to embed it and use it in different browsers with the following code, but I can't use it in the app.
The problem is that I want the user to respond by ticking either Yes or No, and clicking an Accept button.
Could you please help me?
Thanks in advance
form.setRequireLogin(false);
var url = form.getPublishedUrl();
var response = UrlFetchApp.fetch(url);
var htmlBody = HtmlService.createHtmlOutput(response).getContent();
MailApp.sendEmail(responsableDirecto,
"Course request",
"",
{
name: 'Course request confirmation'+""+program+""+name,
htmlBody: htmlBody,
attachments: [documetoDatos],
});

Resources