Why am I not able to access the HTTP call in this node.js app?

64 Views Asked by At

I am trying to build a simple Node Js bot with Twilio, AWS & Claudia Bot.

I'm trying to access this public API, and specifically the "text" part whenever a user's text contains the word "Fact". Currently, I am able to access the first API when a users text contains "Quote", but I am unable to retrieve the fact.

Any thoughts?

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; 

1

There are 1 best solutions below

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

Here text is undefined so it will return an error. Instead try this:

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