(nodejs)如何使用Twit库将GIF图像发布到twitter?

时间:2017-01-28 08:03:46

标签: node.js twitter bots

我正在尝试构建一个基于硬编码类别(目前)将随机GIF发布到Twitter的小型机器人。

我正在使用Twit库使用Twitter API向Twitter发帖。如何将GIF发布到Twitter?

以下是代码:

var twit = require('twit');
var config = require('./config');
var request = require('request');
var fs = require('fs');
var path = require('path');
const GIPHY_API_KEY = 'API-KEY'; 
const GIPHY_API_URL = 'http://api.giphy.com/v1/gifs/random? api_key='+GIPHY_API_KEY+'&tag=study';
var T = new twit(config);
getAndPostGifImg();    
function getAndPostGifImg() { 
request(GIPHY_API_URL,function (error,response,body) {
    var resp = JSON.parse(body);
    var img_url = resp.data.image_url;
    console.log(img_url);
   // post the image to twitter
    postImg(img_url);
   });
 function postImg(img_url) {
      request(img_url).pipe(fs.createWriteStream('images/imgpost.gif'));
     var filename = path.join(__dirname,'/images/','imgpost.gif');
     var params = { encoding: 'base64' };
     var img = fs.readFileSync(filename,params);
    T.post('media/upload', { media_data: img }, onUpload);

 function onUpload(err,data,response) {
     var id = data.media_id_string; console.log(id);

     // post a tweet /hashtag along with image 
     var tweet = { status: 'random Study Tweet #giphyBotTweets', media_ids: [id] };
     T.post('statuses/update',tweet, tweeted);
     }
  function tweeted(err,data,response){
      if(err)
     { 
        var errors = data.errors;
        var i = 0;
         for(i = 0 ; i < errors.length; i++) 
            console.log("Error Message(s) : "+errors[i].message);
       }
       else 
       { console.log(data.text); }
      }
   }  
}

提前致谢。

1 个答案:

答案 0 :(得分:0)

T.post(
          "media/upload",
          {
            media_data: dataImage,
            command: "APPEND",
            media_id: data.media_id_string,
            segment_index: 0
          },
          function(err, data2, response2) {
            T.post(
              "media/upload",
              { command: "FINALIZE", media_id: data.media_id_string },
              function(err3, data3, response3) {
                let tweet = {
                  status: post.caption,
                  media_ids: [id]
                };
                T.post("statuses/update", tweet, function(
                  err,
                  data4,
                  response
                ) {
                  if (err) console.log("Something Went wrong", err.message);
                  if (data4.errors) {
                    console.log(
                      "ERROR On Success : ",
                      data4.errors[0].message
                    );
                  } else {
                    let update = {status: 2, TwitpublishId: data.media_id, updatedAt: Date.now() },
                      options = { new: true };
                    Post.findByIdAndUpdate(post._id, update, options, (error, result) => {
                      if (error) return;
                      console.log('result video========twit',result);
                      console.log("Posted on Twitter : https://twitter.com");
                    });
                  }
                });
              }
            );
          }
        );
      }
    );
  }
}