【问题标题】:Why am I not able to access the HTTP call in this node.js app?为什么我无法访问此 node.js 应用程序中的 HTTP 调用?
【发布时间】:2021-07-01 09:00:47
【问题描述】:

我正在尝试使用 Twilio、AWS 和 Claudia Bot 构建一个简单的 Node Js 机器人。

我正在尝试访问this public API,尤其是当用户的文本中包含“事实”一词时的“文本”部分。目前,当用户文本包含“引用”时,我能够访问第一个 API,但我无法检索到事实。

有什么想法吗?

var botBuilder = require('claudia-bot-builder');
var greeting = require('greeting');
var fetch = require('node-fetch');

var bot = botBuilder(function(message) {
  if (message.text.match(/quote/i)) {
    return fetch('http://api.forismatic.com/api/1.0/?method=getQuote&format=text&lang=en').then(function(res) {
      return res.text();
    });
  } else if (message.text.match(/fact/i)){
    return fetch('https://uselessfacts.jsph.pl/random.json?language=en').then(function(res) {
      return res.json(text);
    });
  } else {
    return greeting.random();
  }
}, { platforms: ['twilio'] });

module.exports = bot; 

【问题讨论】:

  • 你能澄清一下这个问题吗?你有任何错误,超时吗?你能提供任何调试信息吗?

标签: node.js amazon-web-services twilio claudiajs


【解决方案1】:
return fetch('https://uselessfacts.jsph.pl/random.json?language=en').then(function(res) {
      return res.json(text);
});

这里text 是未定义的,所以它会返回一个错误。试试这个:

fetch('https://uselessfacts.jsph.pl/random.json?language=en').then(function(res) {
      return res.json();
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-05
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    相关资源
    最近更新 更多