Issue with Null SpeechletResponse (Alexa) - node.js

Something is messed up with my AMAZON.StopIntent. No matter what I put there (I've tried everything from every tutorial), whenever it's called, I get "There was a problem with the requested skill's response" and the Alexa app shows the error as "speechletresponse cannot be null". My project is in the JSON, not Java format.
If anyone can help, I'd very much appreciate it!
Thanks!
As requested here's what is being sent to Lambda
{
"session": {
"sessionId": "SessionId.XXXXX",
"application": {
"applicationId": "amzn1.ask.skill.XXXXXXX"
},
"attributes": {},
"user": {
"userId": "amzn1.ask.account.XXXXXXX"
},
"new": true
},
"request": {
"type": "IntentRequest",
"requestId": "EdwRequestId.XXXXXX",
"locale": "en-US",
"timestamp": "2017-01-18T22:38:53Z",
"intent": {
"name": "AMAZON.StopIntent",
"slots": {}
}
},
"version": "1.0"
}
And here's the relevent code:
var handlers = {
'LaunchRequest': function () {
this.emit('AMAZON.HelpIntent');
},
'GetNewDogThoughtIntent': function () {
this.emit('GetDogThought');
},
'GetNewCatThoughtIntent': function () {
this.emit('GetCatThought');
},
'GetDogThought': function () {
var dogthoughtIndex = Math.floor(Math.random() * DOGTHOUGHTS.length);
var randomDogThought = DOGTHOUGHTS[dogthoughtIndex];
// Create speech output
var speechOutput = "Your dog is thinking, " + randomDogThought;
this.emit(':tellWithCard', speechOutput, "Your dog was thinking... ", randomDogThought);
},
'GetCatThought': function () {
var catthoughtIndex = Math.floor(Math.random() * CATTHOUGHTS.length);
var randomCatThought = CATTHOUGHTS[catthoughtIndex];
// Create speech output
var speechOutput = "Your cat is thinking, " + randomCatThought;
this.emit(':tellWithCard', speechOutput, "Your cat was thinking... ", randomCatThought);
},
'AMAZON.HelpIntent': function () {
var speechOutput = "You can ask me for what your cat or dog is thinking, or you can say exit... Right now I can only provide thoughts for one cat or dog at a time... What can I help you with?";
var reprompt = "What can I help you with? Make sure to say if your pet is a cat or dog when you ask!";
this.emit(':ask', speechOutput, reprompt);
},
'SessionEndedRequest': function (sessionEndedRequest, session) {
},
"AMAZON.StopIntent": function (shouldEndSession) {
}

I finally got it after consulting the SpaceGeek tutorial again and making some tweaks to it. Basically, here's what worked:
'AMAZON.StopIntent': function () {
this.emit(':tell', "Goodbye!");
}
The key was the ':tell', which I didn't have before. Thanks to everyone who answered and helped!

Can you post your code for the StopIntent? You should be calling a speechlet response in it. For example:
'AMAZON.StopIntent': function (shouldEndSession, response) {
var speechOutput = "Goodbye";
response.tell(speechOutput);
},
Are you building that response properly and passing it?

I have found this link on alexa developer forum. This might help in your issue..
https://forums.developer.amazon.com/questions/49211/system-error-speechletresponse-was-null.html
I am writing this code in php, if that helps
$data = file_get_contents("php://input");
$jsonData = json_decode($data);
if($jsonData->request->type === "IntentRequest"){
$IntentName = $jsonData->request->intent->name;
if($IntentName === "AMAZON.StopIntent"){
$response = '{
"version" : "1.0",
"response" : {
"outputSpeech": {
"type": "PlainText",
"text": ""
},
"shouldEndSession" : false
}
}';
echo $response;
}
}

Related

Node-Express: filter request query and display rest of the data

I am new in Node query. I am using Node express for my backend app. I have one nested json which has three language options and inside language there are auth, dashboard, data, and data1 options. I want to filter the query and display rest of the json data in my browser. for example if I type url like this: http://localhost:5000/?namespaces=auth&languages=en,fi then it will display language en and fi's data and from namespaces I want to display auth's data. For display the data I have created one output empty object and want to add it in my output object. but don't know how to do that.
I have share my code in codesandbox.
This is my json data
{
"en": {
"auth": {
"welcomeMessage3": "Hi John"
},
"dashboard": {
"welcomeMessage": "Hi Doe"
},
"data (1)": {
"welcomeMessage3": "Hi Jonny"
},
"data": {
"welcomeMessage3": "Hi Monty"
}
},
"fi": {
"auth": {
"welcomeMessage3": "Moi name "
},
"dashboard": {
"welcomeMessage": "Moi dashboard"
},
"data (1)": {
"welcomeMessage3": "Moi data 1"
},
"data": {
"welcomeMessage3": "Moi data"
}
},
"sv": {
"auth": {
"welcomeMessage3": "Hej John"
},
"dashboard": {
"welcomeMessage": "Hej dashboard"
},
"data (1)": {
"welcomeMessage3": "Hej data"
},
"data": {
"welcomeMessage3": "Hej data"
}
}
}
This is my express app
const express = require('express')
const app = express()
const port = 5000
const translationData = require('./translations'); // My json
const filterTranslations = (namespaces, languages) => {
let output = {};
const translations = { ...translationData };
console.log("I am typing Languages", languages);
console.log("I am typing namespace", namespaces);
for (const lng in translations) {
console.log("languages are coming from translation json", lng);
if (lng.length !== 0 && lng !== languages) {
delete translations[lng];
console.log("Delete rest of the language", lng);
} else {
for (const ns in translations[lng]) {
if (ns.length !== 0 && ns !== namespaces) {
delete translations[lng][ns];
console.log("delete rest of the Namespace", ns);
}
}
}
}
return output;
};
app.get('/', (req, res) => {
res.send(
filterTranslations(
req.query.namespaces,
req.query.languages,
)
)
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
There are several mistakes :
the function filterTranslations should return a value, here translations,
the conditions in your if statement is not correct : lng !== languages, in your example, languages = 'en,fi' and lng will be en or fi. Look at String.includes( searchString [, position ]) or split your languages and use Array.includes( searchElement[, fromIndex]).
Hoping I helped you, have a good day !
If I understand correctly what you are trying to do I think you overcomplicate things.
First of all passing a comma separated string as a parameter is not the best idea as you would have to turn it into an array in the backend by splitting the string in the comma. It's best if it's an array to begin with. That means that your url would be :
http://localhost:8080/?namespaces=auth&languages[]=en&languages[]=fi
Then the function that handles this would be:
const filterTranslations = (namespaces, languages) => {
let output = [];
const translations = { ...translationData };
//languages are now an array
for (let l in languages) {
let lng = languages[l];
// for each language you send as a parameter you push a new object in your output with the language as a key and the values
output.push({[lng]:translations[lng]});
}
return output;
};
The expected output is the following:
[
{
"en": {
"auth": {
"welcomeMessage3": "Hi John"
},
"dashboard": {
"welcomeMessage": "Hi Doe"
},
"data (1)": {
"welcomeMessage3": "Hi Jonny"
},
"data": {
"welcomeMessage3": "Hi Monty"
}
}
},
{
"fi": {
"auth": {
"welcomeMessage3": "Moi name "
},
"dashboard": {
"welcomeMessage": "Moi dashboard"
},
"data (1)": {
"welcomeMessage3": "Moi data 1"
},
"data": {
"welcomeMessage3": "Moi data"
}
}
}
]

How to properly handle context.sendActivity?

I just want to ask two simple questions and then show the card. Problem is, in the second "sendActivity" keeps on repeating "please give password" just forever. I tried to place another onTurn after and even inside the function, with worst or same results. Dont want to implement a whole waterfall just for 2 questions. Which ActivityHandler fits better what am trying to achieve?
async processLogin(context, next, res) {
await context.sendActivity({
text: 'please give username'
})
const SelectedCard2 = CARDS2[0];
this.onTurn(async (context, next, res) => {
let txt = `"${context.activity.text}"`;
if (txt) {
var name = JSON.parse(txt);
console.log(name)
}
await context.sendActivity({
text: 'please give password'
})
let txt2 = `"${context.activity.text}"`;
if (txt2) {
var password = JSON.parse(txt2);
console.log(password)
res = password;
}
await next();
});
}
enter link description hereIf you just want to collect some info from user by an easy , you can use adaptive card in one step, try the code below :
const { ActivityHandler,CardFactory } = require('botbuilder');
class EchoBot extends ActivityHandler {
constructor() {
super();
// See https://aka.ms/about-bot-activity-message to learn more about the message and other activity types.
var adaptiveCard = {
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"width": 2,
"items": [
{
"type": "TextBlock",
"text": "Pls type your info here . Don't worry, we'll never share or sell your information.",
"isSubtle": true,
"wrap": true,
"size": "Small"
},
{
"type": "TextBlock",
"text": "Username",
"wrap": true
},
{
"type": "Input.Text",
"id": "username",
"placeholder": "your user name here"
},
{
"type": "TextBlock",
"text": "Password",
"wrap": true
},
{
"type": "Input.Text",
"id": "password",
"placeholder": "makre sure no one is around you ..."
}
]
}
]
}
],
"actions": [
{
"type": "Action.Submit",
"title": "Submit"
}
]
};
this.onMessage(async (context, next) => {
if(context.activity.text==="login"){
await context.sendActivity({ attachments: [CardFactory.adaptiveCard(adaptiveCard)] });
}else if(context.activity.value != undefined){
var user = context.activity.value;
await context.sendActivity("hello , your username : " + user.username + ",password :" + user.password);
}else {
await context.sendActivity("send login to do test");
}
await next();
});
this.onMembersAdded(async (context, next) => {
const membersAdded = context.activity.membersAdded;
for (let cnt = 0; cnt < membersAdded.length; ++cnt) {
if (membersAdded[cnt].id !== context.activity.recipient.id) {
await context.sendActivity('Hello and welcome!');
}
}
// By calling next() you ensure that the next BotHandler is run.
await next();
});
}
}
module.exports.EchoBot = EchoBot;
This code is based on official nodejs echo bot , just cover the content of bot.js file to test it :
Hope it helps .

Creating dialogs programmatically from JSON file

I am using the Microsoft Bot Framework with Node.js. I have a config file that looks like the following.
{
"server": {
"port": 3978
},
"dialogs": {
"default": {
"text": "This is some sample text.",
"actions": [
{
"title": "Button 1",
"value": "Action 1"
},
{
"title": "Button 2",
"value": "Action 2"
}
]
},
"hello": {
"text": "hello",
"matches": "^hello$"
},
"asdf": {
"text": "asdf",
"matches": "^asdf$"
},
"goodbye": {
"text": "goodbye",
"matches": "^goodbye$"
}
}
}
I want to use a for loop to read through the dialogs and create them so that they respond with the text value and have the trigger action of the matches value.
For example, the bot responds hello to the input of hello, asdf to the input of asdf, and goodbye to the input of goodbye.
The function I have written in an attempt to solve this looks like this.
var create = function(bot, _config) {
var config = JSON.parse(JSON.stringify(_config));
// Create dialogs from config
var keys = Object.keys(config.dialogs);
for(var i = 0; i < keys.length; i++) {
var dialogName = keys[i];
var dialog = config.dialogs[dialogName];
// Skip over default dialog
if(dialogName == "default") continue;
// Create other dialogs
bot.dialog(dialogName, function(session) {
var text = dialog.text;
session.endDialog(text);
}).triggerAction({
matches: new RegExp(dialog.matches, "i")
});
}
}
When I run this, the bot responds with goodbye to the inputs of hello, asdf, and goodbye. However, the console shows that the correct dialogs are being called when they are supposed to. Even when I call the hello dialog by using session.beginDialog('hello');, the bot returns goodbye.
What seems to be causing the problem here?
It's a common “gotchas” of var in javascript. replace var to let should fix your issue.
The issue similar with
for (var i = 0; i < 10; i++) {
setTimeout(function() { console.log(i); }, 100 * i);
}
The root cause is var is function-scoped and let is block-scoped. You can refer to https://www.typescriptlang.org/docs/handbook/variable-declarations.html for details.

API.ai Actions on Google - Failed to parse JSON response string with 'INVALID_ARGUMENT' error: ": Cannot find field."

This error is similar to what I asked here, but this time it's with NodeJs client.
I am trying to find directions to a location. As soon as the intent is triggered on my webhook, I am calculating the directions using GoogleMapAPI. But before it can finish and send a response, I receive the error on my Actions Console. I checked total response time and it is less than 2 seconds which is less than 5 seconds timeout by Google.Where I am wrong???
My API.ai Intent
Using express.js with Action-on-Google Node Client
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const intentHandler = require('./intent_handler')
const app = express();
app.use(bodyParser.json());
const ApiAiAssistant = require('actions-on-google').ApiAiAssistant;
// Create functions to handle requests here
....
....
const DIRECTION_INTENT = 'action_direction';
function MyAssistant(req, res) {
const assistant = new ApiAiAssistant({request: req, response: res});
assistant.handleRequest(responseHandler(assistant));
}
function responseHandler (assistant) {
// intent contains the name of the intent you defined in the Actions area of API.AI
let intent = assistant.getIntent();
switch (intent) {
case WELCOME_INTENT:
...
break;
case WELCOME_FALLBACK_PERMISSION_INTENT:
...
break;
case DIRECTION_INTENT:
console.log(">>>>>>>DIRECTION_INTENT<<<<<<<");
intentHandler.directionIntent(assistant);
break;
}
}
app.post('/', function (req, res) {
MyAssistant(req, res);
});
app.listen(8080, function () {
console.log('app listening on port 8080!')
});
Handler Code
'use strict';
const speech = require("./speech_template");
const direction = require("./directionModule");
const intent_handler = {
'welcomeIntent': function (assistant) {
.....
},
'welcomeFallbackPermissionIntent': function (assistant) {
.....
},
'directionIntent':function (assistant) {
console.log('direction intent');
direction.getDirectionWithSavedAddress(function (response) {
assistant.ask(response);
});
}
};
module.exports = intent_handler;
Direction Extraction --- ERROR comes on Action Console before this get finished
'use strict';
const striptags = require('striptags');
const speech = require("./speech_template");
let googleMapsClient = require('#google/maps').createClient({
key: global.GOOGLE_DIRECTION_KEY
});
const directionModule = {
'getDirectionWithSavedAddress': function (eventCallback) {
let myAdd = <From Saved Data>;
if (myAdd === undefined) {
console.log("error......");
}
let destination = <From Saved Data>;
this.getDirectionWithAddress(myAdd, destination, function (dir) {
....
if(SUCCESS){
eventCallback(`<speak> ${steps} </speak>`);
}else{
eventCallback(`<speak> ${speech.ERROR_DIRECTIONS} </speak>`);
}
});
},
'getDirectionWithAddress': function (add1, add2, eventCallback) {
let dir = {};
googleMapsClient.directions({
origin: add1,
destination: add2,
mode: "driving",
departure_time: "now"
}, function (err, response) {
if (!err) {
console.log(response.json.routes[0]);
....
....
....
} else {
console.log(`Error --> ${err.toString()}`);
....
}
eventCallback(dir);
});
}
};
module.exports = directionModule;
UPDATE
I am running the code locally via WebStorm and exposing webhook via port forwarding using ngrok.
Update2
BAD REQUEST 400
{
"originalRequest": {
"source": "google",
"version": "2",
"data": {
"isInSandbox": true,
"surface": {
"capabilities": [
{
"name": "actions.capability.AUDIO_OUTPUT"
}
]
},
"inputs": [
{
"rawInputs": [
{
"query": "get me there",
"inputType": "VOICE"
}
],
"arguments": [
{
"rawText": "get me there",
"textValue": "get me there",
"name": "text"
}
],
"intent": "actions.intent.TEXT"
}
],
"user": {
"locale": "en-US",
"userId": "<uID>"
},
"device": {},
"conversation": {
"conversationId": "<cID>",
"type": "ACTIVE",
"conversationToken": "[\"_actions_on_google_\",\"defaultwelcomeintent-followup\"]"
}
}
},
"id": "<ID>",
"timestamp": "2017-09-12T17:08:10.321Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "get me there",
"speech": "",
"action": "action_direction",
"actionIncomplete": false,
"parameters": {},
"contexts": [
{
"name": "_actions_on_google_",
"parameters": {},
"lifespan": 99
},
{
"name": "google_assistant_input_type_voice",
"parameters": {},
"lifespan": 0
},
{
"name": "actions_capability_audio_output",
"parameters": {},
"lifespan": 0
},
{
"name": "defaultwelcomeintent-followup",
"parameters": {},
"lifespan": 4
}
],
"metadata": {
"intentId": "<iID>",
"webhookUsed": "true",
"webhookForSlotFillingUsed": "false",
"nluResponseTime": 15,
"intentName": "DirectionIntent"
},
"fulfillment": {
"speech": "",
"messages": [
{
"type": 0,
"speech": ""
}
]
},
"score": 1
},
"status": {
"code": 200,
"errorType": "success"
},
"sessionId": "<sID>"
}
This looks like before my callback is finished, my webhook is sending empty response to Google Actions.
Why is this happening and How to resolve it?????
The problem lies in how your directionIntent() function calls, and handles the result of, your getDirectionWithSavedAddress() function. It expects getDirectionWithSavedAddress() returns a function, when it does not. Instead, getDirectionWithSavedAddress() expects to send its results to a callback.
So after it makes its call to getDirectionWithAddress(), the function ends, returning nothing. This "nothing" is sent to assistant.ask(), which returns that to Google's server. This is an invalid response, so you're getting the error.
Fixing this should be straightforward. You need to call getDirectionWithSavedAddress() with a callback function. Inside this function you should call assistant.ask() with the value sent to the callback.
So directionIntent() might look something like
'directionIntent':function (assistant) {
console.log('direction intent');
direction.getDirectionWithSavedAddress( function( msg ){
assistant.ask( msg );
} );
}
Updated
This line makes no sense:
assistant.handleRequest(responseHandler(assistant));
The assistant.handleRequest() function is supposed to be passed a Map of Intent names to functions to call to handle the event. You're doing this manually in the responseHandler() function and you're not returning a Map. Since you're not returning a Map, it fails when trying to do the handleRequest() and generates the error "Action Error: Request handler can NOT be empty".
You can fix this by just calling responseHandler(assistant) and not dealing with handleRequest() at all. Or you can create the map that handleRequest() is expecting and get rid of responseHandler() completely.

Facebook Messenger Bot Persistent Menu

I am generating my first bot working with node.js and heroku but finding some difficulties to understand the persistent menu functionalities.
Question 1) How do can I attach event as callbacks?
function persistentMenu(sender){
request({
url: 'https://graph.facebook.com/v2.6/me/thread_settings',
qs: {access_token:token},
method: 'POST',
json:{
setting_type : "call_to_actions",
thread_state : "existing_thread",
call_to_actions:[
{
type:"postback",
title:"FAQ",
payload:"DEVELOPER_DEFINED_PAYLOAD_FOR_HELP"
},
{
type:"postback",
title:"I Prodotti in offerta",
payload:"DEVELOPER_DEFINED_PAYLOAD_FOR_HELP"
},
{
type:"web_url",
title:"View Website",
url:"https://google.com/"
}
]
}
}, function(error, response, body) {
console.log(response)
if (error) {
console.log('Error sending messages: ', error)
} else if (response.body.error) {
console.log('Error: ', response.body.error)
}
})
}
Question 2) The only way I have found for empty the persistent menu and generating a new one is with a delete request via terminal ("as Facebook documented")m is there a possibily to clear inserting a refresh function on my app.js file?
curl -X DELETE -H "Content-Type: application/json" -d '{"setting_type":"call_to_actions","thread_state":"existing_thread"}' "https://graph.facebook.com/v2.6/me/thread_settingsaccess_token=PAGE_ACCESS_TOKEN"
The FB example robot is not well structured for call backs. I haven't found a good way to structure the example in Node callback or promise model. I'm sure a Node expert can reorg it.
As for the persistent menu, if you send an empty call_to_actions array the menu will disappear. The menu seems a bit 'sticky' however as it does not immediately appear/disappear when the message is sent.
I incorporated your snippet into my example robot. You can see it at
https://messenger.com/t/dynamicmemorysolutions
The source is at:
https://github.com/matthewericfisher/fb-robot
See the add/remove menu commands and functions.
EDIT: The persistent menu API has been updated. See this question for more details.
this worked for me:
function menuButton() {
var messageData = {
setting_type : "call_to_actions",
composerinputdisabled :"TRUE",
thread_state : "existing_thread",
call_to_actions:[
{
type:"postback",
title:"¿Tiempo de espera?",
payload:"ACTUALIZAR"
},
{
type:"postback",
title:"Ver Promociones",
payload:"DEVELOPER_DEFINED_PAYLOAD_FOR_START_ORDER"
}
]
}
request({
uri: 'https://graph.facebook.com/v2.6/me/thread_settings',
qs: { access_token: PAGE_ACCESS_TOKEN },
method: 'POST',
json: messageData
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var recipientId = body.recipient_id;
var messageId = body.message_id;
console.log("Successfully sent generic message with id %s to recipient %s",
messageId, recipientId);
} else {
console.error("Unable to send message.");
console.error(response);
console.error(error);
}
});
}
And I call this function at the beggining
app.post('/webhook', function(req, res){
var data = req.body;
if (data.object == 'page') {
menuButton();
data.entry.forEach(function(entry) {
var pageID = entry.id;
var timeOfEvent = entry.time;
// Iterate over each messaging event
entry.messaging.forEach(function(event) {
if (event.message) {
receivedMessage(event);
}else if (event.postback) {
receivedPostback(event);
} else {
console.log("Webhook received unknown event: ", event);
}
});
});
res.sendStatus(200);
}
})
What I have not being able to do is to remove the option of free text input. Facebook claimed now is possible yet have found no instructions or examples on how to do it. Any clues?
If you want to disable the free text input, you shoud add the following parameter to your persistent menu request:
"composer_input_disabled": true
and not
composerinputdisabled :"TRUE"
The FB API document states that the API link to hit for applying persistent menu to the page specific bot is:
https://graph.facebook.com/v2.6/me/messenger_profile?access_token=<PAGE_ACCESS_TOKEN>
Notice the me after version number i.e v2.6 in this specific case. However, this did not worked for a lot of people
There is small change in the API link to hit:
graph.facebook.com/v2.6/Page ID/messenger_profile?access_token=PAGE ACCESS TOKEN
Notice that me is replaced with the fb Page Id.
And the sample payload can still be the same:
{
"get_started": {
"payload": "Get started"
},
"persistent_menu": [
{
"locale": "default",
"composer_input_disabled": false,
"call_to_actions": [
{
"title": "Subscribe",
"type": "postback",
"payload": "subscribe"
},
{
"title": "Stop notifications",
"type": "nested",
"call_to_actions": [
{
"title": "For 1 week",
"type": "postback",
"payload": "For_1_week"
},
{
"title": "For 1 month",
"type": "postback",
"payload": "For_1_month"
},
{
"title": "For 1 year",
"type": "postback",
"payload": "For_1_year"
}
]
},
{
"title": "More",
"type": "nested",
"call_to_actions": [
{
"title": "Fresh jobs",
"type": "postback",
"payload": "fresh jobs"
},
{
"title": "Like us",
"type": "web_url",
"url": "https://www.facebook.com/onlysoftwarejobs/"
},
{
"title": "Feedback",
"type": "web_url",
"url": "https://docs.google.com/forms/d/e/1FAIpQLScjgFRbfBLznO55kFIskcH_eFc23zRSUUxzIgv_o44uj0GMpw/viewform"
}
]
}
]
}
]
}
Notice that it is mandatory to configure get_started button before setting up the persistent_menu.

Resources