A simple answer bot written in Go that uses Twitch's IRC service to do most of the work. This project is meant to explain to anyone else trying to make a similar client all the weird setup crap Twitch makes you do with OAUTH2.
You can not select more than 25 topicsTopics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
A simple answer bot written in Go that uses Twitch's IRC service to do most of the work. This project is meant to explain to anyone else trying to make a similar client all the weird setup crap Twitch makes you do with OAUTH2.
## How to Actually Write a Twitch Bot
Twitch's documentation on this is _extremely_ confusing. It's the classic, "We've explained every
single thing exactly but didn't explain how they all work together." In this document I'm going to
explain how you _actually_ get a Twitch bot setup. I'm using IRC but this initial setup should apply
to the WebAPI...I think.
## Overview of the Problem
Twitch uses [Oauth2](https://oauth.net/2/) to confirm that a user wants to give your bot access to
their account. This is a good thing. Imagine you have a bot that pays people channel points or
some similar thing and any user that wants to play with your bot must give them access. Then the
flow is something like this:
1. User says, "Hey, this bot is cool I want to use it."
2. User goes to a web page explaining the bot.
3. Use clicks the, "Give Access" button which is _your button on your site, not twitch.tv_.
4. This then sends the user to twitch.tv's Oauth2 receiver and Twitch confirms they actually do want
to give the bot access. This will also list the things your bot wants access to in their
account.
5. Once they click the confirmation on _twitch's Oauth2_ end point, twitch bounces them back to your
site's "oauth2 is done" URL.
6. Your bot now has access to the information you requested in their account.
That's all reasonable, and should be the same process for other features like extensions.
## Anyone Using Oauth2 for Servers Deserves to Rot in Hell
Twitch, in their infinite wisdom decided that _you_--the bot author--would use this same workflow to
__give a bot you created in their own UI access to your own bot you created in your own account__.
If that sounds dumb it is, and I have a theory about why they're doing this at the end.
6. It will then output your `ClientSecret` and `RefreshToken` for the `secret.json` file used in
this bot's configuration. __DO NOT COMMIT THIS TO GIT__.
## Refreshing Your Keys
Currently my bot doesn't auto-refresh the keys, but you should only need to send a POST to
`/oauth2/token` with your `RefreshToken` to renew it. Until then I do this manually:
1. Copy `secret_example.json` to `secret.json`.
1. Run `twitch token --refresh RefreshToken` where I replace "RefreshToken" with my `secret.json`'s
`RefreshToken` setting.
2. This outputs a __totally new key that you have to record again__. So update your `secret.json`
file and it should keep working.
Once again, this is incredibly stupid and does not add security to their system. Companies like
Paypal and Stripe only use secret keys given out by their UI and those companies deal with trillions
of real dollars. Adding this bullshit key rotation layer on top of Oauth2 does nothing but make
things complicated and inject a [notoriously](https://owasp.org/www-project-web-security-testing-guide/latest/4-Web_Application_Security_Testing/05-Authorization_Testing/05-Testing_for_OAuth_Weaknesses) [insecure](https://nvd.nist.gov/vuln/detail/CVE-2026-34457) [system](https://portswigger.net/web-security/oauth) into the mix.
## Running This Bot
Once you have your `secret.json` configured you can then edit the `config.json` to configure your
bot. Run `cp config_example.json config.json` to get your version of the file. Here's they key configurations:
`ClientNick`
: This is _your username_, not the bot's, although the bot's will work too.
`Channel`
: Your channel to join. Not sure if it will work with any channel, but you might be able to use the
`chat:read` scope.
`Commands`
: This is a list of `{"command": "Message"}` combinations. The "command" is used by the user with
!command in the chat.
`Periodic`
: This is a list of messages the bot will post into chat periodically. Just say how many seconds to