我正在开发一个twitter机器人,它的目标是发布两张图片和一串文本。我正在使用node.js (我可能是第一次添加)和the Twit package。
我遇到了各种各样的问题,其中许多问题可能只是因为我是一个新手,但我真的不知道如何正确地输出这个该死的东西。我已经设法将文本和单个图像输出,但我试图同时输出两个图像。
主要的bot块使用以下代码来构建和调度tweet:
function mainPostBot() {
console.log("Now assembling a new tweet.");
var leftCard = getRandomNumber(1, 36);
console.log("The left card is #" + leftCard + ", " + cardNames[leftCard] + ".");
// Generates a random number for the left card.
var leftImagePath = path.join(__dirname, '/leftImage/' + imageArray[leftCard]);
console.log("The left image's path is " + leftImagePath);
// Gives the file path to access the correct image for the left.
var rightCard = getRandomNumber(1, 36);
console.log("The right card is #" + rightCard + ", " + cardNames[rightCard] + ".");
// Generates a random number for the right card.
while (leftCard == rightCard) {
var rightCard = getRandomNumber(1, 36);
console.log("Whoops! The right card is now #" + rightCard + ", " + cardNames[rightCard] + ".");
// Generates a random number for the right card in the case of doubles.
}
var rightImagePath = path.join(__dirname, '/rightImage/' + imageArray[rightCard]);
console.log("The right image's path is " + rightImagePath);
// Gives the file path to access the correct image for the left.
console.log('Encoding the images...');
var b64contentLeft = fs.readFileSync(leftImagePath, { encoding: 'base64' });
var b64contentRight = fs.readFileSync(rightImagePath, { encoding: 'base64' });
var bothImages = (b64contentLeft + "," + b64contentRight);
// This encodes the images in base64, which twitter needs. I guess. I dunno, man.
var tweetText = (jsUcfirst(cardNames[leftCard]) + ' and ' + cardNames[rightCard] + '. (#' + leftCard + " " + cardCorrespond[leftCard] + "/#" + rightCard + " " + cardCorrespond[rightCard] + ")");
// This constructs the grammar of the tweet.
// jsUcfirst capitalizes the first letter of a string so it lets me cheat a sentence start.
var tweetTime = getRandomNumber(1000*60*60*4, 1000*60*60*24*3+1);
// Generates an amount of time before the next tweet.
sendTweet(tweetText, bothImages, tweetTime);
setTimeout(mainPostBot, tweetTime);
}
mainPostBot();cardNames、cardCorrespond和imageArray只是程序顶部的大型数组,它们分别列出了图像的名称、有关图像的一些信息以及它们的文件名:
var cardNames = new Array(
"the Fool", //This one will never be called bc of the number generator and it's fun bc, y'know, Tarot
"the Rider","the Clover","the Ship","the House","the Tree","the Clouds","the Snake","the Coffin","the Bouquet","the Scythe","the Whip", //"the Nae Nae",
"the Birds","the Child","the Fox","the Bear","the Stars","the Stork","the Dog","the Tower","the Garden","the Mountain","the Crossroads",
"the Mice","the Heart","the Ring","the Book","the Letter","the Gentleman","the Lady","the Lily","the Sun","the Moon","the Key","the Fish",
"the Anchor","the Cross"
);
var cardCorrespond = new Array(
" ","9♥","6♦","10♠","K♥","7♥","K♣","Q♣","9♦","Q♠","J♦","J♣","7♦","J♠","9♣","10♣","6♥","Q♥","10♥",
"6♠","8♠","8♣","Q♦","7♣","J♥","A♣","10♦","7♠","A♥","A♠","K♠","A♦","8♥","8♦","K♦","9♠","6♣"
);
var imageArray = new Array(
" ","01.png","02.png","03.png","04.png","05.png","06.png","07.png","08.png","09.png","10.png","11.png","12.png","13.png",
"14.png","15.png","16.png","17.png","18.png","19.png","20.png","21.png","22.png","23.png","24.png","25.png","26.png",
"27.png","28.png","29.png","30.png","31.png","32.png","33.png","34.png","35.png","36.png"
);一旦mainPostBot完全构建了这条推文,它就会被传递给sendTweet:
function sendTweet(text, images, time){
console.log('Uploading the images...');
T.post('media/upload', { media_data: images }, function (err, data, response){
if (err){
console.log("There's an issue uploading the images.");
console.log(err);
} else {
console.log('Images uploaded!');
console.log("Now tweeting...")
T.post('statuses/update', {
status: text,
media_ids: new Array(data.media_id_string)
}, function(err, data, response){
if (err) {
console.log("An error has occurred during posting.");
console.log(err);
} else {
console.log("Post successful!");
console.log("The tweet says:" + text);
console.log("The next tweet will send in " + msToTime(time) + "!");
}
});
}
});
}有什么想法吗?当然,我对使用其他npm包持开放态度,但我就是不明白为什么这个包不能正常工作。感谢您的阅读,如果您需要代码的任何其他部分,请告诉我。
编辑1:我的室友也涉足了这类东西,他发现了另一个软件包node-twitter的潜在useful link on github。在这个链接中,一张海报解释了图像应该以字符串的形式传递,用逗号分隔,所以我在mainPostBot和sendTweet中添加了一些编辑,主要是在传递b64图像数据时。
编辑2:这些编辑现在反映在上面的代码中,以及我对整个项目所做的一些其他修复。我到了一个点,事情又顺利地运行了(我发现了一个缺失的括号,我对这个编码的东西很糟糕),tweet成功地发布了,但就像之前一样,我没有通过第二张图片。早些时候帮助过的室友建议为每种可能的卡片组合输出静态的单一图像,但必须有一个更优雅的解决方案。再说一次,任何想法都可以节省我一个星期的奇怪的卧室修修补补,我很感激大家关注这一点。
发布于 2020-05-29 13:43:13
我花了很多功夫,但我还是弄明白了。每张图片都必须单独上传到twitter,所以在加载图片后,我将其data.media_id_string保存到一个变量中,然后将这些值以数组的形式加载到tweet中。
我删除了mainPostBot中组合b64contentLeft和b64contentRight的那一行代码,并使用返回的数据字符串将其添加到sendTweet代码中。现在,我使用以下命令调用sendTweet():
sendTweet(tweetText, b64contentLeft, b64contentRight, tweetTime);sendTweet()现在看起来像这样:
function sendTweet(text, leftimage, rightimage, time){
console.log('Uploading the images...');
T.post('media/upload', { media_data: leftimage }, function (err, data, response){
if (err){
console.log("There's an issue uploading the left image.");
console.log(err);
} else {
console.log('Left image uploaded!');
var leftID = data.media_id_string;
T.post('media/upload', { media_data: rightimage }, function (err, data, response){
if (err){
console.log("There's an issue uploading the right image.");
console.log(err);
} else {
console.log('Right image uploaded!');
var rightID = data.media_id_string;
var bothImages = ( leftID + "," + rightID );
console.log("Now tweeting...")
T.post('statuses/update', {
status: text,
media_ids: new Array(bothImages)
}, function(err, data, response){
if (err) {
console.log("An error has occurred during posting.");
console.log(err);
} else {
console.log("Post successful!");
console.log("The tweet says: " + text);
console.log("The next tweet will send in " + msToTime(time) + "!");
}
});
}
});
}
});
}基本上,如果左边的图片上传正确,它将保存该ID,然后尝试右边的图片。如果成功,它还将保存该ID,然后将这两个ID组合成一个用逗号分隔的字符串,该字符串将作为bothImages加载到media_ids数组中。
这是一场需要解决的噩梦,但我想确保它被记录下来,以防其他人在这里寻找同样的答案。
https://stackoverflow.com/questions/62077448
复制相似问题