-1

Is there any way we can send a message to a device from Lambda function which is invoked by Alexa Skill. The message contains some values collected by Lambda function. So basically I want to do this:

  • Device ---> Voice command ---> Alexa Skill --(Trigger)--> Lambda function
  • Lambda function(collect values) ---- message ---> Device

Is there any example in Java?

Thanks for any pointer/help.

-James

sam2215
  • 31
  • 7
  • What do you mean by "send a message to a device"? An Alexa function returns the text to be spoken. What else did you want to send to the device? – John Rotenstein May 28 '18 at 21:34
  • Thanks for your reply, I see that Alexa function returns a 'Response' but how do I get that Response in our code? Is there any example to handle that Response? Thanks again. – sam2215 May 28 '18 at 21:55
  • Have you examined the [Alexa Skills Kit: Build](https://developer.amazon.com/alexa-skills-kit/build)? It links to [SDKs and Code Samples in GitHub](https://github.com/alexa). The best toolkit at the moment uses node.js. It will help build the response object that you send back to the Alexa service, which will then communicate back to the Alexa device that issued the command. – John Rotenstein May 28 '18 at 22:05

2 Answers2

0

Invoke Alexa device from lambda function is a very similar question, with the answer: "it's not possible YET"

I will elaborate. You can send notifications to all users of a skill such as a new feature, however, you cannot send a notification to a specific user that invokes a function.

To send notifications to all users of an Alexa skill who have notifications enabled, see this design.

Reference this thread for more information on the limitations of sending a notification to a specific user.

KiteCoder
  • 2,364
  • 1
  • 13
  • 29
0

What you are asking can be done.

First the voice command does not come from a human from your diagram. A device talks to Alexa. Alexa invokes or triggers Lambda. Lambda function messages device.

The function inside Lambda is http or https. If your device can handle https or TLS encryption then good. But most of the device are small and have limited computing power, so you will end up using http. As of now 2020, AWS allows http, but a year from now it requires you to use https or TLS 1.3 due to federal regulations. But we don't know until it happens.

Below is a sample of Lambda http post in NodeJS. The trigger data comes in request. So you should know what JSON will come in and extract your data from JSON using the if statement. NodeJS website has good examples for http.

Now your device is the server. It has to anticipate the Lambda request and process it and reply to Lambda if needed.

Now your device talks and receives information.

    const http = require('http');
exports.handler = async (request, context) => {

 if (request.directive.header.namespace === 'FromAlexaSkill') {
httpost("This is your data to device", "192.168.1.2");
}

//**********************************************
let httpPost =async (postData, servername) => {
  
    let dataString = '';
    
    const response = await new Promise((resolve, reject) => {

        const options = {
          hostname: servername,
          port: 1777,
          path: '/dim',
          method: 'POST',
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': Buffer.byteLength(postData)
          }
        };
        
        const req = http.request(options, (res) => 
          {
              res.setEncoding('utf8');
              res.on('data', chunk => {
                  dataString += chunk;
              });
              
              res.on('end', () => {
                  resolve({
                       "body":dataString
                  });
              });
            
          });//http.request
        
       
        req.on('error', (e) => {
          console.error("problem with request: "+e.message);
          reject({
              statusCode: 500,
              body: 'Something went wrong!'
          });
        });
        
        
        // Write data to request body
        req.write(postData);
        req.end();
    
    }); //Promise
    
    return response;

};//httpPost
}
Gibo
  • 21
  • 3