lbry-tipbot/bot.js

58 lines
1.5 KiB
JavaScript
Raw Normal View History

2016-07-13 21:17:49 +02:00
var SlackBot = require('slackbots');
var needle = require('needle');
2016-07-13 22:31:24 +02:00
var SLACK_TOKEN = process.env.SLACK_TOKEN;
2016-07-13 22:32:51 +02:00
var CHANNEL = process.env.CHANNEL;
2016-07-13 22:31:24 +02:00
if (!SLACK_TOKEN) {
throw new Error('SLACK_TOKEN env var required');
}
2016-07-13 22:48:11 +02:00
if (!CHANNEL) {
throw new Error('CHANNEL env var required');
}
2016-07-13 22:31:24 +02:00
2016-07-13 21:17:49 +02:00
var bot = new SlackBot({
2016-07-13 22:31:24 +02:00
token: SLACK_TOKEN,
2016-07-13 21:17:49 +02:00
name: 'hashbot'
});
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
2016-07-13 22:31:24 +02:00
function getData() {
needle.get('https://explorer.lbry.io/api/getmininginfo', function(error, response) {
// if (!error && response.statusCode == 200) {
// console.log(response.body);
// }
var data = response.body,
hashrate = Math.round(data.networkhashps / 1000000000),
difficulty = numberWithCommas(Math.round(data.difficulty)),
block = numberWithCommas(data.blocks);
2016-07-13 22:32:51 +02:00
bot.postMessageToChannel(CHANNEL,
2016-07-13 22:31:24 +02:00
// 'Blockchain stats:\n' +
'Hashrate: ' + hashrate + ' GH/s\n' +
'Difficulty: ' + difficulty + '\n' +
'Current block: ' + block + '\n' +
'_Source: https://explorer.lbry.io_'
);
});
}
2016-07-13 21:17:49 +02:00
bot.on('start', function() {
2016-07-13 22:31:24 +02:00
// more information about additional params https://api.slack.com/methods/chat.postMessage
2016-07-13 21:17:49 +02:00
bot.on('message', function(data) {
2016-07-13 22:31:24 +02:00
if (data.text && data.text.trim() === '!hash') {
getData();
2016-07-13 21:17:49 +02:00
}
});
2016-07-13 22:31:24 +02:00
// Post every hour
setInterval(getData, 3600000);
getData();
2016-07-13 22:48:11 +02:00
});