Twitch bot hosting, connected to chat around the clock
A chat bot is only useful while it is connected. Push the repository, put the token in the environment and deploy: tmi.js on Node.js or TwitchIO on Python, running in its own sandbox, started again if it exits and redeployed on every commit.
tmi.js, on Node.js
const tmi = require("tmi.js");
const client = new tmi.Client({
identity: { username: process.env.TWITCH_USERNAME, password: process.env.TWITCH_OAUTH },
channels: [process.env.TWITCH_CHANNEL],
});
client.on("message", (channel, tags, message, self) => {
if (self) return;
if (message === "!hello") client.say(channel, "hello " + tags.username);
});
client.connect();Put tmi.js in dependencies and "start": "node bot.js" in package.json. The OAuth token goes in an environment variable, prefixed with oauth: the way Twitch hands it to you.
TwitchIO, on Python
import os
from twitchio.ext import commands
bot = commands.Bot(
token=os.environ["TWITCH_OAUTH"],
prefix="!",
initial_channels=[os.environ["TWITCH_CHANNEL"]],
)
@bot.command(name="hello")
async def hello(ctx):
await ctx.send(f"hello {ctx.author.name}")
bot.run()twitchio in requirements.txt and the file saved as bot.py is the whole deployment. Both libraries idle in a few tens of megabytes, so the free 256 MB is roomy for a chat bot.
The ports a chat bot uses
tmi.js connects over a secure WebSocket on port 443 by default, which the free plan's outbound list allows. If your library is pointed at the plain IRC ports, 6667 or 6697, either move it to the WebSocket endpoint or put the app on a paid plan, where you choose what it may reach. The Firewall tab names anything that was refused, so a bot that connects nowhere is not a guess.
EventSub: with an address or without one
EventSub over a WebSocket needs no public address and runs on any tier: the bot dials Twitch and subscriptions arrive on that connection. EventSub over a webhook is the other way round, so Twitch needs a public https address to call. Every app can have one at https://name.fadehost.app with the certificate handled; on the free plan that address sleeps when nobody uses it, so a webhook receiver belongs on a paid app where it stays awake.
Frequently asked questions
Which library should I use?
tmi.js if the bot is Node.js, TwitchIO if it is Python. Both read chat, send messages and handle commands; TwitchIO also wraps the Helix API and EventSub.
Why did the bot stop answering after a while?
Usually the token. A Twitch access token expires, and a bot holding one it cannot refresh goes quiet without crashing. Keep the client id, client secret and refresh token in the environment and ask Twitch for a fresh access token when the bot starts; the panel restarts the process, so a refresh at startup covers most bots.
Do I need a public address?
Not for chat, and not for EventSub over a WebSocket. Only EventSub webhooks need Twitch to reach you, and that is a web address on a paid app.
Can a free bot reach the API it needs?
A free app's outbound traffic follows a fixed list: DNS, HTTP and HTTPS on the usual ports, the database ports, and some game and voice ports. Anything that calls an API over HTTPS is covered. A paid app may reach everything, or whatever list you give it, and the Firewall tab names what was refused.
Where does the token go?
In the environment variables of the app, as BOT_TOKEN or whatever name your code reads, never in the repository. The panel masks the values and injects them when the bot starts.
What happens when I push a commit?
With auto-deploy on, every push to the branch you chose redeploys the bot: install, build if there is one, start. The live console shows each step and the crash doctor explains a failed start in plain words.
What does the free plan include?
One bot or app per verified account: 256 MB of memory, a quarter of a vCPU, 3 GB of disk under /data and 1 GB of traffic a day, with deploys on push, the live console, the crash doctor and rollback. The process stays on; only a web address sleeps when nobody is using it. A free app runs in whichever region has room for it. A paid plan from $2 a month gives the bot 1 GB and a whole vCPU, lets you pick the region and drops the traffic limit.