简体   繁体   English

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

[英]How do i tweet an image with text with node.js' twit

what i want to do is tweet with node.js's twit an image with text(in this case 1637458 or 2637458), how do i do that? 我想要做的是使用node.js的twet带文本的图片(在本例中为1637458或2637458),我该怎么做?

If anyone is willing to help, thank you 如果有人愿意提供帮助,谢谢

My code: 我的代码:

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);

} }); }};

10 month later... ;-) Actually I am not 100% sure what you want to achieve with the numbers (1637458 and 2637458) and the P1, L1 etc. but this code is basically taken from twits github page . 10个月后... ;-)实际上我并不是100%确定你想要用数字(1637458和2637458)以及P1,L1等实现什么,但这段代码基本上取自twits github页面

I added some basic functionality: Random pick images from the image folder. 我添加了一些基本功能:从图像文件夹中随机选择图像。 This can be refined, currently the number of images needs to be the same as defined in the variable number . 这可以改进,目前图像的数量需要与变量number定义的相同。 And a myMessage variable for your tweet. 和你的推文的myMessage变量。

All of this is put into a function which is called every 5 Minutes. 所有这些都被放入一个每5分钟调用一次的函数中。 Please check the comments in the code to see how the image and status are combined. 请检查代码中的注释,以了解图像和状态的组合方式。 In general you first upload the image and if this was successful, the image is attached to the tweet and posted. 通常,您首先上传图像,如果成功,图像将附加到推文并发布。

Please also check How to create a Minimal, Complete, and Verifiable example which often helps to get more and better answers. 另请查看如何创建最小,完整且可验证的示例 ,这通常有助于获得更多更好的答案。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM