Twitch webhooks with python


#1

I’m trying to set up a twitch follow alert integration with my discord server. Here’s my python code so far

payload ={‘hub.mode’:‘subscribe’, ‘hub.topic’:'https://api.twitch.tv/helix/users/follows?first=1&to_id=’+apireqs.user_id,
‘hub.callback’:callback_url,
‘hub.lease_seconds’:“86400”, ‘hub.secret’:‘asecretpass’}
url = ‘https://api.twitch.tv/helix/webhooks/hub/
r = requests.post(url,headers=header, data= payload)

g = requests.get(url=callback_url,params=f’{callback_url}
hub.mode=subscribe&
hub.topic=https://api.twitch.tv/helix/users/follows?first=1&to_id={apireqs.user_id}&
hub.lease_seconds=864000&
hub.challenge=HzSGH_h04Cgl6VbDJm7IyXSNSlrhaLvBi9eft3bw’)

However, I get no messages on my server? What am I doing wrong? my POST request gives a 202 status code and my GET request gives a 200 one.


#2

Is your GET handler responding with the hub.challenge as well as the 200 status code?


#3

I’m not sure I’m following you. Sorry I’m new to webhooks.

My get request (which as far as I know is a verification request) is giving me a 200 response but I think the code is plain wrong. I really need help figuring out how to properly verify the response.


#4

I don’t know python so can’t help directly with the code.

The process though, is that you send a POST to Twitch with your subscription request.
Twitch then send a GET request to your callback URL to make sure it’s reachable which includes a hub.challenge, you have to respond to this GET request by replying the hub.challenge back to Twitch along with a 200 status code.

At this point, the subscription should be successfully created and you can verify that by checking the Get Webhook Subscriptions endpoint: https://dev.twitch.tv/docs/api/reference/#get-webhook-subscriptions


#6

No there aren’t webhooks for cheers or subscriptions, at least not yet.

And please don’t add off-topic questions to threads when you already had created a thread on this subject and should have asked there.


#7

I never used twitch’s webhooks, but I know about Python. I will tell you some errors or weird parts of your code, and with that, Dist could give you some notes for that things you dont understand about the documentation of twitch’s webhooks, if you have more doubts or problems.

Firts, the most weird part to me:

g = requests.get(url=callback_url,params=f’{callback_url} 
hub.mode=subscribe&
hub.topic=https://api.twitch.tv/helix/users/follows?first=1&to_id={apireqs.user_id}
&hub.lease_seconds=864000&hub.challenge=HzSGH_h04Cgl6VbDJm7IyXSNSlrhaLvBi9eft3bw’)

In your code, doesnt appear callback_url's value, but according to the variable name (and the use as value for payload’s key hub.callback), is a url of your own server. So, if you put that on url param of requests.get(), the request was made to yourself, not to twitch.

Furthermore, the params param (:V) of requests.get() is bad formated. Assuming that callback_url = "kappa.tv", params's value start with params="kappa.tvhub.mode=subscribe&...etc". And requests.get() then make a request over kappa.tv?kappa.tvhub.mode=subscribe&...etc (you can see the resulting url with print(g.url) ). Also is bad formated because you have hub.topic url as a GET param (those appears in the request url) being error prone on parse the URL for target server. And this is the second point.

payload dictionary is well formated. But is used on the wrong params. params param put the key-values appearing in the URL (for GET request), data put the key-values on the request body (for POST request) BUT … the content-type value for data is application/x-www-form-urlencoded, where twitch’s docs is asking for a JSON body, being the content-type application/json. For that purpose you just need to use json param instead of data. So your request code would be r = requests.post(url,headers=header, json=payload) (I dont know how you are using headers. if you are using it for the content-type specification, just avoid that and use the json param).

Finally, a third point about the GET request you are doing. According the docs (the link posted by dist before) , that request just have after and first params. Are not the same as the POST request.

Good luck xD