npm init --yes
--yes
argument runs through all of the prompts that you would otherwise have to fill out or skip. Now that we have a package.json
for our app, run this command in the same directory to install the tmi.js library for Twitch chat as well as the Twilio helper library for Node.js:npm install [email protected] [email protected]
index.js
in the same directory as your package.json
. Add the following code to it for logging all messages sent to a given channel as they come in:const tmi = require('tmi.js'); const client = new tmi.Client({ connection: { secure: true, reconnect: true }, channels: [ 'channel_name' ] }); client.connect(); client.on('message', (channel, tags, message, self) => { console.log(`${tags['display-name']}: ${message}`); });
channel_name
with the Twitch username of whoever's stream you're testing with, and run the code with the command node index.js
from your terminal. You should see messages being logged which looks something like this:export TWITCH_OAUTH_TOKEN='oauth:YOUR_TOKEN_HERE'
index.js
with the following:const tmi = require('tmi.js'); const client = new tmi.Client({ options: { debug: true }, connection: { secure: true, reconnect: true }, identity: { username: 'your_username', password: process.env.TWITCH_OAUTH_TOKEN }, channels: ['channel_name'] }); client.connect(); client.on('message', (channel, tags, message, self) => { // Ignore echoed messages. if(self) return; if(message.toLowerCase() === '!hello') { client.say(channel, `@${tags.username}, Yo what's up`); } });
your_username
with whatever Twitch account you're using to test this with, preferably one you created for your bot. I am just going to use my personal account for this, to avoid being rude and testing my bot in someone else's channel. But if you have any close friends who are streaming it can be fun to connect to their streams.index.js
again, and try saying "!hello" in the chat to get a response:sendSms.js
and add the following code to it:const accountSid = process.env.TWILIO_ACCOUNT_SID; const authToken = process.env.TWILIO_AUTH_TOKEN; const client = require('twilio')(accountSid, authToken); client.messages .create({ body: 'This is the ship that made the Kessel Run in fourteen parsecs?', from: 'YOUR_TWILIO_NUMBER, to: 'YOUR_PHONE_NUMBER' }) .then(message => console.log(message.sid));
TWILIO_ACCOUNT_SID
and TWILIO_AUTH_TOKEN
, which the Twilio Node library will use when you run your code.[+][country code][phone number including area code]
.node sendSms.js
from the directory that the file is in, and you should receive a text message!index.js
with the following code in it:const tmi = require('tmi.js'); const twilioClient = require('twilio')(); // Phone numbers of people who signed up to receive SMS messages from your bot const subscribedNumbers = ['YOUR_PHONE_NUMBER']; const channelName = 'CHANNEL_NAME'; const tmiClient = new tmi.Client({ options: { debug: true }, connection: { secure: true, reconnect: true }, identity: { username: 'YOUR_BOT_USERNAME', password: process.env.TWITCH_OAUTH_TOKEN }, channels: [channelName] }); tmiClient.connect(); tmiClient.on('message', (channel, tags, message, self) => { if(self || !message.startsWith('!')) { return; } const args = message.slice(1).split(' '); const command = args.shift().toLowerCase(); if(command === 'hello') { tmiClient.say(channel, `@${tags.username}, Yo what's up`); } else if(command === 'notify') { subscribedNumbers.forEach(number => { if(tags.username === channelName) { twilioClient.messages.create({ body: `@${tags.username}: ${args.join(' ')}`, from: 'YOUR_TWILIO_NUMBER', to: number }).then(message => console.log(message.sid)); } }); } });
index.js
and then go to your Twitch channel, type in a message like "!notify Hey everybody we are starting a new run!". If you've replaced all of the relevant variables in the code, you should receive a text message with whatever you typed in your Twitch chat.