Getting Started with Chat & Chatbots

Reviews for Extensions have resumed in May 2024 and the backlog will be completed in the coming weeks. Reviews for organizations, games, and chatbot verification continue to be temporarily paused while we revise our processes. Thank you for your patience and understanding.

Twitch provides an Internet Relay Chat (IRC) interface that lets chatbots connect to Twitch channels using a WebSocket or TCP connection. Once connected, bots can send and receive chat messages. For example, bots can provide simple reminders like get up and move or hydrate, or they can perform Twitch actions like banning a user, or they can react to user input.

This getting started example is going to react to the user entering the !dice command. When a user enters !dice in the channel’s chat room, the bot randomly generates a number from a six-sided die (1 through 6). Here’s what it looks like in chat:

Screenshot of sample chatbot


Initialize and install the package

Open a terminal window and create a folder for this example.

mkdir dice
cd dice

In the dice folder you created, initialize Node. For the entry point setting, enter bot.js.

npm init

Install the tmi.js package.

npm install tmi.js

Create the bot.js file

Using your favorite IDE, create a file named bot.js in the dice folder. Copy the following code and paste it in the bot.js file.

const tmi = require('tmi.js');

// Define configuration options
const opts = {
  identity: {
    username: '<BOT_USERNAME>',
    password: '<OAUTH_TOKEN>'
  channels: [

// Create a client with our options
const client = new tmi.client(opts);

// Register our event handlers (defined below)
client.on('message', onMessageHandler);
client.on('connected', onConnectedHandler);

// Connect to Twitch:

// Called every time a message comes in
function onMessageHandler (target, context, msg, self) {
  if (self) { return; } // Ignore messages from the bot

  // Remove whitespace from chat message
  const commandName = msg.trim();

  // If the command is known, let's execute it
  if (commandName === '!dice') {
    const num = rollDice();
    client.say(target, `You rolled a ${num}`);
    console.log(`* Executed ${commandName} command`);
  } else {
    console.log(`* Unknown command ${commandName}`);

// Function called when the "dice" command is issued
function rollDice () {
  const sides = 6;
  return Math.floor(Math.random() * sides) + 1;

// Called every time the bot connects to Twitch chat
function onConnectedHandler (addr, port) {
  console.log(`* Connected to ${addr}:${port}`);

Specify the configuration settings

Before running the code, you must replace the placeholder strings.

// Define configuration options
const opts = {
  identity: {
    username: '<BOT_USERNAME>',
    password: '<OAUTH_TOKEN>'
  channels: [

For information about nicknames and passwords, see Authenticating with the Twitch IRC server.

Running the bot

In a terminal window, enter:

node bot.js

The bot is running locally and connected to the Twitch IRC server if it prints “Connected to…” in the terminal window. Now let’s roll the die.

  1. Open a browser and navigate to\<CHANNEL NAME\>.
  2. Open the channel’s chat room.
  3. Enter !dice in the chat room’s message box.

The bot responds to the !dice command by sending a message with the number rolled (for example, You rolled a 4). The bot responds only to the !dice command.


If the chatbot receives messages, but fails to send messages when it detects the !dice command, you may need to add a verified phone number to the chatbots’ account. For more information, see Phone Verification.

Next steps