Setting up our project
Having authorized and created your chatbot account within Twitch it’s time to build our first chatbot.
First of all we have to install @twitchfy/chatbot
package with our Node Package Manager.
pnpm add @twitchfy/chatbot
Building the chatbot
Having set up our project it’s time to start building our first chatbot.
For building the chatbot we have to create a new instance of the ChatBot class.
Here is a brief example of how to build a basic chatbot using webhooks or websocket connection.
import { ChatBot, EventSubConnection } from " @twitchfy/chatbot " ;
import { RedisAdapter, WebhookConnection } from " @twitchfy/eventsub " ;
import { TokenAdapter } from " @twitchfy/helix " ;
import express from " express " ;
const appToken = new TokenAdapter ( {
//app tokens couldn't be refreshed within Twitch but for mocking that refresh the package regenerate the app token. App tokens are only used when setting the connection type to webhooks.
const userToken = new TokenAdapter ( {
refreshToken: " myRefreshToken " ,
const chatbot = new ChatBot ( {
clientId: ' your_client_id ' ,
clientSecret: ' your_client_secret '
connectionType: EventSubConnection . Webhook ,
secret: " itsasecretshhhh " ,
baseURL: " https://eventsub-subscriptions.com " ,
storage: { adapter: new RedisAdapter < WebhookConnection > () },
startServer: true , //the package will start the server by default. Otherwise you will have to start it.
output: " dist " , //the directory where your source code will be located (from current working directory). Using TypeScript it will be the output directory defined in the ts config file.
commands: " commands " //the directory where your commands will be located (from output directory).
To build up a chatbot using Webhooks we need to start a server and bound it with express.
The secret
field is a kind of authorization against malicious attacks.
import { ChatBot, EventSubConnection } from " @twitchfy/chatbot " ;
import { RedisAdapter, WebSocketConnection } from " @twitchfy/eventsub " ;
import { TokenAdapter } from " @twitchfy/helix " ;
const userToken = new TokenAdapter ( {
refreshToken: " myRefreshToken " ,
const chatbot = new ChatBot ( {
clientId: ' your_client_id ' ,
clientSecret: ' your_client_secret '
connectionType: EventSubConnection . WebSocket ,
storage: { adapter: new RedisAdapter < WebSocketConnection > () },
output: " dist " , //the directory where your source code will be located (from current working directory). Using TypeScript it will be the output directory defined in the ts config file.
commands: " commands " //the directory where your commands will be located (from output directory).
Note
Storage
option within eventsub options is used for saving the subscriptions and reloading them everytime you start of your application.
In the example above RedisAdapter is used for storing the subscriptions within a Redis server. However you can customize your storage adapter by extending StorageAdapter class.
Additionally, there is a MongoAdapter class for storing subscriptions in MongoDB.
Start your chatbot
Once you’ve created your chatbot, the next step is to get it online. Simply call the start
method, which returns a Promise which will be resolved when the chatbot is fully operational.
import { ChatBot, EventSubConnection } from " @twitchfy/chatbot " ;
import { RedisAdapter, WebhookConnection } from " @twitchfy/eventsub " ;
import { TokenAdapter } from " @twitchfy/helix " ;
import express from " express " ;
const appToken = new TokenAdapter ( {
const userToken = new TokenAdapter ( {
refreshToken: " myRefreshToken " ,
const chatbot = new ChatBot ( {
clientId: ' your_client_id ' ,
clientSecret: ' your_client_secret '
connectionType: EventSubConnection . Webhook ,
secret: " itsasecretshhhh " ,
baseURL: " https://eventsub-subscriptions.com " ,
storage: { adapter: new RedisAdapter < WebhookConnection > () },
chatbot . start ( 3333 ) . then ( () => console . log ( ' ChatBot is fully operational ' )) //as we set the startServer option into true we have to set the port which will be listened.
import { ChatBot, EventSubConnection } from " @twitchfy/chatbot " ;
import { RedisAdapter, WebSocketConnection } from " @twitchfy/eventsub " ;
import { TokenAdapter } from " @twitchfy/helix " ;
const userToken = new TokenAdapter ( {
refreshToken: " myRefreshToken " ,
const chatbot = new ChatBot ( {
clientId: ' your_client_id ' ,
clientSecret: ' your_client_secret '
connectionType: EventSubConnection . WebSocket ,
storage: { adapter: new RedisAdapter < WebSocketConnection > () },
chatbot . start () . then ( () => console . log ( ' ChatBot fully operational ' ))
Joining Channels
Once you have set up your chatbot it’s time to join to a broadcaster channel and listen to the broadcaster chatroom messages.
To join a channel you have to call join
method in the chatbot ChannelManager with the channel’s id you want to join to.
It’s recommended to join channels after the chatbot has fully started. If not it could led to anytype of errors.
import { ChatBot, EventSubConnection } from " @twitchfy/chatbot " ;
import { RedisAdapter, WebSocketConnection } from " @twitchfy/eventsub " ;
import { TokenAdapter } from " @twitchfy/helix " ;
const userToken = new TokenAdapter ( {
refreshToken: " myRefreshToken " ,
const chatbot = new ChatBot ( {
clientId: ' your_client_id ' ,
clientSecret: ' your_client_secret '
connectionType: EventSubConnection . WebSocket ,
storage: { adapter: new RedisAdapter < WebSocketConnection > () },
chatbot . start () . then ( () => {
console . log ( ' ChatBot fully operational ' )
chatbot . channels . join ( ' channelId ' ) . then ( () => console . log ( ' Joined ' ))
If you have an storage set into your chatbot's eventsub you won't need to join to the same channels everytime you start your application.
To be able to join to channels you need either ` channel: bot ` scope from broadcaster or your chatbot to be a moderator of the channel you will join.