Yet Another Twitter Bot (YATB)

Nikolai Janakiev / @njanakiev

Basic ingredients

  • A working computer
  • Node.js
  • A confirmed twitter account
  • Some generative program

Setting up the Twitter API

Setting up the Twitter API

Setting up the Twitter API

Setting up the Twitter API

What else do we need?

  • Consumer Key
  • Consumer Secret
  • Access Token
  • Access Token Secret

Initialize Node.js

npm init

Install twit package

npm install twit --save

Let's write some code


var Twit = require('twit');
var client = new Twit({
	consumer_key:         '...',
	consumer_secret:      '...',
	access_token:         '...',
	access_token_secret:  '...'
});
client.post('statuses/update', { status: 'It\'s alive!' }, 
	function(err, data, response) {
		console.log(data)
	});
					

Post Images


var fs = require('fs');
var b64content = fs.readFileSync("image.png", {encoding: 'base64'});
					

client.post('media/upload', { media_data: b64content }, callback);
  function (err, data, response) {
    var mediaIdStr = data.media_id_string
    var meta_params = {media_id: mediaIdStr, alt_text: {text: "Image"}}
  
    client.post('media/metadata/create', meta_params, 
      function (err, data, response) {
      if (!err) { client.post('statuses/update', 
        {status: params.caption, media_ids: [mediaIdStr]}, 
          function (err, data, response) {
      	    console.log(data);
          }
        );
      } else { console.log(err); }
   });
});
					
More details at github.com/ttezel/twit

Tumblr Bots

npm install tumblr.js --save

Quite similar process to Twitter


var tumblr = require('tumblr.js');

 client = tumblr.createClient({
   consumer_key:    '...',
   consumer_secret: '...',
   token:           '...',
   token_secret:    '...'
});
blogName = 'blog';

client.createTextPost(blogName, options, callback);
					
More details at github.com/tumblr/tumblr.js

Post Images


var fs = require('fs');
var b64content = fs.readFileSync("image.png", {encoding: 'base64'});

client_tumblr.createPhotoPost(blogName,
{
	caption: "Some caption",
	tags: "some,tags,nofilter",
	data64: b64content
}, 
function(err, json) {
	if(err){
		console.log(err);
	} else {
		console.log('Image posted on Tumblr');
	}
});
					
More details at github.com/tumblr/tumblr.js

Other Platforms

  • Facebook npm fb
  • Flickr npm flickrapi
  • Behance (no posting API interface)
  • Instagram (no posting API interface)

Demo / Generate Stuff

Resources

Code : github.com/njanakiev/generic-bot