0

Alexa スキルからトリガーされる Lambda 関数から IoT トピックに発行しようとしています。Lambda 関数では、次の IoT パブリッシュを実行しています。

var params = {
  topic: 'testTopic',
  payload: new Buffer('test message'),
  qos: 1
};

iotData.publish(params, function(err, data) {
  if (err) console.log('ERR: ', err); // an error occurred
  else if (data) console.log('DATA: ', data);  // successful response
});

このコード (およびこのコードのみ) を Lambda 関数で実行すると、正常に動作します。しかし、params と publish 関数を Alexa スキル テンプレートの onIntent 関数に入れ、それを新しい Lambda 関数に入れると、機能しません。(両方の Lambda 関数の設定とポリシーは同じです。)

IntentHandler.call() をコメントアウトすると、iotData.publish() が実行されるため、intentHandler 呼び出しが何らかの理由でそれをキャンセルしているように見えます。

onIntent: function (intentRequest, session, response) {
  var intent = intentRequest.intent,
      intentName = intentRequest.intent.name,
      intentHandler = this.intentHandlers[intentName];

  if (intentHandler) {
    console.log('dispatch intent = ' + intentName);

    var params = {
      topic: 'testTopic',
      payload: new Buffer('test message'),
      qos: 1
    };

    iotData.publish(params, function(err, data) {
      if (err) console.log('ERR: ', err); // an error occurred
      else if (data) console.log('DATA: ', data);  // successful response
    });

    intentHandler.call(this, intent, session, response);
  } else {
    throw 'Unsupported intent = ' + intentName;
  }
}
4

1 に答える 1

0

これは、Lambda 関数のコンテキストに関連しているようです。intentHandler.call()は問題のインテントを呼び出し、インテントは を呼び出しますcontext.succeed()iotData.publish()これは、呼び出しが実行される前に Lambda 関数の実行が終了することを意味すると想定してintentHandler.call()いますiotData.publish()

onIntent: function (intentRequest, session, response) {
  var intent = intentRequest.intent,
      intentName = intentRequest.intent.name,
      intentHandler = this.intentHandlers[intentName];

  if (intentHandler) {
    console.log('dispatch intent = ' + intentName);

    var params = {
      topic: 'signals',
      payload: new Buffer(intentName),
      qos: 1
    };

    iotData.publish(params, function(err, data) {
      if (err) console.log(err, err.stack); // an error occurred
      else {
        console.log(data);           // successful response
        intentHandler.call(this, intent, session, response);
      }     
    });
  } else {
    throw 'Unsupported intent = ' + intentName;
  }
}
于 2016-07-26T16:50:37.283 に答える