Getting the number of subscribers in Python

Sorry if this has been asked before, if I’m asking in the wrong place, or if the solution is easy. First time using the forums, and new to building bots for Twitch.

I’m trying to code a bot in Python (since that’s the programming language I know) for a community I am involved in. It works as a chat bot, but I also want it to be able to get information like the number of subscribers to that community’s channel. Assuming I get the required OAuth token for the channel, how would I be able to get just the number of subscribers, or find a way to get all the subscribers as a list?

Also, what is the best way to get and store these OAuth tokens that my bot would need to access this privileged information? Should it be in the same file as the python code, or should I put it into a .txt or .json file?

Personally I do a fetch weekly (or daily) against this API ^^ and then use Webhooks to keep up to date with changes.

And I store the list of subscribers in a database or redis (depending on the use case)

And I count my internal list for count purposes

What ever works for you. I use a mix of JSON file, redis, and structured database depending on the use case.

This is something I did not understand when making the bot (again, completely new at this). I have it able to do stuff upon people subscribing, as well as respond to commands. But setting up and keeping up to date with a webhook, that’s something I can’t seem to get my head around with setting up.

There are a few ways to get subscriber data from Twitch

  • Read chat for the presence of the subscriber badge, that’ll tell you if a person chatting is a sub or not
  • Read chat for the (re)subscriber events from USERNOTICE
  • Read PubSub for Subscription events
  • Poll the subscribers API every few minutes to update your list
  • Receive data via Webhooks

With Webhooks you will need a Web Server to receive that data, so it might not work for your use case.

I keep most of my subscriber logic out of the bot, but use badges to check if a user can run a sub only command or not. (saves a database or API round trip)

Awesome. I think what is best is for me is to poll the Subscribers API every so often, since the current bot can share the number of subscribers for the next sub goal (they currently have Nightbot in conjunction of the bot I’m coding, and one of the goals is to remove Nightbot)

While I am unsure as to how to make it poll the subs API every few minutes, I could always look for that online anyway. Regardless, thank you for the help!

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