0

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; 

Simbeul
  • 441
  • 4
  • 11
  • Can you clarify the issue? Do you get any errors, timouts? Can you provide any debuging info? – Marcin Jun 30 '21 at 21:52

1 Answers1

0
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();
});
trizin
  • 347
  • 2
  • 13