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

问题描述 投票:1回答:1

我想要做的是使用node.js的twet带文本的图片(在本例中为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);

} });

javascript node.js twitter
1个回答
0
投票

10个月后...... ;-)其实我并不是100%确定你想要用数字(1637458和2637458)和P1,L1等达到什么目的,但这段代码基本上来自twits github page

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

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

还请检查How to create a Minimal, Complete, and Verifiable example,这通常有助于获得更多更好的答案。

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);
© www.soinside.com 2019 - 2024. All rights reserved.