Discord.js Bot to monitor streamers

Hello,

I am new to JS and even more new to APIs. I have a node.js discord bot (running locally at home) on my discord server and I want to incorporate a feature that will send an embed on a specific discord channel when someone on my server goes live on twitch. I want to use API to do it.

My issue is i get authentication errors when I try the example url located here. I am also not sure what I need to have my oauth redirect to be.

Thanks in advance to anyone who can help me out.

All of Helix requires an oAuth token to be specified.

For your use case a server to server token, aka client credentials makes the most sense

Since just using Server to server, just leave it as localhost. You only need a redirect URI if you are needing to authenticate a user

Ok. So I would need a html send and request for JavaScript and send that at the beginning? Then it should respond with a json that I can save and use for my token. Is there other things I need to authenticate a channel request? And how would I save the responses from Twitch? Would I need a database like program on the bot as well?

Sorry if theses questions seam quite simple but I really don’t know what I’m doing. Thanks again

For a user token yes
For a server to server token no.

If you use Twitch Webhooks instead of long polling the API you can get away with not storing anything

So I would run the post once for server to server and use the return key for my requests?

What is the difference in information that Webhooks provide from long-pulling?

Also how would the request look different for long pulling and Webhooks. For Webhooks they showed this: “ /api/webhooks-reference/#subscribe-tounsubscribe-from-events” if I wanted to subscribe to a specific streamer how would I write it out and include my key.

And finally, how to I capture a response form Twitch? Would I need an async function to await for some response?

Nothing.

The webhook sends the same payload as the streams API would

You make a request to the Webhooks hub to create a subscription (which remains active for up to 10 days). Then when the stream goes online/offline/changes title you get a POST’ed payload to your Callback

The structure of the POST to create a webhook subscription, for the headers is the same as any API request to Helix.

You just need to POST a JSON blob that tells Twitch what topic you want and how long you want it for. And setup a callback handler to receive (and verify the subscription)

Example:

{
    'hub.callback': 'https://yourURL/',
    'hub.mode': 'subscribe',
    'hub.topic': 'https://api.twitch.tv/helix/streams?user_id=USERID',
    'hub.lease_seconds': 10,
    'hub.secret': 'somesecret'
}

For long polling you just load the streams API once per minute every minute, (wasting resources and API requests), Webhooks you get data when data changes as Twitch tells you data has changed.

The callback handlebar will need the token? Is that the verification you were referring to?

No

  1. You make a POST to Twitch at https://api.twitch.tv/helix/webhooks/hub with a App access Token and ClientID in the header, with a JSON blob similar to whats above, you get a 204 (No Content) or a 202 Accepted (I forget which offhand)

  2. Twitch makes a GET request to your callback URL, to check that it’s valid, Twitch includes a hub.challenge as a Query string parameter

  3. You Echo back that challenge

  4. Then when data occurs, Twitch will make a POST to your callback with the data.

Steps 2/3 are the Verification I refer to

I provide a couple of Callback handler examples here https://github.com/BarryCarlyon/twitch_misc/tree/master/webhooks/handlers

So. I’m still stuck at getting my OAUTH token. I put this into my browser with my proper ids but i didn’t get anything. just a failed page.

https://id.twitch.tv/oauth2/token?client_id=&client_secret=&grant_type=client_credentials

Also would this work as the subscribe request or am I way off?

const requesttopic = {

    'hub.callback': 'HTTPS://YOURURL',

    'hub.mode': 'subscribe',

    'hub.topic': 'https://api.twitch.tv/helix/streams?user_id=USERID',

    'hub.lease_seconds': 864000,

    'hub.secret': 'SOMESECRET'

};

// The parameters of fetch

const options = {

    method: 'POST',

    Headers: {

        'Authorization': 'Bearer <OAUTH TOKEN>',

        'Client-Id': 'eaj131p033pzugihwnjazrl4peuhfn',

        'Content-Type': 'application/json'

    },

    body: JSON.stringify(requesttopic),

};

// Sending fetch to webhook hub

app.post('https://api.twitch.tv/helix/webhooks/hub', function (req, res) {

    res.send(options)

Putting it in a browser sends a GET request, to get a token requires a POST request.

Looks about right, depends if what you have written is correct for whatever app is

for app i have:

const express = require(‘express’);
const app = express();

for the token? would i do something like this?

app.post('https://id.twitch.tv/oauth2/token?client_id=eaj131p033pzugihwnjazrl4peuhfn&client_secret=<SECRET>&grant_type=client_credentials', function (req, res) {

    res.send();

    req.console.log(req);

});

or would I do it a bit different?
thanks

express handles in bound HTTP requests, it cannot make outbound requests.

You created a page on your website of

http(s)://somepage.com/https://id.twitch.tv/oauth2/token?client_id=eaj131p033pzugihwnjazrl4peuhfn&client_secret=<SECRET>&grant_type=client_credentials

Responding to inbound HTTP Post requests

ah, yeah sorry. I am using node-fetch.js an ran this (Assuming const fetch = require('node-fetch'); ):

fetch('https://id.twitch.tv/oauth2/token?client_id=eaj131p033pzugihwnjazrl4peuhfn&client_secret=<SECRET>&grant_type=client_credentials', { method: 'POST'})

    .then(res => res.json()) // expecting a json response

    .then(json => console.log(json));

and I got my key back. Do I have to rerun that every month or so because it looks to have an expiration?

Now I have to change my other code to do fetch() and set up a handler for when Twitch responds. Can I use express for the handler? And am I missing anything else? Thanks

Ok, a few more questions regarding the subscribe request:

  1. For the callback URL, can it be either a DomainName:port or a PublicIP:port? Or is it something different?

  2. What is the hub.secret? is it something i can define and how does it work?

  3. Is is possible to subscribe to multiple users in one request or do I need to make it run everytime for every user? If i can do it in one command, what would the format look like? If every user, what is the best way of accomplishing this because i don’t just want to paste the code in 10 different times and change the user part.

Thanks again

Yes but also no.

Ideally you would be operating over SSL

Yes, you define the secret and then a head is signed with the secret so you know Twitch sent it. As only Twitch knows the secret you pick.

For example

No, there are no bulk subscribers, each stream/follow topic accepts one user_id etc

I’m not all that familiar with SSL and I just have a public IP and port forwarding(Because I’m hosting locally from home). But having 'hub.callback': 'http://<PUBLIC_IP>:8000', will work?

For non sensitive topics sure.

But you really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really shouldn’t be home hosting this.

If you are running from home, go back to polling the API

long poll and wait for the data to change basically? Would I log the times when data is received to compare the differences? Also i believe i can pull multiple users at once by just long pulling right?

yup

yeah streams endpouint lets you lookup 100 users at once