Icanhazdadjoke Custom command

I’m very unfamiliar with the use of APIs, but I found this documentation page. How could I pull a plaintext response for a random dad joke?

https://icanhazdadjoke.com/api

Type this into your chat:

!commands add !joke $(eval jokeString = decodeURIComponent("$(querystring $(urlfetch json https://icanhazdadjoke.com/slack))"); try{joke = JSON.parse(jokeString); (joke&&joke.attachments&&joke.attachments[0]&&joke.attachments[0].text || `Unable to parse joke: ${jokeString}`).substr(0,400)}catch(e){`Unable to parse joke: ${e.message}: ${jokeString}`.substr(0,400)})
1 Like

Thanks, want expecting a reply so soon.

The following error is what I got…

Unable to parse joke: Unexpected token E in JSON at position 0: Error Connecting To Remote Server

“Error Connecting To Remote Server” means Nightbot had trouble connecting to the joke API. Try the command again and it should work.

I’ve tried a few times over the last 8 hours in case there was some sort of time out happening but I still continue to get the same error in my channel. :cry:

It works every time I try it. You must not have copied the command correctly.

Copy this text EXACTLY and paste it into your chat:

!commands edit !joke $(eval jokeString = decodeURIComponent("$(querystring $(urlfetch json https://icanhazdadjoke.com/slack))"); try{joke = JSON.parse(jokeString); (joke&&joke.attachments&&joke.attachments[0]&&joke.attachments[0].text || `Unable to parse joke: ${jokeString}`).substr(0,400)}catch(e){`Unable to parse joke: ${e.message}: ${jokeString}`.substr(0,400)})

You’re absolutely right. It’s my fault for changing the command to be !dadjokes and didn’t realize I would need to modify the rest.

What else needs to be changed in the string if I want it to be dadjoke instead?

Again thanks for all your help!

!commands add !dadjoke $(eval jokeString = decodeURIComponent("$(querystring $(urlfetch json https://icanhazdadjoke.com/slack))"); try{joke = JSON.parse(jokeString); (joke&&joke.attachments&&joke.attachments[0]&&joke.attachments[0].text || `Unable to parse joke: ${jokeString}`).substr(0,400)}catch(e){`Unable to parse joke: ${e.message}: ${jokeString}`.substr(0,400)})

Replace add with edit if you already have a command named !dadjoke.

All good now, thanks again for all the help!

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