alexa / alexa-skills-kit-sdk-for-nodejs

The Alexa Skills Kit SDK for Node.js helps you get a skill up and running quickly, letting you focus on skill logic instead of boilerplate code.
Apache License 2.0
3.12k stars 735 forks source link

Cannot send a proper response to Alexa after MQTT Publish #721

Closed MarcoEsposito890 closed 1 year ago

MarcoEsposito890 commented 1 year ago

Hello, I'm having issues communicating back to Alexa after I publish an MQTT message on AWS IoT using the following handler.

const MQTTHandler = {

  canHandle(handlerInput) {
    return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
      && Alexa.getIntentName(handlerInput.requestEnvelope) === 'MQTTIntent';
  },
  handle(handlerInput) {
    var params = {
      topic: topic,
      payload: "{test message}",
      qos:0
  };
  iotData.publish(params, (error, data)=>{
    if (!error){ 
      var mqttText = 'Test lambda OK';
      return handlerInput.responseBuilder
      .speak(mqttText)
      .getResponse();
    }else{
      var mqttText = 'Test lambda NOT OK';
    };

    return handlerInput.responseBuilder
      .speak(mqttText)
      .getResponse();

  });

  }
}

I get this invalid response

{
  "version": "1.0",
  "userAgent": "ask-node/2.12.1 Node/v16.16.0"
}

instead of the desired one. Other handlers that do simpler tasks (such as just returning a speech response) work fine. The MQTT publishing also works (I see the messages on the broker).

I'm using the Alexa SDK v2 and the AWS SDK.

I'm aware that the issue might be in the fact that I put the return statement in the callback function, but I don't know how to send a response back to Alexa in other ways. I have tried to wrap iotData.publish in another function but I'm only able to log data to console that way.

Any help about this issue would be very appreciated.