Node.js 如何使用Twit库将GIF图像发布到twitter?

Node.js 如何使用Twit库将GIF图像发布到twitter?,node.js,twitter,bots,Node.js,Twitter,Bots,我正在尝试构建一个小机器人,根据硬编码的类别(目前)向twitter发布随机GIF 我正在使用Twit库使用twitterapi向twitter发布帖子。如何将GIF发布到twitter 代码如下: var twit = require('twit'); var config = require('./config'); var request = require('request'); var fs = require('fs'); var path = require('path'); co

我正在尝试构建一个小机器人,根据硬编码的类别(目前)向twitter发布随机GIF

我正在使用
Twit
库使用twitterapi向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); }
      }
   }  
}
var twit=require('twit');
var config=require('./config');
var请求=要求(“请求”);
var fs=需要('fs');
var path=require('path');
const GIPHY_API_KEY='API-KEY';
const GIPHY_API_URL=http://api.giphy.com/v1/gifs/random? api_键='+GIPHY_api_键+'&tag=研究';
var T=新twit(配置);
getAndPostGifImg();
函数getAndPostGifImg(){
请求(GIPHY\U API\U URL、函数(错误、响应、正文){
var resp=JSON.parse(body);
var img_url=resp.data.image_url;
console.log(img_url);
//将图片发布到twitter上
postImg(img_url);
});
函数postImg(img_url){
请求(img_url).pipe(fs.createWriteStream('images/imgpost.gif');
var filename=path.join(uu dirname,/images/,'imgpost.gif');
var params={encoding:'base64'};
var img=fs.readFileSync(文件名,参数);
T.post(‘媒体/上传’,{媒体数据:img},onUpload);
函数onUpload(错误、数据、响应){
var id=data.media\u id\u string;console.log(id);
//随图像一起发布tweet/标签
var tweet={状态:'随机研究tweet#giphyBotTweets',媒体id:[id]};
T.post(‘状态/更新’、tweet、tweet);
}
功能推文(错误、数据、响应){
如果(错误)
{ 
var误差=数据误差;
var i=0;
对于(i=0;i

提前感谢。

您遇到了哪些错误?您的代码在哪里失败?代码显示为
null
,消息显示为
media type unrecogned
我还使用了
console.log
作为
media\u id
undefined
显示代码可能在
T.post()
readFileAsync()
方法失败。我查看了文档,他们没有上传GIF图像的示例。您发现了哪些错误?您的代码在哪里失败?代码显示为
null
,消息显示为
media type unrecogned
我还使用了
console.log
作为
media\u id
undefined
显示代码可能在
T.post()
readFileAsync()
方法失败。我查看了文档,他们没有上传GIF图像的例子,而这段代码可能会解决这个问题,如何以及为什么解决这个问题会真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。虽然此代码可能会解决此问题,但如何以及为什么解决此问题将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。
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");
                    });
                  }
                });
              }
            );
          }
        );
      }
    );
  }
}