【问题标题】:Using Alexa with other API将 Alexa 与其他 API 一起使用
【发布时间】:2017-07-24 18:42:00
【问题描述】:

我正在尝试开发 Alexa 技能,该技能可以找到用户所说的示例句子。我找到了这个 API(WordAPI),虽然当我打电话时,响应是未定义的。有人可以帮忙吗?

我的代码:

'use strict';
var Alexa = require('alexa-sdk');
var appId = 'this is valid';
var unirest = require('unirest');

var APP_STATES = {
    START: "_STARTMODE",
    TRANSLATE: "_TRANSLATE"
}

function getData(word){
    unirest.get("https://wordsapiv1.p.mashape.com/words/" + word)
    .header("X-Mashape-Key", "my key")
    .header("Accept", "application/json")
    .end(function (result) {
        return JSON.parse(result.body);
    });
}

exports.handler = function(event, context, callback){
    var alexa = Alexa.handler(event, context);
    alexa.appId = appId;
    alexa.registerHandlers(newSessionHandlers, startStateHandler, translateStateHandler);
    alexa.execute();
}

var newSessionHandlers = {
    'LaunchRequest': function(){
        this.handler.state = APP_STATES.START;
        this.emitWithState("BeginState", true);
    },

    'Unhandled': function () {
        this.emit(":tell", "Something went wrong");
    },
}

var startStateHandler = Alexa.CreateStateHandler(APP_STATES.START, {
    'BeginState': function(){
        var message = "You will say a word and I will give you facts about it, would you like to continue ?";
        this.emit(":ask", message, message);
    },

    'AMAZON.YesIntent': function(){
        this.handler.state = APP_STATES.TRANSLATE;
        this.emit(":ask", "Great, say a word !");
    },

    'AMAZON.NoIntent': function(){
        this.emit(":tell", "Ok, see you later !");
    },

    'Unhandled': function () {
        this.emit(":tell", "Something went wrong");
    },
});

var translateStateHandler = Alexa.CreateStateHandler(APP_STATES.TRANSLATE, {
    'GetWordIntent': function(){
        var word = this.event.request.intent.slots.word.value;
        console.log(getData(word));
        this.emit(":tell", "You said " + word);
    },

    'Unhandled': function () {
        this.emit(":tell", "Something went wrong");
    },

});

当我尝试使用 console.log 功能时会出现问题。它返回未定义。

    'GetWordIntent': function(){
      var word = this.event.request.intent.slots.word.value;
      console.log(getData(word));
      this.emit(":tell", "You said " + word);
    },

原始函数应该从调用返回解析数据。

function getData(word){
  unirest.get("https://wordsapiv1.p.mashape.com/words/" + word)
  .header("X-Mashape-Key", "my key")
  .header("Accept", "application/json")
  .end(function (result) {
    return JSON.parse(result.body);
  });
}

这还处于开发的早期阶段,我正在尝试 console.log 输出。这可能是一些我看不到的愚蠢错误。我替换了 appId 和 API 密钥。 API有效,我在其他场景中检查过。

任何线索或提示将不胜感激。

【问题讨论】:

  • 目前的形式还不清楚。你能指出究竟是什么调用出错了,什么是未定义的?
  • 好的,请稍等。我将编辑原始帖子。

标签: node.js api alexa


【解决方案1】:

您不会从您的 getData 函数返回任何值

试试

function getData(word){
    return unirest.get("https://wordsapiv1.p.mashape.com/words/" + word)
    .header("X-Mashape-Key", "my key")
    .header("Accept", "application/json")
    .end(function (result) {
        return JSON.parse(result.body);
    });
}

【讨论】:

    猜你喜欢
    • 2021-07-26
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    相关资源
    最近更新 更多