试图让Twitter Bot一次发布两张图片

问题描述 投票:1回答:1

我正在开发一个Twitter机器人,其目的是发布两个图像以及一串文本。我正在使用node.js(第一次,我应该添加)和the Twit package

我遇到了各种各样的问题,其中许多可能只是由于我是新手,但我确实无法弄清楚如何正确地输出悬挂的东西。我已经设法输出文本和单个图像,但是我想一次吐出两个图像。

主要的机器人程序块使用以下代码来构建和安排一条推文:

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完整构建了tweet,它就会传递到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:这些编辑现在反映在上面的代码中,以及我对整个项目所做的一些其他修复。我到达了一个状态,一切又恢复了平稳(找到了一个缺少的括号,我很讨厌这个编码方面的东西),并且成功发布了一些推文,但就像之前我没有获得第二张图片一样。较早帮助过的室友建议只为每种可能的卡组合抽出静态单张图像,但必须有一个更优雅的解决方案。再说一遍,任何想法都可以节省我一个星期奇怪的卧室修改工作,对此我深表感谢。

javascript node.js image twitter bots
1个回答
0
投票

[花了很多时间,但我弄清楚了。每个图像都必须单独上载到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,然后将二者组合成一个用逗号分隔的字符串,该字符串将作为bothImages加载到media_ids数组中。

这真是一场噩梦,但我想确保记录在案,以防其他任何人在这里偶然寻找相同答案的情况。

© www.soinside.com 2019 - 2024. All rights reserved.