63 lines
1.5 KiB
JavaScript
63 lines
1.5 KiB
JavaScript
const tmi = require('tmi.js');
|
|
var amqp = require('amqplib/callback_api');
|
|
|
|
twitch_token = process.env.TWITCH_ACCESSTOKEN
|
|
twitch_username = process.env.TWITCH_USERNAME
|
|
twitch_channel = process.env.TWITCH_CHANNEL
|
|
|
|
rabbitmq_user = process.env.RABBITMQ_DEFAULT_USER
|
|
rabbitmq_pass = process.env.RABBITMQ_DEFAULT_PASS
|
|
rabbitmq_host = process.env.RABBITMQ_HOST
|
|
rabbitmq_queue = process.env.RABBITMQ_QUEUE
|
|
|
|
// Define twitch configuration options
|
|
const opts = {
|
|
identity: {
|
|
username: twitch_username,
|
|
password: twitch_token,
|
|
},
|
|
channels: [twitch_channel,]
|
|
};
|
|
console.log(opts);
|
|
|
|
|
|
amqp_url = `amqp://${rabbitmq_user}:${rabbitmq_pass}@${rabbitmq_host}`;
|
|
console.log(amqp_url);
|
|
amqp.connect(amqp_url, function(error0, connection) {
|
|
if (error0) {
|
|
throw error0;
|
|
}
|
|
connection.createChannel(function(error1, channel) {
|
|
if (error1) {
|
|
throw error1;
|
|
}
|
|
|
|
var queue = rabbitmq_queue;
|
|
|
|
channel.assertQueue(queue, {
|
|
durable: false
|
|
});
|
|
|
|
// Create a clieng with our options
|
|
const client = new tmi.client(opts);
|
|
// Connect to Twitch:
|
|
client.connect();
|
|
client.on('message', onMessageHandler);
|
|
client.on('connected', onConnectedHandler);
|
|
|
|
function onMessageHandler (target, context, msg, self) {
|
|
console.log('msg:', msg)
|
|
channel.sendToQueue(queue, Buffer.from(JSON.stringify({
|
|
"msg": msg,
|
|
"context": context,
|
|
"target": target,
|
|
})));
|
|
}
|
|
|
|
});
|
|
});
|
|
// Called every time the bot connects to Twitch chat
|
|
function onConnectedHandler (addr, port) {
|
|
console.log(`* Connected to ${addr}:${port}`);
|
|
}
|