From ecb56e153929f043d625276d7f874db2158989a7 Mon Sep 17 00:00:00 2001 From: loblao <12ksit@gmail.com> Date: Mon, 5 Sep 2016 15:54:34 -0300 Subject: [PATCH] Implement gifbot --- app.js | 5 +- bots/gifbot.js | 151 +++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 4 +- 3 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 bots/gifbot.js diff --git a/app.js b/app.js index 53698b7..9978141 100644 --- a/app.js +++ b/app.js @@ -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) { diff --git a/bots/gifbot.js b/bots/gifbot.js new file mode 100644 index 0000000..da9db60 --- /dev/null +++ b/bots/gifbot.js @@ -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(' + 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); + }); + }); +} diff --git a/package.json b/package.json index b84d509..8d7c748 100644 --- a/package.json +++ b/package.json @@ -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": {