Skip to content

Instantly share code, notes, and snippets.

@maxkueng
Last active December 12, 2015 10:09
Show Gist options
  • Save maxkueng/4757191 to your computer and use it in GitHub Desktop.
Save maxkueng/4757191 to your computer and use it in GitHub Desktop.

Instructions

  1. Download and install Node.js and npm (comes with Node.js)
  2. Download this code
  3. Change to the directory where the source code is (with a terminal)
  4. Type npm install to install module dependencies
  5. Edit app.js and edit the "settings" section
  6. Run the app by typing node app.js
// -- settings / edit below
var DEMO_MODE = true; // set this to false to enable sending bitcoins
var BTC_AMOUNT = 0.01; // amount in BTC to send to each address
var TWITTER_TRACK_KEYWORD = '@BTCForMe';
// Twitter API credentials. Get them here: https://dev.twitter.com/apps/new
var TWITTER_CONSUMER_KEY = '**********';
var TWITTER_CONSUMER_SECRET = '**********';
var TWITTER_ACCESS_TOKEN_KEY = '**********';
var TWITTER_ACCESS_TOKEN_SECRET = '**********';
// You could also use your Bitcoin client's JSON-RPC interface instead of blockchain.info
var JSONRPC_USER = '********-****-****-****-************'; // e.g. blockchain.info wallet identifier
var JSONRPC_PASSWORD = '**********'; // e.g. blockchain.info wallet password
var JSONRPC_HOST = 'blockchain.info';
var JSONRPC_PORT = 80;
// -- end of settings
var Twitter = require('ntwitter');
var bitcoin = require('bitcoin');
var client = new bitcoin.Client({
'host' : JSONRPC_HOST,
'port' : JSONRPC_PORT,
'user' : JSONRPC_USER,
'pass' : JSONRPC_PASSWORD
});
var findBitcoinAddress = function (tweet) {
var matches = /[123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ]{25,34}/.exec(tweet);
if (matches && matches[0]) {
return matches[0];
}
return false;
};
var twit = new Twitter({
'consumer_key' : TWITTER_CONSUMER_KEY,
'consumer_secret' : TWITTER_CONSUMER_SECRET,
'access_token_key' : TWITTER_ACCESS_TOKEN_KEY,
'access_token_secret' : TWITTER_ACCESS_TOKEN_SECRET
});
twit.stream('statuses/filter', {
'track' : TWITTER_TRACK_KEYWORD
}, function (stream) {
stream.on('data', function (data) {
var bitcoinAddress = findBitcoinAddress(data.text);
if (!bitcoinAddress) { return console.error('Tweet does not contain a bitcoin address'); }
client.validateAddress(bitcoinAddress, function (err, check) {
if (err) return console.error(err);
if (check.isvalid) {
if (DEMO_MODE === false) {
client.sendToAddress(bitcoinAddress, BTC_AMOUNT, function (err, transactionId) {
if (err) return console.error(err);
console.info('Sent', BTC_AMOUNT, 'BTC to', bitcoinAddress);
console.info('Transaction:', transactionId);
});
} else {
console.info('DEMO MODE:', 'Didn\'t send', BTC_AMOUNT, 'BTC to', bitcoinAddress);
}
}
});
});
stream.on('error', function (err) {
console.error(err);
});
});
{
"name" : "BTCForMe",
"version" : "0.0.1",
"dependencies" : {
"ntwitter": "*",
"bitcoin" : "*"
},
"engines" : {
"node" : "*"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment