Follower alert in chat bot

I created a chat bot using the documentation here.

I would like to add a follower notification too like other bots do such as WizeBot. From my understanding, I need to poll the API here for a list of followers and then post the message myself? Is this the current way to do this or is there a better solution that doesn’t involve polling the API?

A better solution than polling the API would be to use webhooks

Once you’ve subscribed to a topic, Twitch will POST notifications of new followers to your callback URL.

Thank you! Is there a code sample? I a little confused how I actually subscribed to the endpoint so that I get notifications.

I tried following all the documentation I still can’t get this to work. I tried just the curl example with my endpoint but it’s not sending anything to my callback.

Have you set up whatever web server you’re using to handle the callback? You need 2 handlers, 1 for the GET request Twitch will send, which is to test your server is reachable and you have to reply the challenge that is set, and 1 handler for the POST requests which are the actual notifications for the topic you subscribed to.

Yes, I have setup the GET request but when I try to subscribe nothing is sent from Twitch.

Here is my CURL request.

curl -H ‘Client-ID: {CLIENT_ID}’
-H ‘Content-Type: application/json’
-X POST -d ‘{“hub.mode”:“subscribe”,
“hub.secret”: “s3cRe7”}’

I’m not sure what you’re actually requesting, the first link you’ve posted shows you’ve got a GET request from Twitch containing the hub.challenge, have you sent that back with a 200 status?

If you have it should show up in your list of webhook subscriptions and you’ll receive POST requests from Twitch when there is a change to the topic (keep in mind there’s caching, it’s not instant).

The reason I’m a little confused is your CURL command differs from the first URL you posted, as the CURL only has a 0 second lease, so will expire the moment it is created anyway and wouldn’t receive any response from Twitch after the hub.challenge stage of the subscription process.

I did 0 for testing. From the documentation it should send a verification but my callback is never called for the GET request.

The default (0) allows you to test the subscription-creation workflow without creating any subscriptions (since they expire immediately).

Do you have any suggestions for debugging? I’ve been spending a lot of time on this and cannot get it to work at all.

Without seeing how you’ve set up your requests/routes/handlers, my advice would be to log ‘all the things’.

eg, what’s the reply you’re getting from your initial request to Twitch, you should be getting a 202 back, if not you should be getting an error in the body of the response.

If your subscription request was successful, you should then have a GET request to your callback url. If you’re not getting anything, either it’s not reachable or your handler isn’t configured correctly. If you’re getting a request, it should contain the hub.callback which you respond with, along with a 200 status code.

At this point, assuming you did everything right and your lease is > 0 seconds, you could be able to call the webhook subscriptions endpoint and verify that the webhook has been created. If it has then you should be receiving POST requests to your callback url, if you’re not getting any then it’s likely an error in the configuration of your POST route handler.

Thank you for the reply!

Yes, I’m getting back 202 when I subscribe to follows so that works. How do I verify that the web hook has been created?

If you’re getting a 202 then at least you know the initial request works, at which point Twitch will be performing their GET request on your callback URL for you to respond to.

To check that it has been created you can use this endpoint

The sub probably won’t show up, if the sub is not confirmed by echoing the callback token

Sounds like there something weird on the execute-api url firewall, coz amazon. Or just something not quite setup right at that URL to capture

Ok, thank you! I think I have it working now. It’s sending me verification request when I subscribe but I’m not getting the follower alerts. Do these only happen when the stream is live? I tried testing by following and unfollowing with a 2nd account but I’m not receiving any notifications.

For the followers topic it doesn’t matter if the stream is live or not.

If you’re not getting notifications it means either your subscription has expired/wasn’t created (you can check this by using the endpoint I previously mentioned), or the channel you’re testing follows on differs from that of the topic, or notifications are being sent but either your route or your handler isn’t correctly handling the POST requests.

Also keep in mind that webhooks rely on the underlying API, so there is a delay due to caching and waiting for all servers to reach a consistent state. One thing you can do is send a request directly to the topic, as a normal API request, if you see that there’s a change in followers there, then a notification should be sent out shortly after.

Ok, I tried to verify the subscription but I get this error.
{"error":"Unauthorized","status":401,"message":"Must provide valid app token."}

Am I making this call correctly?
curl -H 'Client-ID: ***' -X GET ''

It requires an App Access Token, a Client-ID alone isn’t sufficient (the example is pretty bad in not showing this)

Ok, so this is not trivial? Can I use tmi.js for this?

I don’t use tmi.js due to it not being kept up to date for a long time, but getting an App Access Token isn’t particularly complicated all you need to do is make a POST request with the required params, and you’ll get back a token!

I think the reasoning behind the webhooks subscriptions endpoint requiring an App Access Token, is that a Client-ID is potentially public, so by requiring a token it means only you will be able to access it and which will help reduce the chance of your callback URL being made public, and more generally stops random people being able to see what webhook topics you’re subscribed to.

Ok! Is there something better to use beside tmi.js for a chat bot?

Also, will the follower notification be generated if I unfollow and then follow again from a 2nd account?