gpt4 book ai didi

javascript - 通过 Twit w/Node.js 发布 Twitter 主题

转载 作者:行者123 更新时间:2023-12-03 00:13:52 24 4
gpt4 key购买 nike

我正在使用 Node 和 npm Twit 模块将推文发布到 Twitter。它正在工作......有点。

我能够成功发布一条推文,没有任何问题。但是,当我尝试一起发布一串推文(如 Twitter 上的线程)时,推文无法正确显示。这是我的代码的相关部分。

本质上,我可以毫无问题地发布最初的推文(函数中的“第一个”参数)。然后,我获取该推文的唯一 ID(同样,没问题)并尝试循环遍历字符串数组(“后续”参数)并发布对该推文的回复。代码如下:

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!`);
})
}

});
}
});
};

无论出于何种原因,这些推文都没有显示在 Twitter 上的同一线程下。它看起来像这样:(这里应该还有两条“子推文”。这些推文“发布”但与原始推文分开):

enter image description here

还有其他人在使用 Twitter API 时遇到过类似的问题吗?知道如何通过 Twit 更优雅地发帖吗?谢谢!

最佳答案

使用twit-thread

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

const { TwitThread } = require("twit-thread");
// or import { TwitThread } from "twit-thread" in Typescript

const config = {
consumer_key: '...',
consumer_secret: '...',
access_token: '...',
access_token_secret: '...',
timeout_ms: 60*1000, // optional HTTP request timeout to apply to all requests.
strictSSL: true, // optional - requires SSL certificates to be valid.
};

}
async function tweetThread() {
const t = new TwitThread(config);

await t.tweetThread([
{text: "hello, message 1/3"},
{text: "this is a thread 2/3"},
{text: "bye 3/3"}
]);
}

tweetThread();

更多信息:https://www.npmjs.com/package/twit-thread

关于javascript - 通过 Twit w/Node.js 发布 Twitter 主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54602481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com