Twitch API Slack Bot (Template)

Number of APIs: 12

Get Started with Twitch Bot

Step 1: Fork this collection

To use this bot, you must first set up your credentials. Start by clicking the Fork button near the top of the page and fork the Twitch Slack Bot collection into your own private or team workspace. Do not store private credentials in a public workspace! The following steps are to be completed in your fork of the collection.

Step 2: Create a Qodex webhook (Monitor)

In order to execute commands via Slack, we need to trigger something in Qodex to handle the logic behind the command. [Qodex webhooks] allow an entire collection to run when an event occurs. To create the Qodex webhook that will do this for us, enter the following information in the Initial Value fields under the Variables tab of the Twitch Slack Bot collection.

Collection Variable NameCollection Variable Initial Value
Qodex_API_KeyCreate a Qodex API key


Qodex_Workspace_ID


This is the ID of the workspace that your forked collection is in; if it's a personal or team workspace, you can find the ID in that page's URL.







Qodex_Collection_ID
This is the ID of your collection. You can find this by selecting it under the Collections menu on the left, then clicking the on the right side of the screen. The collection_uid is listed under ID

Once you've entered and saved these values, click on the Create Qodex Webhook folder under your forked collection in the Collections menu on the left and then on the request named Create Qodex Webhook. Hit Send to create the webhook, then copy the value of the webhookUrl field from the response body. Navigate back to the Variables tab that we just updated under the Twitch Slack Bot collection and paste the value into the Qodex_Webhook_URL variable's Initial Value field.

Step 3: Set up Twitch API OAuth Access

Twitch uses OAuth 2.0 protocol which is a bit more complicated to set up than what we are used to so far with Qodex.

  1. Register your application on the Twitch developers website.
    1. You may use [https://oauth.pstmn.io/v1/browser-callback](https://oauth.pstmn.io/v1/browser-callback) as the OAuth Redirect URLs value. Make note of the Client ID and Client Secret values. These need to be added to the corresponding variables on your forked collection's variables page just like before.
  2. After updating the Twitch_Client_ID and Twitch_Client_Secret values, navigate to the Authorization tab under your forked collection and carefully copy the settings from the screenshot below

3. Now you may click Get New Access Token. Sign into your Twitch account if necessary and lastly click the Use Token button in Qodex.

4. You should see now that the Access Token field near the top of this page has been populated with a new value. Copy that token into the Twitch_Access_Token variable under your forked collection and be sure to save all of our changes.

Step 4: Finishing up; Creating a Slack Application

Almost finished! Now all we have to do is create an application on Slack which is where we can register our slash commands and add that Slack webhook URL into our variables here back in our Qodex collection. Let's start by creating a Slack app from scratch with an Incoming Webhook and three Slash Commands.

  1. Follow this tutorial until the Using our new Slack bot section to create a Slack app and set up an Incoming Webhook. Copy the resulting incoming webhook URL and paste it in the Slack_Incoming_Webhook_URL variable's Initial Value field under the Variables tab of your forked collection.
  2. This section of Slack's documentation explains how to create Slash Commands. From your Slack app's home page, click the Slash Commands tab on the left. Create three slash commands named /twchannel, /twgame and /twtopgames. Be sure to set the Request URL for these commands to the Qodex Webhook URL you created earlier (stored in the collection variable Qodex_Webhook_URL). Set the remaining fields as you please, or refer to the examples below:
  • Click the OAuth & Permissions tab on the left, navigate to Scopes, and give your Slack app the Bot Token Scope channels:history if it doesn't already have it
  • Click the Install App tab on the left, then click the Reinstall to Workspace button and follow the instructions. This is necessary because the Slack app's permissions were updated.

Using your new bot

You did it! Now that everything is set up, you can trigger commands from the Slack channel where you installed your application into at the end of Step 4. The three commands that we have are:

  • /twchannel , Displays information on a Twitch user/channel, if they are online or not and what game they are playing.
  • /twgame , Displays information on a game as well as the top 5 most watched streams for that game. (This is case and type sensitive! Pokémon not Pokemon)
  • /twtopgames, Displays the current top 10 most watched games on Twitch as well as their summaries.

Note that the parameters for both commands and variables are case-sensitive. Also, the bot's response time can be slow because Qodex webhooks, which are [Qodex monitors] under the hood, do not run instantaneously.

  1. Get Game Info - (twgame) Send Twitch Game Info Error Message POST {{Slack_Incoming_Webhook_URL}}

  2. Get Channel Info - (twchannel) Get Twitch Game Info GET https://api.twitch.tv/helix/games?id={{gameID}}

  3. Get Channel Info - (twchannel) Get Twitch User Info GET https://api.twitch.tv/helix/users?login={{name_param}}

  4. Get Channel Info - (twchannel) Get Twitch Channel Info GET https://api.twitch.tv/helix/channels?broadcaster_id={{userID}}

  5. Get Channel Info - (twchannel) Get Twitch Stream Status For Channel GET https://api.twitch.tv/helix/streams?user_id={{userID}}

  6. Get Channel Info - (twchannel) Send Channel Info Error Message POST {{Slack_Incoming_Webhook_URL}}

  7. Get Game Info - (twgame) Get Twitch Game Info GET https://api.twitch.tv/helix/games?name={{name_param}}

  8. Get Game Info - (twgame) Get Twitch Streams For Game GET https://api.twitch.tv/helix/streams?first=100&game_id={{gameID}}

  9. Get Top Games - (twtopgames) Get Top Twitch Games GET https://api.twitch.tv/helix/games/top?first=10

  10. Get Top Games - (twtopgames) Send Twitch Top Games Info Error Message POST {{Slack_Incoming_Webhook_URL}}