Hey guys. I am having troubles figuring out just what it is I need to do in order to get the Subscription verify request from twitch helix for new follows.
I have successfully been able to setup an IRC bot and retrieve follows(using helix) but getting the subscription thing working has me broken.
I am using c# in unity.
Here is what I am attempting to do but it is returning null:
public void GetLiveFollowers()
{
//print(userName);
string userID = GetUserID(userName);
// Bacon_Donut: 36155872
// imaqtpie: 24991333
string subFollowersAlert = "https://api.twitch.tv/helix/webhooks/hub?hub.mode=subscribe&hub.topic=https://api.twitch.tv/helix/users/follows?to_id=24991333&hub.callback=http://somesite.com/trackfollow/callback.html&hub.lease_seconds=864000";
try{
//certificate handling
ServicePointManager.ServerCertificateValidationCallback = CertificateValidationMonoFix;
//Make request
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(subFollowersAlert);
//set headers
req.Method = "POST";
req.Headers.Add("Client-ID", clientID);
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
Stream resStream = res.GetResponseStream();
StreamReader sr = new StreamReader(resStream);
// Attempt at receiving a challenge and sending mathcing verify response
//StreamWriter writer = new StreamWriter(resStream);
var message = sr.ReadLine(); //sr.ReadLine();
if (message.Contains("hub.challenge")){
print("received challenge");
}
string srValue = sr.ReadToEnd();
//Serialize response to readable format
var followers100 = JsonConvert.DeserializeObject<LiveFollowersRoot>(srValue);
print(resStream.ToString()); //null response
print(srValue);
print(followers100);
//print("From: " + followers100.from_id + "\nDate: " + followers100.followed_at);
}
catch(Exception ex){
print(ex.ToString());
}
}
Do I need some kind of async web request done or am I just completely off the mark? If someone can provide me with the code I need to get to a point of twitch sending me a challenge then I can work from there.
You make a request to Twitch to subscribe to data.
Then twitch sends you the data to the URL/EndPoint you have specified.
So yes, you need to be web accessible from the world in order to receive the webhook data sent by twitch. How you achieve that is up to you, you can open a hole in your firewall for your bot, or use a “proper” web server
When you make a subscription request, the only response you will get is whether or not the subscription is successful.
To get the updates, you need to properly handle updates being sent by Twitch to the endpoint you have specified in hub.callback. You will only receive updates here once a subscription request succeeds, and only for the duration specified in hub.lease_seconds
We worked to conform closely to the W3 spec for webhooks https://w3c.github.io/websub/ Hopefully this can answer some of your questions.
I have a callback page working somewhat I think. I have POST request being made via ajax and get a 202 response as seen in my network inspector on chrome.
Do I not need to POST a matching challenge or is the subscription complete with that?
I’ve been searching and trying to find information on how to handle this in C# as well. Unfortunately all I could find so far have been “tutorials” for the WebHooks Visual Studio handles natively, which essentially does everything for you without telling you how it does it. The closest thing I could find that might be useful is this msdn tutorial to create your own rest api to handle the POST that Twitch sends to your call back URL. I have no idea if this is the right path to go down, so this very well may be a rabbit hole that may be all for nothing. It’s an interesting read at the very least.
I was referring to handling the POST that Twitch sends you, not the POST that you sent Twitch to subscribe to the WebHook. Sending the subscription request is the easy part.
From my understanding we would have to make both a full blown client application that interacts with a callback. The subscription post is confirmed with 202 and then twitch send a GET to the callback at which point we need to ensure we have an endpoint to handle it.
In my case, I need to make both a web application (with its own api for a GET endpoint from twitch) and a unity application.
All good, I was kind of vague in my first post. I feel like this might be one of those situations that just requires a bit of reading and trial and error. Well, and maybe some money to host a domain
That was my train of thought as well. The link in my first post does a little walk-through of how you can make your own Rest API, i.e. your own endpoints. I haven’t done any actual work with that article yet, but it seems like a good starting point.
I actually have an API built for a website at work and it wasn’t all that hard but having to make another API to interact with a twitch api all just to send a single response to complete a handshake seems like a bit much imo.
This is why I feel like there is some solution that is simpler that I am missing
I think the call back is to serve a double purpose, too not only handle the data that Twitch gives to you but to also finishing the handshake with the 2XX status response. I’m new to WebHooks in general, so this just my best guess.
I haven’t started using webhooks yet, but to me it looks to be as simple as having a web server that needs a single route. During the subscription process Twitch will send a GET request to that, for subscription verify or subscription deny. When a subscription is active, and an event for that subscription happens, Twitch will send a POST request to that route containing the data. I’ve only had a brief glance at the docs though as webhooks are later on my todo list.
If you don’t want to, or can’t, have a web server running then poll the API as you would normally to get the data that webhooks would otherwise provide.
I planned to handle the webhooks through my bots control panel and have the panel forward it to my bot through its API. So it’s finally available? Woo
As others have said, yes, you need to properly handle the reqiest twitch sends to the endpoint you provided. I myself have used the following two methods to receive said callbacks for different purposes:
A cgi Script. Cgi stands for “common gateway interface” and is the most basic way of executing scripts serverside, but is basically as old as the internet and has some inherent flaws, so i recommend only using it if the others dont work for you. Though its advantage is that you can basically use any programming language that supports cgi, which ranges from java to python to c#, (this is not a complete list)
A node.js application. That’s the more modern way to do it, and as i know how to google it was not too hard to set up to work even alongside apache2 to redirect requests on port 80. This locks you to javascript, but has a lot of neat and easy libraries
Do note, that in whatever way you want to do this, this will be an application running on a Server, not within your unity app, so you will have to manually use a custom app of your own which connects your players/users together and sends them information.
As you are looking into this, i assume you need live updates of follows - if you dont and just want to check for them periodically, use the normal api endpoints, that’s what they’re for.
Live follows are indeed what I am looking for. I have a somewhat working method via retrieval of a users followers in an initial list and then checking it again(assign second check to diff list) and checking for the differences in the two.
Still working on some kinks as the indexes occasionally mess up for some unknown reason resulting in an old follow being looked at as a new one.
I would like to have a node server set up and I can do it quite easily but I would like to have scalability.
I will need to have a twitch authentication as well so that I can get the other webhook alerts as well. It is the weekend again so that means a larger chunk of time to work on my app. Hoping to get something functional with the new API vs my occasionally-breaking solution.