I have this code and i have this error :
TypeError: Cannot read property 'ask' of null
/**
'use strict';
const express = require("express");
const bodyParser = require("body-parser");
const { WebhookClient } = require('dialogflow-fulfillment');
const { Card, Suggestion } = require('dialogflow-fulfillment');
const { Carousel } = require('actions-on-google');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
const restService = express();
const request = require('request-promise-native');
const app = actionssdk();
restService.use(
bodyParser.urlencoded({
extended: true
})
);
restService.use(bodyParser.json());
restService.post("/echo", function(req, res) {
const imageUrl = 'https://developers.google.com/actions/images/badges/XPM_BADGING_GoogleAssistant_VER.png';
const imageUrl2 = 'https://lh3.googleusercontent.com/Nu3a6F80WfixUqf_ec_vgXy_c0-0r4VLJRXjVFF_X_CIilEu8B9fT35qyTEj_PEsKw';
const linkUrl = 'https://assistant.google.com/';
const agent = new WebhookClient({ request: req, response: res });
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);
agent.handleRequest(intentMap);
});
function welcome(agent) {
let conv = agent.conv();
// Use Actions on Google library to add responses
conv.ask('Please choose an item:')
conv.ask(new Carousel({
title: 'Google Assistant',
items: {
'WorksWithGoogleAssistantItemKey': {
title: 'Works With the Google Assistant',
description: 'If you see this logo, you know it will work with the Google Assistant.',
image: {
url: imageUrl,
accessibilityText: 'Works With the Google Assistant logo',
},
},
'GoogleHomeItemKey': {
title: 'Google Home',
description: 'Google Home is a powerful speaker and voice Assistant.',
image: {
url: imageUrl2,
accessibilityText: 'Google Home',
},
},
},
}))
// Add Actions on Google library responses to your agent's response
agent.add(conv);
}
function fallback(agent) {
let conv = agent.conv();
// Use Actions on Google library to add responses
conv.ask('Please choose an item:')
// Add Actions on Google library responses to your agent's response
agent.add(conv);
}
restService.listen(process.env.PORT || 5001, function() {
console.log("Server up and listening :) !");
});
Check in your package.json that you have last version "dialogflow-fulfillment": "^0.3.0-beta.3".
Try with npm i --save dialogflow-fulfillment@latests.
i was also having similar issue. Dont test with dialog flow inbuilt try it now. Test it with google assistant .It will work.
Sure, agent.conv() only works for Actions on Google (Google Assistant).
Take a look the code:
https://github.com/dialogflow/dialogflow-fulfillment-nodejs/blob/master/src/dialogflow-fulfillment.js#L391-L397
Duplicate of #28
Like jorgecasar pointed out, you need to set the agent.requestSource, otherwise, agent.conv() return null
const agent = new WebhookClient({ request, response });
agent.requestSource = agent.ACTIONS_ON_GOOGLE;
then agent.conv() would not return null
What about dialogflow-webhook? Is there any way to end conversation by webhook without "set this intent as end of conversation"? I mean: I would like to find a way to close the conversation programmatically, i.e.
agent.endConversation_ = true;
when the user says specific utterances (for example, "stop", "end" etc).
Yes with agent.end() instead of adding.add()
ok, so if i got it right, that would end the current session, but it won't emit a sound like in actions-on-google, right?
i was also having similar issue. Dont test with dialog flow inbuilt try it now. Test it with google assistant .It will work.
Thanks, it worked for me!
Most helpful comment
i was also having similar issue. Dont test with dialog flow inbuilt try it now. Test it with google assistant .It will work.