Comments (7)
Thanks @jerolimov - this will work just fine
If arrow functions aren't your thing, another popular option is to keep a pointer to the real 'this' context at the top of the function. I've made it a practice in cases where I have callback functions in case I forget to use an arrow function.
function myHandler(input, callback) {
const self = this; // <--- pointer to this in the outer function scope
request('http://rest_url...', function(error, response, body) {
if (response) { // we have a success so call the call back
self.emit(":tell", "hello"); // <--- self points to the object you want
} else {
self.emit(':ask', 'hello');
}
}
}
closing this as resolved. Feel free to re-open if you have further questions
from alexa-skills-kit-sdk-for-nodejs.
The keyword this
is bounded to the last function
scope, which is in your case the request callback block.
Instead of writing function (error, response, body) { .. }
you should write (error, response, body) => { ... }
. This does not re-bind the this
-scope, so that this
is not changed in the callback.
Hope this helps you @svonkleeck and @JasonVonKrueger.
Complete fix for the original issue on top:
var request = require('request);
function myHandler(input, callback) {
request('http://rest_url...', (error, response, body) => {
if (response) { // we have a success so call the call back
callback(null, 'found it!');
} else {
callback(error);
}
}
}
from alexa-skills-kit-sdk-for-nodejs.
Have you tried creating a variable outside of the function?
Ex. var self = this;
self.tell(':tell','found it!');
from alexa-skills-kit-sdk-for-nodejs.
I did not explicitly create a new variable, but I did verify that the correct this was bound at the execution. Since the fat arrow is used, this should be bound correctly. Over the weekend, I may see if an explicit Promise resolves the issue. I was able to work around it by using the request_sync library.
from alexa-skills-kit-sdk-for-nodejs.
I have the same issue. Were you able to resolve it?
from alexa-skills-kit-sdk-for-nodejs.
I have been trying to call the ticketmaster API in an alexa skill I am building using the SDK and am having a lot of trouble accessing the JSON response. I tried to use the format that you have outlined, but I clearly do not know how to access the right part of the response. In my function, I am doing self.emit and emitting the list of Object keys for "response", which does not seem to be the JSON response. Body, I have discovered, is of type string, also not the JSON response. I would appreciate any advice you could give me. @bclement-amazon @jerolimov
'showsNearMe': function (input,callback) {
var filledSlots = delegateSlotCollection.call(this);
//compose speechOutput that simply reads all the collected slot values
var speechOutput = randomPhrase(showIntro);
var showCity=this.event.request.intent.slots.showCity.value;
var timePeriod=this.event.request.intent.slots.timePeriod.value + 'T10:00:00Z';
const self = this; // <--- pointer to this in the outer function scope
request("https://app.ticketmaster.com/discovery/v2/events.json?city=Boston&size=1&segmentName=Music&apikey=bP5gQH4GDftg8tE8j3dyXHYYPdNzDBV7", function(error, response, body) {
if (response) {
self.emit(":tell",Object.keys(response).join(' '));
} else {
self.emit(':ask', 'hello');
}
})
},
from alexa-skills-kit-sdk-for-nodejs.
Hello together,
I have also the problem the when asking Alexa for something, there is always this NoSpeechlet response when invoking the searchIntent. I have been struggling for two days without a solution. Here is the code:
'use strict';
var Alexa = require('alexa-sdk');
var http = require('http');
var axios = require('axios');
var calendar = require('./helpers/googleCalendar.js');
var self = this;
// Register handlers and initialize start
exports.handler = function(event, context, callback) {
var alexa = Alexa.handler(event, context);
alexa.appId = APP_ID;
alexa.registerHandlers(newSessionHandlers, startSearchHandlers);
console.log('before exec');
alexa.execute();
console.log('after exec');
};
// Reference to Alexa object
// States
const states = {
SEARCHMODE: '_SEARCHMODE'
};
// Messages and other reoccuring data
const APP_ID = 'amzn1.ask.skill.e0cd726d-f53c-4769-967c-8cacd8862a80';
const helpMessage =
'For example, you can ask: What are my appointments for today?';
const HELP_REPROMPT = 'What can I help you with?';
const STOP_MESSAGE = 'Goodbye!';
const SKILL_NAME = 'Miracle';
const welcomeMessage = 'You can ask for your todays appointments';
const shutdownMessage = 'Ok. See you soon.';
const errorMessage = 'Ups... Something went wrong. Please start me again.';
// Adding session handlers
const newSessionHandlers = {
NewSession: function() {
this.handler.state = states.SEARCHMODE;
console.log('I am in the launch');
this.response
.speak(`Welcome to ${SKILL_NAME}` + ' ' + ' ' + welcomeMessage)
.listen(welcomeMessage);
this.emit(':responseReady');
},
Unhandled: function() {
this.response.speak(helpMessage).listen(helpMessage);
this.emit(':responseReady');
}
};
// Creating hanlders for state SEARCH state
const startSearchHandlers = Alexa.CreateStateHandler(states.SEARCHMODE, {
// Built-in intents
'AMAZON.YesIntent': function() {
output = welcomeMessage;
this.response.speak(output).listen(welcomeMessage);
this.emit(':responseReady');
},
'AMAZON.NoIntent': function() {
this.response.speak(shutdownMessage);
this.emit(':responseReady');
},
// 'AMAZON.RepeatIntent': function() {
// this.response.speak(output).listen(HelpMessage);
// },
SessionEndedRequest: function() {
this.emit('AMAZON.StopIntent');
},
searchIntent: function() {
const url = 'http://127.0.0.1:5000/customers';
axios.get(url).then(
response => {
console.log('Success!', response); // Yea, REST all the things
self.emit(':tell', 'ok');
},
error => {
console.error('Failure!', response); // Boo, bad feels
self.emit(
':tell',
"Hmm.. that didn't work. Check the CloudWatch Luke."
);
}
);
}
});
I really appreciate your help.
Best regards
Oli
from alexa-skills-kit-sdk-for-nodejs.
Related Issues (20)
- AskSdk.DynamoDbPersistenceAdapter Error: Could not read item HOT 2
- error when speak detects an accented character
- WebSocket error from ask-sdk-local-debug with EU region HOT 3
- ExpressAdapter.verifySignature causes skills to fail validation HOT 11
- Debugging doesn't work HOT 12
- Docs don't explain how to get Client ID
- Amazon content HOT 1
- Cannot send a proper response to Alexa after MQTT Publish
- Email HOT 1
- addAudioPlayerPlayDirective not playing some URLs HOT 1
- Keep Compiled code in a dist folder
- `AlexaHouseholdListEvent.ItemsCreated` only ever provides a single ItemId HOT 2
- AskSdk.DynamoDbPersistenceAdapter error after upgrading to aws-sdk v2.1269.0
- The directive Navigation.SetDestination it's not accepted in the sdk
- Inaccurate type definitions in `ask-sdk-model`
- Aws sdk V2 in maintenance mode, upgrade for v3 HOT 3
- `withAskForPermissionsConsentCard` generating errors
- Missing support for SMAPI Metrics API v2 HOT 1
- Dynamic media URL and intercepting AudioPlayer fetch requests for HLS long streaming
- Can we change user's reminder after it has been set
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from alexa-skills-kit-sdk-for-nodejs.