=================
Twitch offers an Internet Relay Chat (IRC) interface for chat functionality. Chatbots allow you to programmatically interact with a Twitch chat feed using IRC standards; the bot connects to the Twitch IRC network as a client to perform these actions.This guide presents an easy bot example to get you started.
We’ll build a simple chatbot that responds in chat when someone types !dice
. (This is known as a chatbot command.) When triggered, it randomly generates a number between 1-6. We run the fully-functioning chatbot on this Glitch, as well as show you how to run locally on our Developer Docs.
To start, you’ll need three environment variables:
| Variable | Description |
|---|---|---|---|---|
| BOT_USERNAME
| The account (username) that the chatbot uses to send chat messages. This can be your Twitch account. Alternately, many developers choose to create a second Twitch account for their bot, so it's clear from whom the messages originate. |
|CHANNEL_NAME
| The Twitch channel name where you want to run the bot. Usually this is your main Twitch account.
|OAUTH_TOKEN
|The token to authenticate your chatbot with Twitch's servers. Generate this with https://twitchapps.com/tmi/ (a Twitch community-driven wrapper around the Twitch API), while logged in to your chatbot account. The token will be an alphanumeric string.|
-
To start with this template, click the Remix button in the upper right.
-
Glitch automatically installs Node and Tmi.js for us.
-
Add the three environmental vars in our
.env
file. -
View the code in
bot.js
. -
Your chatbot is ready to run! Glitch automatically deploys & runs each version. View the status button to ensure there are no errors.
-
Try the chatbot! Interact with your channel (twitch.tv/<CHANNEL_NAME>) by trying the
!dice
command.
Note: This bot connects to the IRC network as a client and isn't designed to respond over HTTP. If you click "Show Live" you will see a simple "Hello World"
- For a thorough understanding of Twitch chatbots and IRC, read the Chatbots & IRC Guide and the rest of the Twitch IRC documentation.
- To authenticate your chatbot in a production setting, we recommend you register your app (chatbot) and use the OAuth Authorization code flow. This enables you to authenticate programmatically. To learn more, read the Apps & Authentication Guide. Read Chatbots & IRC documentation.
- Reach out to @twitchdev or the Twitch chatbot forum for help!