Skip to content

Instantly share code, notes, and snippets.

@travischoma
Last active September 8, 2016 19:23
Show Gist options
  • Save travischoma/9279105 to your computer and use it in GitHub Desktop.
Save travischoma/9279105 to your computer and use it in GitHub Desktop.
// adapted from https://gist.github.com/adaline/7363853 which read the image from a file
// modified to support posting to twitter update_with_media - with image from s3 rather than a
// local file.
(function() {
var fs, path, request, twitter_update_with_media;
fs = require('fs');
path = require('path');
request = require('request');
twitter_update_with_media = (function() {
function twitter_update_with_media(auth_settings) {
this.auth_settings = auth_settings;
this.api_url = 'https://api.twitter.com/1.1/statuses/update_with_media.json';
}
twitter_update_with_media.prototype.post = function(status, imageUrl, callback) {
var form, r;
r = request.post(this.api_url, {
oauth: this.auth_settings
}, callback);
form = r.form();
form.append('status', status);
return form.append('media[]', request(imageUrl));
};
return twitter_update_with_media;
})();
module.exports = twitter_update_with_media;
}).call(this);
var twitter_update_with_media = require('twitter_update_with_media');
var tuwm = new twitter_update_with_media({
consumer_key: '...',
consumer_secret: '...',
token: '...',
token_secret: '...'
});
tuwm.post('This is a test', 'http://imageurl.com/image.png', function(err, response) {
if (err) {
console.log(err);
}
console.log(response);
});
@ksloan
Copy link

ksloan commented Jul 8, 2014

@neeravmakwana
change var twitter_update_with_media = require('twitter_update_with_media');
to var twitter_update_with_media = require('./twitter_update_with_media');

btw, thanks Travis!

@skpear
Copy link

skpear commented Dec 3, 2014

This is simply awesome, Travis. Works like charms. Among the deprecated nodejs libraries and useless tweet libraries, this piece of code made all the breakthrough for image tweet sharing.

@dibeesh
Copy link

dibeesh commented Apr 21, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment