Urlfetch Nightbot API for song_requests::queue

Nightbot’s Urlfetch variable currently sends along name={name}&displayName={displayName}&provider=twitch&providerId={pId} as Nightbot-Channel header;

however the (undocumented public/unauthorized/token-less) end-point https://api.nightbot.tv/1/song_requests/queue requires either a Nightbot-Channel header or channel query parameter in the form of a 12-byte nightbot channel id (e.g. 5b215c90e9e70666b399a593), but the header is priotized over the query parameter.

So even with the channel query parameter, the Nightbot-Channel header from Urlfetch messes this up and causes a {"status":404,"message":"Channel does not exist"} - which is a weird incompatibility between Nightbot commands and Nightbot API.

I know this may not be an intended way to use those apis, but I’m still asking this out of curiosity because I find that some nightbot features are not documented (for example the aforementioned song queue end-point, or the json switch in $(urlfetch json <url>)):

Is there any way around this issue aside from setting up a proxy server, which would incur additional network delay?

Thanks.

Background: I want to replicate !songs current command while song request is disabled.

curl test outputs
$ curl https://api.nightbot.tv/1/song_requests/queue/?channel=5b215c90e9e70666b399a593 -s
{"_total":0,"_currentSong":{"_id":"5b29cd367cb97a07e56c04d1","createdAt":"2018-06-20T03:42:46.113Z","updatedAt":"2018-06-20T03:42:47.381Z","track":{"providerId":"167434994","provider":"soundcloud","duration":228,"title":"Draper - Joyride","artist":"Monstercat","url":"http://soundcloud.com/monstercat/draper-joyride"},"user":{"name":"ngokimphu","displayName":"ngokimPhu","providerId":"81957093","provider":"twitch"}},"_requestsEnabled":false,"status":200,"queue":[]}
$ curl https://api.nightbot.tv/1/song_requests/queue/?channel=5b215c90e9e70666b399a593 -sH'Nightbot-Channel:name=ngokimphu&displayName=ngokimPhu&provider=twitch&providerId=81957093'
{"status":404,"message":"Channel does not exist"}
$ curl https://api.nightbot.tv/1/song_requests/queue/ -sH'Nightbot-Channel:5b215c90e9e70666b399a593'
{"_total":0,"_currentSong":{"_id":"5b29cd367cb97a07e56c04d1","createdAt":"2018-06-20T03:42:46.113Z","updatedAt":"2018-06-20T03:42:47.381Z","track":{"providerId":"167434994","provider":"soundcloud","duration":228,"title":"Draper - Joyride","artist":"Monstercat","url":"http://soundcloud.com/monstercat/draper-joyride"},"user":{"name":"ngokimphu","displayName":"ngokimPhu","providerId":"81957093","provider":"twitch"}},"_requestsEnabled":false,"status":200,"queue":[]}
$ curl https://api.nightbot.tv/1/song_requests/queue/ -sH'Nightbot-Channel:name=ngokimphu&displayName=ngokimPhu&provider=twitch&providerId=81957093'
{"status":404,"message":"Channel does not exist"}
sample command that uses proxy
@$(touser) $(eval `$(urlfetch json https://cors-proxy.htmldriven.com/?url=https://api.nightbot.tv/1/song_requests/queue?channel=5b215c90e9e70666b399a593)`.match(/([^"]*)","artist":"([^"]*)/).slice(1).join(' - '))

(Don’t use this command though, it fails when song title/artist contains " character)

https://api.nightbot.tv/1/song_requests/queue requires authorization in every use case. There is always an Authorization Header sent with the request. Your best bet is the utilize the API as intended by the docs.

As for the undocumented nightbot features, there is a PR open for the documentation.

You can fix the issue with the command that uses a proxy by parsing the response as JSON.

@$(touser) $(eval p=decodeURIComponent("$(querystring $(urlfetch json https://cors-proxy.htmldriven.com/?url=https://api.nightbot.tv/1/song_requests/queue?channel=5b215c90e9e70666b399a593))");try{t=JSON.parse(JSON.parse(p).body)._currentSong.track;`${t.title} - ${t.artist}`}catch(e){`Failed to parse proxy response: ${e.message}: ${p}`.slice(0,400)})

In my tests, the proxy you provided does not cause any noticeable delay.

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