【问题标题】:Wikipedia's API not working in Alexa skill using Request moduleWikipedia 的 API 在使用请求模块的 Alexa 技能中不起作用
【发布时间】:2020-10-02 14:07:58
【问题描述】:
const GetInfoIntentHandler = {
    canHandle(handlerInput) {
        const request = handlerInput.requestEnvelope.request;
        return (request.type === 'IntentRequest'
        && request.intent.name === 'GetInfoIntent');
    },
    handle(handlerInput) {
        let data;
        const request = require("request");

        let options = { method: 'GET',
            url :  "https://en.wikipedia.org/w/api.php?format=json&origin=*&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles=manulife&redirects=",
            qs: 
            { action: 'query' } };

        request(options, function (error, response, body) {
            if (error) throw new Error(error);
            let json = body;
            let obj = JSON.parse(json);
            data = obj;

        });
        const x = "Hello";
        const speechOutput = data;
        return handlerInput.responseBuilder
        .speak(speechOutput)
        .getResponse();
    },
};

我得到的回复是Undefined。其他 API 也不起作用。我需要使用 HTTP API 吗?我已经尝试了所有方法,但似乎没有任何效果。

我已经安装了请求依赖项。

我只想让 Alexa 返回地址的第一段。

【问题讨论】:

    标签: json api mediawiki alexa-skill


    【解决方案1】:

    您收到Undefined,因为没有值分配到变量data中。 因为您的函数在从 url 获取数据之前返回。

    将return语句放入请求块中。还要从obj 对象中提取您要返回的属性。

    const request = require("request");
    const getData = function() {
    return new Promise((resolve, reject) => {
        let options = { 
            method: 'GET',
            url :  "https://en.wikipedia.org/w/api.php?format=json&origin=*&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles=manulife&redirects=",
            qs: {
                action: 'query' 
            } 
        };
        request(options, function (error, response, body) {
            if (error) reject(error);
            resolve(JSON.parse(body)); // you may want to check parsing error.
        });
     });
    }
    const HelloWorldIntentHandler = {
      canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 
           'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 
           'HelloWorldIntent';
    },
     async handle(handlerInput) {
        const response = await getData();
        console.log(response);
        return handlerInput.responseBuilder
        .speak(response.query.pages["1928349"].extract)
        .getResponse();
     }
    };
    

    使用您提供的以下网址是您应该期望的响应:

    {
         "batchcomplete": "",
         "query": {
         "normalized": [
            {
                "from": "manulife",
                "to": "Manulife"
            }
          ],
          "pages": {
                "1928349": {
                "pageid": 1928349,
                "ns": 0,
                "title": "Manulife",
                "extract": "Manulife Financial Corporation (also known as Financière 
                            Manuvie in Quebec) is a Canadian multinational insurance 
                            company and financial services provider headquartered in 
                            Toronto, Ontario, Canada. The company operates in Canada and 
                            Asia as \"Manulife\" and in the United States primarily 
                            through its John Hancock Financial division. As of December 
                            2015, the company employed approximately 34,000 people and had 
                            63,000 agents under contract, and has CA$935 billion in assets 
                            under management and administration. Manulife services over 26 
                            million customers worldwide.Manulife is the largest insurance 
                            company in Canada and the 28th largest fund manager in the 
                            world based on worldwide institutional assets under management 
                           (AUM).Manulife Bank of Canada is a wholly owned subsidiary of 
                            Manulife."
              }
            }
          },
       "limits": {
        "extracts": 20
      }
     }
    

    【讨论】:

    • 非常感谢您的回复。我在请求块中添加了返回语句,但我收到“请求的技能有问题”错误。我不确定用 obj 添加什么。我尝试了 obj.pages 但得到了同样的错误。
    • 您好,对于这个特定的响应,您必须说出一个不是对象的属性。在这种情况下,您应该使用 obj.query.pages["1928349"].extract
    • 感谢您的回复。我正是这样做的,但仍然是同样的错误。我将发布我的整个代码。如果你有几分钟,你能看看吗
    • 你的GetInfoIntentHandler 真的被解雇了吗?你能确认一下吗?还发布您在 alexa 技能模拟器中看到的输入请求
    • 请查看我对代码的回答。非常感谢你
    【解决方案2】:
    const GetInfoIntentHandler = {
        canHandle(handlerInput) {
            const request = handlerInput.requestEnvelope.request;
            return (request.type === 'IntentRequest'
            && request.intent.name === 'GetInfoIntent');
        },
        handle(handlerInput) {
            let data;
            const request = require("request");
    
            let options = { method: 'GET',
                url :  "https://en.wikipedia.org/w/api.php?format=json&origin=*&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles=manulife&redirects=",
                qs: 
                { action: 'query' } };
    
            request(options, function (error, response, body) {
                if (error) throw new Error(error);
                let json = body;
                let obj = JSON.parse(json);
                return handlerInput.responseBuilder
                .speak(obj.query.pages["1928349"].extract)
                .getResponse();
    
            });
            const x = "Hello";
            
        },
    };
    

    这是 JSON 输出 { “身体”: { “版本”:“1.0”, “回复”: {}, "userAgent": "ask-node/2.9.0 Node/v10.22.1 sample/wiki-app/v1.0", “会话属性”:{} } }

    这是我的错误 "请求的技能响应有问题"

    【讨论】:

      【解决方案3】:
      const GetInfoIntentHandler = {
          canHandle(handlerInput) {
              const request = handlerInput.requestEnvelope.request;
              return (request.type === 'IntentRequest'
              && request.intent.name === 'GetInfoIntent');
              
          },
          handle(handlerInput) {
              const speakOutput = 'reached intenthandler';
      
              return handlerInput.responseBuilder
                  .speak(speakOutput)
                  .reprompt(speakOutput)
                  .getResponse();
          }
      };
      

      此代码有效,这意味着我的 getinfointenthandler 确实有效。

      const GetInfoIntentHandler = {
          canHandle(handlerInput) {
              const request = handlerInput.requestEnvelope.request;
              return (request.type === 'IntentRequest'
              && request.intent.name === 'GetInfoIntent');
              
          },
          handle(handlerInput) {
              let data;
              const request = require("request");
              let options = { method: 'GET',
              url :  "https://en.wikipedia.org/w/api.php?format=json&origin=*&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles=manulife&redirects=",
              qs: 
              { action: 'query' } };
              request(options, function (error, response, body) {
                  if (error) throw new Error(error);
                  let json = body;
                  let obj = JSON.parse(json);
                  return handlerInput.responseBuilder
                  .speak(obj.query.pages["1928349"].extract)
                  .getResponse();
              });
              
          },
          
      };
      

      此代码不起作用。它在启动请求后停止。

      这是我的 JSON 输入

      "request": {
              "type": "SessionEndedRequest",
              "requestId": "amzn1.echo-api.request.f26816fa-672b-4e35-9de2-126780cfb1ef",
              "timestamp": "2020-10-05T20:52:16Z",
              "locale": "en-US",
              "reason": "ERROR",
              "error": {
                  "type": "INVALID_RESPONSE",
                  "message": "SpeechletResponse was null"
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-27
        • 1970-01-01
        • 2017-11-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多