I am developing a smart home skill for Alexa. So all requests from Alexa are sent to my AWS Lambda function which then forwards the requests to our servers which contacts the individual smart home devices. So according to the Alexa documentation (https://developer.amazon.com/en-US/docs/alexa/device-apis/alexa-response.html#response) I can answer these requests Synchronously, meaning I wait all the way till the operation on the device has completed (while the http connection between the lambda and our servers remain open -> causing charges as lambda is running longer) and send the response via the lambda back to Alexa.
The other option is to answer Asynchronously by sending the answer as a new http request to the Alexa event gateway.
As some operations take some time (considering the way from our servers to the smart home devices, performing the operation, answering etc.) I'd prefer the async method, as it also saves the time on the lambda. I already implemented all the necessary components to answer async but I don't know what I should answer the lambda in case I'll answer async.
My Lambda currently looks somewhat like this:
const https = require('https');
exports.handler = function (request, context) {
function handleServerRequest(request, context) {
const doPostRequest = () => {
const data = request;
return new Promise((resolve, reject) => {
const options = {
host: 'xxx.ngrok.io',
path: '/dyn/alexa/request',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
/* ... perform https request and resolve promise*/
});
};
doPostRequest().then((response) => {
log("DEBUG", "Server Response: ", JSON.stringify(response));
// in case the server decides to answer async (via event gateway) it
// immediately answers the https request with a flag "async: true".
if(response.async) {
// -> WHAT TO TELL THE LAMBDA HERE?
//context.succeed();
return;
}
context.succeed(response);
});
}
handleServerRequest(request, context, "");
When I just do a context.succeed() without a proper response I'll get an error in the Alexa app, telling me "the device is not reacting", followed by indicating the correct status as quickly after that Alexa receives a valid StateReport directive via the event gateway.
How do I properly end the lambda function in case I'll answer asynchronously?