繁体   English   中英

在Node.js中搜索列表以获取Alexa技能

[英]Search list in nodejs for Alexa skill

我正在尝试使用Alexa技能,而在AWS Lambda中执行后端代码时却陷入困境。 基本上,我有一个城市列表,如果用户对Alexa说列表中的一个城市,她应该做出回应,则该城市有效。 下面是我的城市列表,还有应该在列表中排序以找到匹配项的方法。

var data = [
    "San Mateo.",
    "San Francisco.",
    "Palo Alto.",
    "Redwood City.",
    "New York.",
    "Boston.",
    "Chicago.",
    "La Jolla.",
    "San Diego.",
    "San Carlos.",
    "San Bruno."
];

exports.handler = function(event, context, callback) {
    var alexa = Alexa.handler(event, context);
    alexa.APP_ID = APP_ID;
    alexa.registerHandlers(handlers);
    alexa.execute();
};

var handlers = {
     'LaunchRequest': function () {
          this.emit('GetNewFactIntent');
     },

     'GetNewFactIntent': function () {
          var cityName = this.event.request.intent.slots.value;
          var factArr = data;

          for(var i = 0; i < factArr.length; i++){
              if(cityName.equals(factArr[i].value)){
                 this.emit(":tell", LYFT_IS_AVALIABLE);
              }
          }

          this.emit(":tell", LYFT_NOT_AVALIABLE);
      }
}

因此,只是一个快速提示,您实际上并没有提出问题,只是粘贴了一些代码并说了您想要实现的目标,这就是为什么有人故意低估您的原因。

因此,假设您拥有的数组是您可以做的列表,那么您想使用的是点表示法:

var data = [
    "San Mateo.",
    "San Francisco.",
    "Palo Alto.",
    "Redwood City.",
    "New York.",
    "Boston.",
    "Chicago.",
    "La Jolla.",
    "San Diego.",
    "San Carlos.",
    "San Bruno."
];

exports.handler = function(event, context, callback) {
    var alexa = Alexa.handler(event, context);
    alexa.APP_ID = APP_ID;
    alexa.registerHandlers(handlers);
    alexa.execute();
};

var handlers = {
    'LaunchRequest': function () {
        this.emit('GetNewFactIntent');
    },

    'GetNewFactIntent': function () {
        var cityName = this.event.request.intent.slots.value;

        // this is like doing data['San Mateo.']
        if(typeof data[cityName] !== 'undefined'){

            this.emit(":tell", LYFT_IS_AVALIABLE);

        } else {
            this.emit(":tell", LYFT_NOT_AVALIABLE);
        }
    }
}

您还可以在Lambda中使用console.log(someVariable)并在CloudWatch Logs中检查结果-仅供参考您的城市是否需要'。' 在末尾?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM