Reputation: 1
I have been exploring Dialogflow from last 6-7 days and have created a bot which has menu in the form of List.
After going through lot of articles got to know that we need to have event actions_intent_OPTION
in one of the intents for List to work properly. Also got to know that on top of it we need to have handler/intent for actions_intent_OPTION
. This intent would be triggered once user taps one of the option of List.
Now i am struggling in defining handler for event actions_intent_OPTION
. I had defined intent with name "actions_intent_OPTION-handler" but I am not able to find the code which i can code in for fulfillment section of Dialogflow, which will identify the option selected by user and will call the intent associated to that option.
I am a not from coding background, and I tried one code (index.js), but when deployed doesn't given any error however when executed on simulator it throws error "Failed to parse Dialogflow response into AppResponse because of empty speech response."
Reiterating my requirement, I am looking for a sample code which can capture the option selected by user (from list) and trigger the already defined intent.
Details about bot,list and intents is attached herewith.
Note - Intent which needs to be called is "1.1 - ElectricityDetails - DueDate"
Here is the code -> Please don't ask me why i have used certain peice of code, as iam newbie :).
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {dialogflow} = require('actions-on-google');
const app = dialogflow({debug: true});
//const agent = new WebhookClient({ request, response });
let intentMap = new Map();
app.intent('actions_intent_OPTION-handler', (conv, params, option) => {
if (!option) {
conv.ask('You did not select any item from the list or carousel');
} else if (option === 'Payment Due Date Electricity') {
//conv.ask('You are great');
//intentMap.set('Default Welcome Intent', welcome);
intentMap.set('1.1 - ElectricityDetails - DueDate',option);
} else {
conv.ask('You selected ' + option);
}
});
//agent.handleRequest(intentMap);
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);
Upvotes: 0
Views: 1224
Reputation: 50701
You have a few issues here, so it is difficult to tell exactly which one is the real problem, but they all boil down to this statement in your question:
Please don't ask me why i have used certain peice of code
We understand that you're new - we've all been there! But copying code without understanding what it is supposed to do is a risky path.
That said, there are a few things about your design and code that jump out at me as issues:
Mixing libraries
You seem to be loading both the actions-on-google library and the dialogflow-fulfillment library. While most of what you're doing is with the actions-on-google library, the intentMap
is what is used by the d-f library.
You can't mix the two. Pick one and understand how to register handlers and how those handlers are chosen.
Register handlers with actions-on-google
If you're using the a-o-g library, you'll typically create the app
object with something like
const app = dialogflow();
and then register each handler with something like
app.intent( 'intent name', conv => {
// handler code here
});
You'll register the app to handle the request and response with something like
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);
Register handler with dialogflow-fulfillment
The dialogflow-fulfillment approach is similar, but it suggests creating a Map
that maps from Intent Name to handler function. Something like this:
let intentMap = new Map();
intentMap.set( 'intent name', handlerFunction );
Where handlerFunction
is also the name of a function you want to use as the handler. It might look something like
function handlerFunction( agent ){
// Handler stuff here
}
You can then create an agent, set the request and response objects it should use, and tell it to use the map to figure out which Intent Handler to call with something like
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
agent.handleRequest( intentMap );
Intents represent what the user does, not what you do with it
Remember that Intents represent a user's action.
What you do based on that action depends on a lot of things. In your case, once they have selected an option, you want to reply the same way as if they had triggered it with a particular Intent. Right?
You don't do that by trying to trigger that Intent.
What you do is you have both Handlers call a function that does what you want. There is nothing fancy about this - both are just calling the same function, just like lots of other code that can call common functions.
Don't try to dynamically register handlers
Related to the previous issue, trying to register a new Handler inside an existing Handler won't do what you want. By that time, it is too late, and the handlers are already called.
There may be situations where this makes sense - but they are very few, far between, and a very very advanced concept. In general, register all your handlers in a central place as I outlined above.
Upvotes: 1