Getting Started
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.
1pnpm add @twitchfy/chatbot
1npm i @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.
1import { ChatBot, EventSubConnection } from "@twitchfy/chatbot";2import { RedisAdapter, WebhookConnection } from "@twitchfy/eventsub";3import { TokenAdapter } from "@twitchfy/helix";4import express from "express";5
6const appToken = new TokenAdapter({7 type: "app",8 token: "myAppToken",9});10
11//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.12
13const userToken = new TokenAdapter({14 type: "code",15 token: "myUserToken",16 refreshToken: "myRefreshToken",17});18
19const chatbot = new ChatBot({20 clientId: 'your_client_id',21 clientSecret: 'your_client_secret'22 userToken,23 connectionType: EventSubConnection.Webhook,24 eventsub: {25 secret: "itsasecretshhhh",26 appToken,27 server: express(),28 baseURL: "https://eventsub-subscriptions.com",29 storage: { adapter: new RedisAdapter<WebhookConnection>() },30 startServer: true, //the package will start the server by default. Otherwise you will have to start it.31 },32 paths: {33 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.34 commands: "commands" //the directory where your commands will be located (from output directory).35 },36});
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.
1import { ChatBot, EventSubConnection } from "@twitchfy/chatbot";2import { RedisAdapter, WebSocketConnection } from "@twitchfy/eventsub";3import { TokenAdapter } from "@twitchfy/helix";4
5const userToken = new TokenAdapter({6 type: "code",7 token: "myUserToken",8 refreshToken: "myRefreshToken",9});10
11const chatbot = new ChatBot({12 clientId: 'your_client_id',13 clientSecret: 'your_client_secret'14 userToken,15 connectionType: EventSubConnection.WebSocket,16 eventsub: {17 storage: { adapter: new RedisAdapter<WebSocketConnection>() },18 },19 paths: {20 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.21 commands: "commands" //the directory where your commands will be located (from output directory).22 },23});
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.
1import { ChatBot, EventSubConnection } from "@twitchfy/chatbot";2import { RedisAdapter, WebhookConnection } from "@twitchfy/eventsub";3import { TokenAdapter } from "@twitchfy/helix";4import express from "express";5
6const appToken = new TokenAdapter({7 type: "app",8 token: "myAppToken",9});10
11const userToken = new TokenAdapter({12 type: "code",13 token: "myUserToken",14 refreshToken: "myRefreshToken",15});16
17const chatbot = new ChatBot({18 clientId: 'your_client_id',19 clientSecret: 'your_client_secret'20 userToken,21 connectionType: EventSubConnection.Webhook,22 eventsub: {23 secret: "itsasecretshhhh",24 appToken,25 server: express(),26 baseURL: "https://eventsub-subscriptions.com",27 storage: { adapter: new RedisAdapter<WebhookConnection>() },28 startServer: true,29 paths: {30 output: "dist",31 commands: 'commands'32 },33});34
35chatbot.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.
1import { ChatBot, EventSubConnection } from "@twitchfy/chatbot";2import { RedisAdapter, WebSocketConnection } from "@twitchfy/eventsub";3import { TokenAdapter } from "@twitchfy/helix";4
5const userToken = new TokenAdapter({6 type: "code",7 token: "myUserToken",8 refreshToken: "myRefreshToken",9});10
11const chatbot = new ChatBot({12 clientId: 'your_client_id',13 clientSecret: 'your_client_secret'14 userToken,15 connectionType: EventSubConnection.WebSocket,16 eventsub: {17 storage: { adapter: new RedisAdapter<WebSocketConnection>() },18 },19 paths: {20 output: "dist",21 commands: 'commands'22 },23});24
25chatbot.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.
1import { ChatBot, EventSubConnection } from "@twitchfy/chatbot";2import { RedisAdapter, WebSocketConnection } from "@twitchfy/eventsub";3import { TokenAdapter } from "@twitchfy/helix";4
5const userToken = new TokenAdapter({6 type: "code",7 token: "myUserToken",8 refreshToken: "myRefreshToken",9});10
11const chatbot = new ChatBot({12 clientId: 'your_client_id',13 clientSecret: 'your_client_secret'14 userToken,15 connectionType: EventSubConnection.WebSocket,16 eventsub: {17 storage: { adapter: new RedisAdapter<WebSocketConnection>() },18 },19 paths: {20 output: "dist",21 commands: 'commands'22 },23});24
25chatbot.start().then(() => {26
27 console.log('ChatBot fully operational')28
29 chatbot.channels.join('channelId').then(() => console.log('Joined'))30
31});```32
33:::note34
35If 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.36
37:::38
39:::note40To 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.41:::