Last active
January 15, 2019 18:37
-
-
Save sgdc3/5fc6379300d26160bffc9d7087f7b386 to your computer and use it in GitHub Desktop.
TwitchNotificationBot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Include Telegraf module | |
const Telegraf = require('telegraf'); | |
const twitch = require('twitch-api-v5'); | |
twitch.clientID = ''; | |
const bot = new Telegraf(''); | |
const channelName = ''; | |
const telegramGroupId = -1; | |
const pinNotification = true; | |
var lastStream; | |
var lastPinnedMessage; | |
function update() { | |
console.log('Checking user ' + channelName + '!'); | |
// Resolve user id by name | |
twitch.users.usersByName({users: [channelName]}, (error, result) => { | |
var userId = result.users[0]._id; // We requested a single name, grab the first element | |
console.log('UserId is ' + userId + '!'); | |
// Get the channel's current stream | |
twitch.streams.channel({channelID: userId}, (error, result) => { | |
var stream = result.stream; | |
if(!stream) { | |
// Handle offline | |
console.log('Not online!'); | |
// Unpin the pinned notification, if present | |
if(lastPinnedMessage) { | |
bot.telegram.getChat(telegramGroupId).then(chat => { | |
var pinnedMessage = chat.pinned_message; | |
if(!pinnedMessage || pinnedMessage.message_id != lastPinnedMessage) { | |
lastPinnedMessage = null; | |
return; | |
} | |
console.log('Unpinnig notification!'); | |
bot.telegram.unpinChatMessage(telegramGroupId); | |
lastPinnedMessage = null; | |
}); | |
} | |
return; | |
} | |
// Handle online | |
var channel = stream.channel; | |
var title = channel.status; | |
console.log('Online: ' + title); | |
// Check if this stream was already notified | |
if(lastStream == stream._id) { | |
console.log('Already notified! Skip...'); | |
return; | |
} | |
lastStream = stream._id; | |
console.log('New stream detected! Notify!'); | |
// Send notification and pin message | |
bot.telegram.sendMessage(telegramGroupId, title + " | IN ONDA | " + channel.url).then(result => { | |
var messageId = result.message_id; | |
if(pinNotification) { | |
bot.telegram.pinChatMessage(telegramGroupId, messageId); | |
lastPinnedMessage = messageId; | |
} | |
}); | |
}); | |
}); | |
} | |
// First update | |
update(); | |
// Schedule next updates | |
setInterval(update, 60 * 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment