Merge pull request #4 from loblao/master

Implement gifbot
This commit is contained in:
Alex Grin 2016-09-08 12:45:51 -04:00 committed by GitHub
commit 811f571530
3 changed files with 158 additions and 2 deletions

5
app.js
View file

@ -28,7 +28,8 @@ tipbot.init(process.env.RPCUSER, process.env.RPCPASSWORD);
var hashbot = require('./bots/hashbot');
hashbot.init(slackbot, process.env.MINING_CHANNEL);
var gifbot = require('./bots/gifbot');
gifbot.init(slackbot, process.env.IMGUR_CLIENT_ID);
slackbot.on('start', function() {
slackbot.on('message', function(data) {
@ -36,6 +37,8 @@ slackbot.on('start', function() {
setTimeout(function() { sendWelcomeMessage(data.user.id); }, 2000); //Delay because of slow slack api updates which sometimes does not send msg.
}
if (data.text) {
gifbot.handle_msg(data.text, data.channel);
var command = data.text.trim().split(' ')[0];
if (command === hashbot.command) {

151
bots/gifbot.js Normal file
View file

@ -0,0 +1,151 @@
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
var slackbot;
var imgur;
var cache = {};
var cache_timeout = 3600; // 1h
var output_dir = 'files';
module.exports = {
init: init,
handle_msg: handle_msg
};
function init(_slackbot, imgur_client_id)
{
slackbot = _slackbot;
imgur = require('imgur');
imgur.setClientId(imgur_client_id);
}
function jsonrpc_call(method, params, callback) {
var xhr = new XMLHttpRequest;
xhr.addEventListener('load', function() {
var response = JSON.parse(xhr.responseText);
callback(response);
});
xhr.addEventListener('error', function (e) {
callback({error: e})
});
xhr.open('POST', 'http://localhost:5279/lbryapi', true);
xhr.send(JSON.stringify({
'jsonrpc': '2.0',
'method': method,
'params': [params],
'id': 0
}));
}
function handle_msg(msg, channel)
{
var words = msg.trim().split(' ');
words.forEach(function(word)
{
if (word.lastIndexOf('<lbry://', 0) === 0)
{
word = word.slice(8, -1); // strip <lbry:// and >
handle_url(word, channel);
}
});
}
function check_url(url, callback)
{
jsonrpc_call('resolve_name', {'name': url}, function(response)
{
if (response.error)
{
callback(response.error);
return;
}
var resolved = response.result;
if (!resolved)
{
callback(false);
return;
}
if (resolved.fee)
{
callback(false);
return;
}
var meta_version = resolved.ver ? resolved.ver : '0.0.1';
var field_name = (meta_version == '0.0.1' || meta_version == '0.0.2') ?
'content-type' : 'content_type';
var content_type = resolved[field_name];
callback(content_type == 'image/gif');
});
}
function handle_url(url, channel)
{
console.log('Detected URL', url, 'on channel', channel);
if (!cache[channel])
{
cache[channel] = {};
}
var now = new Date().getTime() / 1000;
if (cache[channel][url])
{
var elapsed = now - cache[channel][url];
if (elapsed < cache_timeout)
{
console.log(url, 'is cached for this channel, ignoring...')
return;
}
}
cache[channel][url] = now;
check_url(url, function(valid)
{
if (valid)
{
console.log('Fetching', url);
fetch_url(url, channel);
}
else
{
console.log('Ignoring', url);
}
});
}
function fetch_url(url, channel)
{
jsonrpc_call('get', {'name': url, 'download_directory': output_dir},
function(response)
{
var result = response.result;
if (!result)
{
console.warn('Failed to fetch', url);
console.warn(response);
slackbot.postMessage(channel, 'Unable to fetch URL [' + url + ']. Insufficient funds?');
return;
}
var filename = result.path;
console.log('Uploading', filename);
imgur.uploadFile(filename).then(function(uploaded)
{
var link = uploaded.data.link;
console.log(link);
var attachments = [{image_url: link, title: url}];
slackbot.postMessage(channel, null, {attachments: attachments})
}).catch(function(err)
{
console.error(err.message);
});
});
}

View file

@ -7,7 +7,9 @@
"bitcoin": "^3.0.1",
"needle": "^1.0.0",
"slackbots": "^0.5.1",
"request": "^2.74.0"
"request": "^2.74.0",
"xmlhttprequest": "1.8.0",
"imgur": "0.1.7"
},
"devDependencies": {},
"scripts": {