Robot Engage Blog Buy Now
Blog

Misty Community Forum

Getting news data with misty

Hi Everyone I am trying to use simple send external request to the newsapi and fetch the headlines using the SendExternalRequest command but I am facing difficulties with that.

When I use an external request command It does return a object, but when I try to parse the values it does not return any values, I am not sure what is going wrong over here. If anyone can take a look that will be great.

Here is my code:

misty.Debug("News skill running");

misty.SendExternalRequest("GET", 'https://newsapi.org/v2/top-headlines?' + 'country=ca&' + 'apiKey=63756dc5caca424fb3d0343406295021', "", "", 0, false, false, "", "");

function _SendExternalRequest(data)

{

misty.Debug("Inside function");

_data = JSON.parse(data.response.articles.Data)

_title = _data.articles._title

_description = _data.articles._description

misty.Pause(1000);

misty.ChangeLED(70, 200, 80);

misty.Debug("I dont receive anything" + _title);

misty.Debug("Misty here! Just letting you know it's " + _title + " and " + _description );

misty.Speak("Misty here! Just letting you know it is " + _title + " and " + _description );

}

and here is the debug screen

Is the JSON.parse argument correct? In particular, did you try the following?

_data = JSON.parse(data.Result.ResponseObject.Data);

Hi, @sp22!

Looks like there’s a couple of things going on.

First, definitely try @slivingston’s suggestion. Response data from external requests comes back to your skills inside the data.Result.ResponseObject.Data object.

Second, try removing all of the optional parameters from your call on the SendExternalRequest method. We only need the HTTP verb and the URL for your particular request, and the others aren’t needed. I think we might be getting some issues for some of those arguments where empty string values are being used in places where null values would be preferred, so better to simplify:

misty.SendExternalRequest("GET", 'https://newsapi.org/v2/top-headlines?' + 'country=ca&' + 'apiKey=63756dc5caca424fb3d0343406295021');

Third, it looks like article data for this API comes back in an array, so we can’t just reference the title property without specifying an element of the articles array.

I took a few seconds to modify your code a bit, and this works. It may not be exactly the functionality you need, but it should be easy to customize this to get the results you want :slight_smile:

misty.Debug("News skill running");

misty.SendExternalRequest("GET", 'https://newsapi.org/v2/top-headlines?' + 'country=ca&' + 'apiKey=63756dc5caca424fb3d0343406295021');

function _SendExternalRequest(data) {

    misty.Debug("Inside function");
    _data = JSON.parse(data.Result.ResponseObject.Data); // Our response data
    misty.Debug(JSON.stringify(_data.articles)); // Prints the articles array
    _articlesList = _data.articles // Store the articles array
    _firstArticleTitle = _data.articles[0].title // Get the title of the first article at index 0
    _firstArticleDescription = _data.articles[0].description // Get the description of first article at index 0

    misty.Pause(1000);
    misty.ChangeLED(70, 200, 80);

    misty.Debug("First article: " + _firstArticleTitle + _firstArticleDescription);
    misty.Speak("First article: " + _firstArticleTitle + _firstArticleDescription);
}

yes It did worked. I completely missed result in parser.
Thanks @johnathan and @slivingston .

1 Like