Need a little help with API development

So I’ve created a javascript (embedded into an html page) with a similar functionality to the “Stay Hydrated Bot” and I’ve been trying to use it with Nightbot.
HTML
Javascript

Now I’ve stumbled upon the following problem.
When I call the API through my browser it works perfectly fine.
But when I try calling it using $(urlfetch) instead of using the result it fetches the entire html code and runs out of characters.

I’ve compared what I made to others and realized that their actual content of the webpage is just the result instead of an html page. I couldnt figure out a way how to get that to work for me.
Alternatively, a way to make jquery work in $(eval) would work as well

Can anyone help me out with that?
(Sidenode: I’m rather new to Javascript and creating APIs)

Thanks in advance!

@guardie

The way wasser.js is written, this would be the way to use your script in a Nightbot command:

$(eval $(urlfetch json http://www.guardelola.net/scripts/darko/wasser.js))

The $(eval) variable executes the $(urlfetch) result as raw JS. Unfortunately as it is right now, this command setup wouldn’t work because $(eval) doesn’t support jQuery. I noticed you’re using jQuery to get a result from the DecAPI uptime endpoint. To work around having to use jQuery, you could set up a $(twitch) variable inside $(eval) to get the uptime as a string “uptime”, then rewrite wasser.js to handle the output for Nightbot’s response using “uptime”.

$(eval const uptime = `$(twitch $(channel) "{{uptimeLength}}")`;$(urlfetch json http://www.guardelola.net/scripts/darko/wasser.js))

To actually write a custom API that works with Nightbot, typically you would write something in a server-side scripting language, for example PHP, that returns only a plain text result.

$(urlfetch yourwebsite.xyz/foobar.php)

Oooh. I didnt realize php returned a plain text, ill try that. Thank you!

Also i never thought about just inserting the javascript urfetch into an eval. ill try if that works. (if it does that would be amazing)

Thanks a lot ^^

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.