This tutorial gets you up and running with a simple chat bot for Twitch channel.
Who's this tutorial for?
Beginners to coding and experienced coders new to Python.
Contents
We'll start out by setting up the accounts, getting the secrets, and installing the softywares. Then we'll setup and code the bot. By the end of this tutorial you should be ready to start adding your own custom commands.
BUT FIRST... we need to make sure we have our credentials in order. 👏
Papers, please!
📢 Glory to Arstotzka!
- Make an account on Twitch (for your bot) or use the one you stream with. Make it something cool like
RealStreamer69
😎 - Request an oauth code. You'll need to login and give the app permissions to generate it for you.
- Register your app with Twitch dev and request a client-id (so you can interface with Twitch's API)
Keep the oauth and client id somewhere handy but not public. You'll need them later to configure the bot.
💡 PROTIP™ -- Keep it secret. Keep it safe.
Install all the things! 🧹
- Install Python 3.6 or 3.7 -- Windows // Linux // OS X
- Install PIPENV. In the console, run ⇒
pip install pipenv
Create a cozy home for the bot to live in
Virtual environments require a couple extra steps to set up but make developing Python apps a breeze. For this tutorial, we'll use PIPENV which marries pip and venv into a single package.
- In the console, navigate to your working directory
- Run ⇒
pipenv --python 3.6
orpipenv --python 3.7
- This is going to create
pipfile
andpiplock
. They hold venv info like Python version and libraries you install.
- This is going to create
- Then run ⇒
pipenv install twitchio
Configuring and authorizing the bot
Create 2 files in your working directory. One called bot.py
and another called .env
(no file name, just the extension - it's weird, I know).
/.env
Your secrets will live inside the .env
file. Add the oauth token and client-id from above after the =
in the file. Fill in the other vars as well.
# .env
TMI_TOKEN=oauth:
CLIENT_ID=
BOT_NICK=
BOT_PREFIX=!
CHANNEL=
/bot.py
Inside bot.py
, import the libraries we'll need and create the bot obj that we'll start in the next step.
# bot.py
import os # for importing env vars for the bot to use
from twitchio.ext import commands
bot = commands.Bot(
# set up the bot
irc_token=os.environ['TMI_TOKEN'],
client_id=os.environ['CLIENT_ID'],
nick=os.environ['BOT_NICK'],
prefix=os.environ['BOT_PREFIX'],
initial_channels=[os.environ['CHANNEL']]
)
💡 PROTIP™ -- When we run
bot.py
using PIPENV, it first loads the variables from the.env
file into the virtual environment and then runsbot.py
. So, inside this venv, we have acess (on an instance-basis) to these variables. We're going to use python'sos
module to import them, just like we would import environment variables on our native environment.
At the bottom of the file, we need to make sure the bot runs when we call bot.py
directly using if __name__ == "__main__":
# bot.py
if __name__ == "__main__":
bot.run()
WAKE BOT UP (wake bot up inside!)
Let's test the bot and make sure it can connect to Twitch.
- In the console, run ⇒
pipenv run python bot.py
If it worked, you shouldn't get any errors - that means the environment variables loaded correctly and your bot successfully connected to Twitch!
If you got errors, check out the next section before moving on.
Error: Can't wake up. [save me]
A wild Request to join the channel has timed out. Make sure the channel exists.
appears. You evade with..
Make sure you have the right tokens (oauth and client-id) in the .env file and that they're in the same directory/folder as
bot.py
-
Your directory structure at this point should look like this...
working-directory/ ├─ .env ├─ bot.py ├─ Pipfile └─ Pipfile.lock
If that still doesn't fix it, comment below and we'll sort it out for ya!
Adding some functionality to the bot
Greet the chat room!
Back to bot.py
.... Below the bot object, let's create a function called event_ready
with the decorator @bot.event
. This function will run once when we start the bot. It then reports to terminal and chat that it successfully connected to Twitch.
# bot.py, below bot object
@bot.event
async def event_ready():
'Called once when the bot goes online.'
print(f"{os.environ['BOT_NICK']} is online!")
ws = bot._ws # this is only needed to send messages within event_ready
await ws.send_privmsg(os.environ['CHANNEL'], f"/me has landed!")
Go ahead and test the bot again. It should greet chat when it comes online now.
Respond to messages in chat
Next up, we're going to add a function that's run every time a message is sent in your channel. You can add all sorts of logic here later, but we'll start out with making sure the bot ignores itself.
# bot.py, below event_ready
@bot.event
async def event_message(ctx):
'Runs every time a message is sent in chat.'
# make sure the bot ignores itself and the streamer
if ctx.author.name.lower() == os.environ['BOT_NICK'].lower():
return
After that, we'll drop in a line of code that will annoyingly echo back every message sent in chat. ᴷᵃᵖᵖᵃ
# bot.py, in event_message, below the bot-ignoring stuff
await ctx.channel.send(ctx.content)
Restart the bot and check it out!
💡 PROTIP™ -- Comment out that line now cuz it's actually really annoying.
Making a chat command
Any command you make needs to follow this format when defining them..
- Decorated with
@bot.command(name='whatever')
- Be asynchronous functions with names that match the
name
variable in the decorator - Pass the message context in through the function
How the function works and what it does is all up to you. For this example, we'll create a command called !test
that says test passed!
in chat when we call it.
# bot.py, below event_message function
@bot.command(name='test')
async def test(ctx):
await ctx.send('test passed!')
Before this can work, we need to make sure that the bot knows to listen for commands coming through.
Add this just below the ignore bot code in event_message
:
#bot.py, in event_message, below the bot ignore stuffs
await bot.handle_commands(ctx)
Alright! Time to test it out. Reboot the bot and send !test
in chat!
Responding to specific messages
Tell my bot I said... "Hello."
You can respond to specific messages in your chat too, they don't have to be !commands
. Let's write some code that says hi when people say hello.
# bot.py, at the bottom of event_message
if 'hello' in ctx.content.lower():
await ctx.channel.send(f"Hi, @{ctx.author.name}!")
Go ahead and test it out! You've got the framework to start buildng your bot and adding commands.
Here's what you should have when you're done
/bot.py
import os
from twitchio.ext import commands
# set up the bot
bot = commands.Bot(
irc_token=os.environ['TMI_TOKEN'],
client_id=os.environ['CLIENT_ID'],
nick=os.environ['BOT_NICK'],
prefix=os.environ['BOT_PREFIX'],
initial_channels=[os.environ['CHANNEL']]
)
@bot.event
async def event_ready():
'Called once when the bot goes online.'
print(f"{os.environ['BOT_NICK']} is online!")
ws = bot._ws # this is only needed to send messages within event_ready
await ws.send_privmsg(os.environ['CHANNEL'], f"/me has landed!")
@bot.event
async def event_message(ctx):
'Runs every time a message is sent in chat.'
# make sure the bot ignores itself and the streamer
if ctx.author.name.lower() == os.environ['BOT_NICK'].lower():
return
await bot.handle_commands(ctx)
# await ctx.channel.send(ctx.content)
if 'hello' in ctx.content.lower():
await ctx.channel.send(f"Hi, @{ctx.author.name}!")
@bot.command(name='test')
async def test(ctx):
await ctx.send('test passed!')
if __name__ == "__main__":
bot.run()
And ofc your .env
with your secrets and a pipfile
and Piplock
.
I've uploaded the files to a github repo too, if that's your thing.
Congrats!! 🥳🎉
You've made it this far.. You know what that means? Time to celebrate by clicking this GitKraken referral link and signing up so I can get free socks (or maybe a Tesla? 😎).
Also, feel free to check out the Live Coding Stream recap where we developed this tutorial. Shoutouts to everyone in chat that collaborated!
What do you want to do next?
Questions? Comments? Ideas? Let me know in the comments below!
I'll be following up to this post soon with some tips on how to get the most out of the TwitchIO library -- more stuff that's not really well-documented and was a PITA to figure out, like how to get the author, using badges for permissions, etc.
Oldest comments (57)
I keep trying to run the bot but get the error: KeyError: 'jordo1'.
asyncio.exceptions.TimeoutError: Request to join the "jordo1" channel has timed out. Make sure the channel exists.
My channel certainly exists. I tried it with both Python 3.7 and 3.8. My code is basically the same, but I made my own class inheriting Bot instead.
I regenerated my oauth and reconfirmed my client ID.
Please help. Driving me nuts for hours.
Edit: Just realized the bot is working but this error still appears despite joining the channel. Strange. The nick isn't working though, it's just using my twitch username.
I'm getting the same error and resolution. And I can't get any of the bot.command functions to work, even with copy/pasting the code given here. If I fold it inside the event_message function it works, but not as its own thing.
this wound up working great! ty for the start :) what section of the api docs did you find these?
EDIT:nvm... i just figured it out... you need to use the Account name as the NICK not the botname... might help if you make that a bit more clear in the instructions.
I'm having the same issue as Jordan and Cai...
I think its safe to assume it is connecting to twitch API but something about the channel name variable is not working.
I created a new twitch account just for the bot and registered the application on that account.
I have tried with a different channel (a friends) to no avail.
I have checked and double checked my oAuth and Client-ID and they're both correct.
My (4) files are all in the same directory. (changing the channel name changes name in the error so pipenv is definitely working from the correct .env and other files...)
could you maybe post a "throw away" screen cap of what its supposed to look like all filled out? maybe its a simple syntax derp that i'm missing... i'm currently not using " marks or anything else after the = in the .env file.
-error return below-
PS C:\Users\thato\Documents\Python Files> pipenv run python bot.py
Loading .env environment variables…
Task exception was never retrieved
future: exception=KeyError('chreaus')>
Traceback (most recent call last):
File "C:\Users\thato.virtualenvs\Python_Files-vA_Pk7W6\lib\site-packages\twitchio\websocket.py", line 558, in join_action
cache = self._channel_cache[channel]['channel']._users
KeyError: 'chreaus'
Task exception was never retrieved
future: exception=KeyError('chreaus')>
Traceback (most recent call last):
File "C:\Users\thato.virtualenvs\Python_Files-vA_Pk7W6\lib\site-packages\twitchio\websocket.py", line 558, in join_action
cache = self._channel_cache[channel]['channel']._users
KeyError: 'chreaus'
Task exception was never retrieved
future: exception=TimeoutError('Request to join the "chreaus" channel has timed out. Make sure the channel exists.')>
Traceback (most recent call last):
File "C:\Users\thato.virtualenvs\Python_Files-vA_Pk7W6\lib\site-packages\twitchio\websocket.py", line 280, in _join_channel
await asyncio.wait_for(fut, timeout=10)
File "c:\users\thato\appdata\local\programs\python\python37\lib\asyncio\tasks.py", line 449, in wait_for
raise futures.TimeoutError()
concurrent.futures._base.TimeoutError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\thato.virtualenvs\Python_Files-vA_Pk7W6\lib\site-packages\twitchio\websocket.py", line 228, in auth_seq
await self.join_channels(channels)
File "C:\Users\thato.virtualenvs\Python_Files-vA_Pk7W6\lib\site-packages\twitchio\websocket.py", line 271, in join_channels
await asyncio.gather([self._join_channel(x) for x in channels])
File "C:\Users\thato.virtualenvs\Python_Files-vA_Pk7W6\lib\site-packages\twitchio\websocket.py", line 285, in _join_channel
f'Request to join the "{channel}" channel has timed out. Make sure the channel exists.')
concurrent.futures._base.TimeoutError: Request to join the "chreaus" channel has timed out. Make sure the channel exists.
I have a problem at the point where i have to wake the bot up with "pipenv run python bot.py". It keeps saying "Loading .env environment variables…" but nothing more. Its just stuck at this point and doesnt even give me an error.
I would really appreciate an answer since im stuck here for several hours and its driving me nuts. Thank you in advance
Same problem, eventually when trying to abort it prints this error:
C:\Users\User.virtualenvs\Chatbot_work--fd3mIVH\lib\site-packages\twitchio\websocket.py:618: RuntimeWarning: coroutine 'WebSocketCommonProtocol.close' was never awaited
self._websocket.close()
Not sure if its actually an issue to be stuck on "Loading .env environment variables…" because I can continue with the rest of the code, and my bot does go online.
This happens when my Bot_Nick and Channel are the same. Before making my Bot_Nick the same as my channel name, I was having the "channel does not exist" error. If I make my Bot_Nick my channel name, it doesn't seem to matter what the Channel input is, because the bot still goes online as my channel name.
Would love to see this fixed because it's very easy to get running otherwise!
Bot_Nick is not a nickname, it is the name of the channel.
Hi I am having the same problem and can't get around it. I am stuck on this error. Did anyone get a fix? The name/channel seems to be key but I can't get a working combination. I don't understand the user who said the nick is the channel.
If it says nothing more and your still at beginning of tutorial, then you did it right, it's in the channel, do the next step and it should put a message in chat like it says in tutorial
I'm so sorry that it took someone more than a year to find this but this is a super easy fix that was not very clear in the guide.
(the following example has a couple extra spaces to avoid formatting)
the:
if __ name __ == "__ main __":
bot.run()
must go at the bottom of your bot.py file
if there are functions after it, it will not load.
The reason "Loading .env environment variables..." will stay, is because there is nothing to print to the console afterwards. In the "event_ready" function, if you print something, then there will be output.
how to send whispers?
It says that the twitchio module isn't found. I also looked in the directory I ran the commands in, and there's no pipfile.lock
Install the TwitchIO module using pip on Windows with the command
py -m pip install twitchio
. To install on Mac/Linux, I believe simplypip install twitchio
should do the trick.Still says channel not found for me, do I put my channel name in the "channel" variable?
you need to define CHANNEL in your .env file like this:
CHANNEL="#channel_name"
Make sure you are using the TMI token and Client ID for your bot channel and not your personal Twitch account.
BOT_NICK
is the channel name of your bot andCHANNEL
is the channel name of the account whose chat you'd like the bot to join.It keeps saying "Make sure channel exists. I have tried the url type, twitch.tv/channelname, and just the channel name. I have made a new oauth, and tried everything I can. EDIT: Just looked at the other comments, and figured out that bot_nick needs to be the channel name, so what do I put at CHANNEL=?
you need to define CHANNEL in your .env file like this:
CHANNEL="#channel_name"
BOT_NICK
is the channel name of the bot account,CHANNEL
is the channel name of the channel whose chat you'd like the bot to join. Just the usernames, don't use the "twitch.tv/" prefix.Can I change the name of the bot? My friend doesent want it to be called after me!
I had to make a new Twitch account for my bot in order to change the name per the advice I found here: discuss.dev.twitch.tv/t/twitch-cha...
The bot will use the name that the OAuth token is assigned to.
Hey y'all! Like most of you I ended up here and had trouble getting the channel to connect.
What you want to do is lead the channel name with a "#" in your env file.
For instance:
CHANNEL="#space_cdt"
After this I was able to connect!
Could you be a little more specific? Doing this still leads to having it not set the correct channel name,
This is more likely an error with you TMI token or Client ID. Make sure both are generated using your bot's Twitch account and not your personal Twitch account.
To everyone with the error:
self._websocket.close()
If you scroll up there should be an earlier error saying:
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1097)
To resolve this I used this: gist.github.com/marschhuynh/31c937...
Simply copy this into another python file called install_ssl_certs.py and run it with
pipenv run python install_ssl_certs.py
After doing this I was able to connect and got the message: Ready | frenchtoastbot_
i get this error
Traceback (most recent call last):
File "B:\ChatLoyalty\ChatLoyalty.py", line 7, in
irc_token=os.environ['TMI_TOKEN'],
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\os.py", line 675, in getitem
raise KeyError(key) from None
KeyError: 'TMI_TOKEN'
what am i doing wrong?
I figured this out. What you want to do is put in what is in these quotations for each of the environments into bot.py instead of .env.
"os.environ['TMI_TOKEN']=
os.environ['CLIENT_ID']=
os.environ['BOT_NICK']=
os.environ['BOT_PREFIX']=
os.environ['CHANNEL']=
"
For some reason, the OP didn't specify how to call the .env file before it tries to run the rest of bot.py, but doing that eliminates the need for .env
I'm getting the same issue and cannot figure out where to put your solution. I've tried putting the .env info directly into bot.py as variables to eliminate the need for the .env, and I still get the KeyError despite any changes I make. Where am I putting this info to fix this problem? Thank you!
FOR THOSE HAVING "Make sure channel exists." make sure your "BOT_NICK" is the name of your bot account. Also make sure the "CHANNEL" is the name of the channel you're trying to join. For "BOT_PREFIX" I just put the same name as my bot_NICK. If this works let me know! I had spent some time on this issue because of the confusion on this discussion post.
For anybody reading,
BOT_PREFIX
should be the prefix used for your bot commands. Most often on Twitch, bot commands start with "!" which I believe is why the original author has the lineBOT_PREFIX=!
How would I daemonize this script. I'd like to run a few at a time to manage different channels.
Currently I have each run detached using screen.
What do I put in the "channel" section in the .env?
If I put my channel name it says.
asyncio.exceptions.TimeoutError: Request to join the "[my channel]" channel has timed out. Make sure the channel exists.
Help?
I apologize in advance for maybe not being as clear as someone more advanced, I do not have advanced programming knowledge. I would like to add pygame functionality to this bot. Where I'm caught up is in basically having to do with the main loop. Either my pygame stuff works one time then everything is limited to the bot or my pygame stuff is looped without the bot loading. Anyone able to provide any guidance on this matter?
Did you solve your problem? Sounds like a cool use for a bot! Since the loops are getting you caught up, maybe it would be better to take an OOP approach similar to the example used in the official docs: twitchio.readthedocs.io/en/latest/...