我如何用node.js的twit发布带有文本的图像

时间:2017-05-09 21:42:34

标签: javascript node.js twitter

我想要做的是用node.js推文带文字的图片(在这种情况下是1637458或2637458),我该怎么做?

如果有人愿意提供帮助,谢谢

我的代码:

var Twit = require('twit')

var fs = require('fs'),
path = require('path'),
Twit = require('twit'),
config = require(path.join(__dirname, 'config.js'));

var T = new Twit(config);
function random_from_array(images){
return images[Math.floor(Math.random() * images.length)];
}
var P = ['1','2'];
var P1 = P[Math.floor(Math.random() * P.length)];
var L = ['3'];
var L1 = L[Math.floor(Math.random() * L.length)];
var P3 = ['4'];
var P2 = P3[Math.floor(Math.random() * P3.length)];
var E = ['5'];
var E1 = E[Math.floor(Math.random() * E.length)];
frase = P1 + ' 6 ' + L1 + ' 7 '+ P2 + E1 + '8'
function upload_random_image(images){
console.log('Opening an image...');
var image_path = path.join(__dirname, '/images/' + 
random_from_array(images)),
  b64content = fs.readFileSync(image_path, { encoding: 'base64' });

console.log('Uploading an image...');
T.post('statuses/update', frase, function(err, data, response) {
if (err){
  console.log('ERROR:');
  console.log(err);
}
else{
  console.log('Image uploaded!');
  console.log('Now tweeting it...');
  }});
  T.post('media/upload', { media_data: b64content }, function (err, data, 
  response) {
if (err){
  console.log('ERROR:');
  console.log(err);
}
else{
  console.log('Image uploaded!');
  console.log('Now tweeting it...');

  T.post('statuses/update', {
    media_ids: new Array(data.media_id_string)
  },
    function(err, data, response) {
      if (err){
        console.log('ERROR:');
        console.log(err);
      }
      else{
        console.log('Posted an image!');
      }
    }
  );
}
});
 }
  fs.readdir(__dirname + '/images', function(err, files) {
    if (err){
    console.log(err);
  }
   else{
var images = [];
files.forEach(function(f) {
  images.push(f);
});

setInterval(function(){
  upload_random_image(images);
}, 1000*60*5);

} });

1 个答案:

答案 0 :(得分:0)

10个月后...... ;-)实际上我并不是100%确定你想要用数字(1637458和2637458)以及P1,L1等来实现什么,但这段代码基本上取自{{3} }。

我添加了一些基本功能:从图像文件夹中随机选择图像。这可以改进,目前图像的数量需要与变量number中定义的相同。和推文的myMessage变量。

所有这些都被放入一个每5分钟调用一次的函数中。请检查代码中的注释,以了解图像和状态的组合方式。通常,您首先上传图像,如果成功,图像将附加到推文并发布。

请同时查看twits github page,这通常有助于获得更多更好的答案。

var Twit = require('twit');
var config = require('./config');
const fs = require('fs');

T = new Twit(config);

var number = 10; // Number of images, exchange with your number of images
var waitTime = 5 * 60 * 1000 // wait 5 minutes or 300000 ms

function postRandomImage(){

  var myMessage = '1637458'; // your message

  // access and assign a random image in the images folder
  var b64content = fs.readFileSync('./images/' +  Math.floor((Math.random() * number) + 1) + '.jpg', { encoding: 'base64' })

  // first we must post the media to Twitter then the alt text
  T.post('media/upload', { media_data: b64content }, function (err, data, response) {

    var mediaIdStr = data.media_id_string;
    var altText = "Here can go your optional images alt text.";
    var meta_params = { media_id: mediaIdStr, alt_text: { text: altText } }

    T.post('media/metadata/create', meta_params, function (err, data, response) {
      if (!err) {
        // now we can reference the media and post a tweet (media will attach to the tweet)
        var params = { status: myMessage, media_ids: [mediaIdStr] }

        T.post('statuses/update', params, function (err, data , response) {
          // check the response in the console
          console.log(data)
        })
      }
    })
  })
}
// first post without delay
postRandomImage();
// timer calls the function every 5 minutes
setInterval(postRandomImage, waitTime);