Javascript 通过Twit w/Node.js发布Twitter线程

Javascript 通过Twit w/Node.js发布Twitter线程,javascript,node.js,npm,twitter,Javascript,Node.js,Npm,Twitter,我正在使用Node和npmtwit模块向Twitter发布推文。它在起作用……有点 我能够成功发布一条推文,没有任何问题。然而,当我试图将一串推文放在一起(就像推特上的一个线程)时,推文无法正确显示。这是我的代码的相关部分 基本上,我可以毫无问题地发布初始tweet(函数中的“first”参数)。然后,我获取该tweet的唯一ID(同样,没有问题),并尝试循环通过字符串数组(“后续”参数)并发布对该tweet的回复。代码如下: const tweet = (first, subsequent)

我正在使用Node和npmtwit模块向Twitter发布推文。它在起作用……有点

我能够成功发布一条推文,没有任何问题。然而,当我试图将一串推文放在一起(就像推特上的一个线程)时,推文无法正确显示。这是我的代码的相关部分

基本上,我可以毫无问题地发布初始tweet(函数中的“first”参数)。然后,我获取该tweet的唯一ID(同样,没有问题),并尝试循环通过字符串数组(“后续”参数)并发布对该tweet的回复。代码如下:

const tweet = (first, subsequent) => { 
  bot.post('statuses/update', { status: `${first}` }, (err,data, response) => {
    if (err) {
      console.log(err);
    } else {
      console.log(`${data.text} tweeted!`);

   /// Find the tweet and then subtweet it!
      var options = { screen_name: 'DoDContractBot', count: 1 };
      bot.get('statuses/user_timeline', options , function(err, data) {
        if (err) throw err;

        let tweetId = data[0].id_str;
        for(let i = 1; i < subsequent.length; i++){
          let status = subsequent[i];
          bot.post('statuses/update', { status, in_reply_to_status_id: tweetId }, (err, data, response) => {
            if(err) throw err;
            console.log(`${subsequent[i]} was posted!`);
          })
        }

      });
    }
  });
};
const tweet=(第一个,后续)=>{
bot.post('statuses/update',{status:`${first}`},(err、data、response)=>{
如果(错误){
控制台日志(err);
}否则{
log(`${data.text}tweeted!`);
///找到推文,然后将其分为子推文!
var options={screen_name:'DoDContractBot',计数:1};
bot.get('status/user_timeline',选项,函数(err,data){
如果(错误)抛出错误;
让tweetId=data[0].id_str;
for(设i=1;i{
如果(错误)抛出错误;
log(`${后续[i]}已发布!`);
})
}
});
}
});
};
不管出于什么原因,推特上的推文不会出现在同一个线程下。下面是它的样子:(这里应该还有两个‘子推文’。这些推文是“发布”的,但与原始推文是分开的):


还有其他人在Twitter API上有类似的问题吗?你知道如何通过Twit更优雅地完成一个线程吗?谢谢

我知道该怎么办了

正如Andy Piper提到的,我需要响应特定的tweet id,而不是线程中的原始tweet id。因此,我重构了代码,将twit模块包装在一个promise包装器中,并使用了一个带有async/await的for循环。像这样:

const Twit = require('twit');
const config = require('./config');
const util = require("util");
const bot = new Twit(config);

// Wrapping my code in a promise wrapper...
let post_promise = require('util').promisify( // Wrap post function w/ promisify to allow for sequential posting.
  (options, data, cb) => bot.post(
    options,
    data,
    (err, ...results) => cb(err, results)
  )
);

// Async/await for the results of the previous post, get the id...
const tweet_crafter = async (array, id) => { 
  for(let i = 1; i < array.length; i++){
    let content = await post_promise('statuses/update', { status: array[i], in_reply_to_status_id: id });
    id = content[0].id_str;
  };
};

const tweet = (first, subsequent) => { 
  post_promise('statuses/update', { status: `${first}` })
    .then((top_tweet) => {
        console.log(`${top_tweet[0].text} tweeted!`);
        let starting_id = top_tweet[0].id_str; // Get top-line tweet ID...
        tweet_crafter(subsequent, starting_id);
    })
    .catch(err => console.log(err));
};

module.exports = tweet;
consttwit=require('Twit');
const config=require('./config');
const util=require(“util”);
const bot=新Twit(配置);
//将我的代码包装在承诺包装中。。。
让post_promise=require('util').promisify(//包装post函数w/promisify以允许顺序过帐。
(选项、数据、cb)=>bot.post(
选项,
数据,
(错误,…结果)=>cb(错误,结果)
)
);
//异步/等待上一篇文章的结果,获取id。。。
const tweetu crafter=async(数组,id)=>{
for(设i=1;i{
post_承诺('status/update',{status:`${first}}})
.然后((顶部推文)=>{
log(`${top\u tweet[0].text}tweeted!`);
让starting\u id=top\u tweet[0]。id\u str;//获取顶行tweet id。。。
tweet_-crafter(后续,起始id);
})
.catch(err=>console.log(err));
};
module.exports=tweet;

我知道该怎么办了

正如Andy Piper提到的,我需要响应特定的tweet id,而不是线程中的原始tweet id。因此,我重构了代码,将twit模块包装在一个promise包装器中,并使用了一个带有async/await的for循环。像这样:

const Twit = require('twit');
const config = require('./config');
const util = require("util");
const bot = new Twit(config);

// Wrapping my code in a promise wrapper...
let post_promise = require('util').promisify( // Wrap post function w/ promisify to allow for sequential posting.
  (options, data, cb) => bot.post(
    options,
    data,
    (err, ...results) => cb(err, results)
  )
);

// Async/await for the results of the previous post, get the id...
const tweet_crafter = async (array, id) => { 
  for(let i = 1; i < array.length; i++){
    let content = await post_promise('statuses/update', { status: array[i], in_reply_to_status_id: id });
    id = content[0].id_str;
  };
};

const tweet = (first, subsequent) => { 
  post_promise('statuses/update', { status: `${first}` })
    .then((top_tweet) => {
        console.log(`${top_tweet[0].text} tweeted!`);
        let starting_id = top_tweet[0].id_str; // Get top-line tweet ID...
        tweet_crafter(subsequent, starting_id);
    })
    .catch(err => console.log(err));
};

module.exports = tweet;
consttwit=require('Twit');
const config=require('./config');
const util=require(“util”);
const bot=新Twit(配置);
//将我的代码包装在承诺包装中。。。
让post_promise=require('util').promisify(//包装post函数w/promisify以允许顺序过帐。
(选项、数据、cb)=>bot.post(
选项,
数据,
(错误,…结果)=>cb(错误,结果)
)
);
//异步/等待上一篇文章的结果,获取id。。。
const tweetu crafter=async(数组,id)=>{
for(设i=1;i{
post_承诺('status/update',{status:`${first}}})
.然后((顶部推文)=>{
log(`${top\u tweet[0].text}tweeted!`);
让starting\u id=top\u tweet[0]。id\u str;//获取顶行tweet id。。。
tweet_-crafter(后续,起始id);
})
.catch(err=>console.log(err));
};
module.exports=tweet;
使用twit线程 Twit Thread是一个用Typescript编写的Node.js模块,它向Twit Twitter API包装器添加实用程序函数,并帮助您在Twitter机器人中实现线程

const{TwitThread}=require(“TwitThread”);
//或者从Typescript中的“twit线程”导入{TwitThread}
常量配置={
消费者密钥:“…”,
消费者的秘密:“…”,
访问令牌:“…”,
访问令牌密钥:“…”,
timeout_ms:60*1000,//可选HTTP请求超时以应用于所有请求。
strictSSL:true,//可选-要求SSL证书有效。
};
}
异步函数tweetThread(){
const t=新的TwitThread(配置);
等待t.tweetThread([
{文本:“你好,消息1/3”},
{text:“这是一个线程2/3”},
{正文:“再见3/3”}
]);
}
tweetThread();
更多信息:

使用twit线程 Twit Thread是一个用Typescript编写的Node.js模块,它向Twit Twitter API包装器添加实用程序函数,并帮助您在Twitter机器人中实现线程

const{TwitThread}=require(“TwitThread”);
//或者从Typescript中的“twit线程”导入{TwitThread}
常量配置={
消费者密钥:“…”,
消费者的秘密:“…”,
访问令牌:“…”,
访问令牌密钥:“…”,
timeout_ms:60*1000,//可选HTTP请求超时以应用于所有请求。
strictSSL:true,//可选-需要SSL证书