aichaos / rivescript-js

A RiveScript interpreter for JavaScript. RiveScript is a scripting language for chatterbots.
https://www.rivescript.com/
MIT License
377 stars 145 forks source link

Hello can anybody help me on how to get data from an api using javscript. I am trying in rivescript playground but it is showing object not found. #368

Closed somesh1234567 closed 2 years ago

somesh1234567 commented 3 years ago
> object testing javascript
    return fetch('https://api.openweathermap.org/data/2.5/forecast? 
           id=1269750&appid={APPID}&units=metric')
        .then(res => res.json())
       .then(data => return(data.list[0].main.temp);)
< object

+ test javascript
- Testing javascript... <call>testing</call>.
kirsle commented 3 years ago

Hello,

Your script had some syntax errors in it. When I pasted your code into the RiveScript Playground and clicked "Run" with the Debug checkbox open, the RiveScript debug log printed some errors when evaluating the object macro:

With those problems resolved, the macro parses and makes the HTTP request but gets an Unauthorized error from the API (probably because of the "{APPID}" placeholder in the API URL not being correct). https://play.rivescript.com/s/QAFrFxptew

Bot says: "Testing javascript... [ERR: Error raised by object macro]."

So we got the fetch() trying to do its thing, if you have more trouble with it (with your correct {APPID} in there, etc.), that will be between you and the OpenWeathermap API to resolve further (it may need CORS headers to allow cross origin requests between the play.rivescript.com domain and the weather API, etc.)