Issues with Twitter Bot/node/processing

Hey everybody,

Not sure if this is the correct section for this question, apologies if not.

I am working my way through Daniel Shiffman's excellent twitter bot tutorials. I can get almost all my sketches etc working no problem however I am having trouble with one.

I am trying to get my bot to generate an image every time a certain twitter account tweets. I can get it it work occasionally but more not often than not it seems to be looping(in a glitchy kind of way)

Here is the code that:

    /* The goal of this test sketch
    is that everytime a certain twitter user tweets
    an image is created in Processing and
    is uploaded to my twitterbot
    */

    // set up
    console. log('The streambot is starting');//lets me check that it is working
    var Twit = require('twit'); //requires twit package 
    var config = require('./config'); // Get the config file I've setup with keys
    var T = new Twit(config);//run config file
     //end of setup 

     //related to image
    var exec = require('child_process').exec;
    var fs = require('fs');//lets me use the function readFileSync

    //stream for twitter account
    var stream = T.stream('statuses/filter', { follow: 'TWITTER ID HERE' });//twitter accounts id no. here 
    stream.on('tweet',tweetIt);
    console.log('got here')


    //running processing 
    function tweetIt(){ 
    var cmd = 'processing-java --sketch=`pwd`/image --run';
    exec(cmd,processing);
    console.log('and here'); //This is where the problem seems to be
    //terminal just keeps spintting out 'and here' repeatedly

    function processing(){ 
        var filename = 'image/output.png'
        var params = {
            encoding: 'base64' 

    }
    var b64 = fs.readFileSync(filename,params);
    T.post('media/upload',{ media_data:b64 }, uploaded);
    }

    //uploading image
    function uploaded(err,data,respnse){//THIS IS ThE CALL BACK and what I will TWEET
        var id = data.media_id_string; 
        var tweet = {

    status: '.@MYTWITBOTACCOUNT', // uploads my image to my bot account
        media_ids: [id]
    }
    T.post('statuses/update',tweet,tweeted);
    }
    function tweetIt(txt){ // whenever I call tweet it function text that gets passed in gets tweeted 
    var tweet = {//this is my OBJECT called tweet
        status: txt//tweets the txt function which is in the follwoed function 
    }
    }
      function tweeted(err, data, response) {//this is my call back function don't really neei it here 
      if (err){ // but am using if there is an error
        console.log("something went wrong!");//after it tweets tells me if it works
      }else{
        console.log("It Worked");

      }
      }
    }

I think the problem might be here

   //running processing 
        function tweetIt(){ 
        var cmd = 'processing-java --sketch=`pwd`/image --run';
        exec(cmd,processing);
        console.log('and here'); //This is where the problem seems to be
        //terminal just keeps spintting out 'and here' repeatedly

As in the terminal I keep getting 'and here ' repeatedly (to the point of crashing terminal) although sometimes it will work and generate the image.

If anyone has encountered a similar problem or can spot the problem please let me know,

Thanks for your time,

Paul

Answers

  • Answer ✓

    Isn't line 20 looping over every tweet? And calling tweetIt every time? That's why you're getting the message repeatedly. I'd stop that, at least until it's working

    Your code needs indenting, it aids readability immensely.

  • Hey Koogs,

    Thanks for the reply. It was working originally but now every time I try(and on different computers) I'm getting the looping issue.

    I am trying to set it up so that when the person I'm follwoing tweets an new tweet tweetIt will run allowing my bot to draw an image and post.

    If I remove stream.on('tweet',tweetIt);

    Will this not mean that the image part of the code will not be called?

    I just tried:

    var stream = T.stream('statuses/filter', {follow: 'TWITTER ID HERE' });//twitter accounts id no. here 
    stream.on (tweetIt);
    console.log('got here')
    

    but got following errot message in terminal

    var stream = T.stream('statuses/filter' { follow: 'TWITTER ID' });
                          ^^^^^^^^^^^^^^^^^
    
    SyntaxError: missing ) after argument list
        at exports.runInThisContext (vm.js:53:16)
        at Module._compile (module.js:373:25)
        at Object.Module._extensions..js (module.js:416:10)
        at Module.load (module.js:343:32)
        at Function.Module._load (module.js:300:12)
        at Function.Module.runMain (module.js:441:10)
        at startup (node.js:139:18)
        at node.js:968:3
    

    Thanks again for help (it's my first time working on a project like this and my programming skills are bot the best, excuse the ignorance!)

  • Answer ✓
    var stream = T.stream('statuses/filter', {follow: 'TWITTER ID HERE' });
    

    vs

    var stream = T.stream('statuses/filter' { follow: 'TWITTER ID' });
    

    you've deleted the comma

    Will this not mean that the image part of the code will not be called?

    yes. but it's only temporary until you fix the processing call - you've no idea how many tweets there are going to be. there could be 100 tweets, that's 100 calls to the processing bit, and 99 of those are just noise.

  • Hey Koogs,

    Thanks for getting back to me. I've put the comma back in place.

    However, when I just run it as

    var stream = T.stream('statuses/filter', {follow: 'TWITTER ID HERE' });//twitter accounts id no. here 
    stream.on (tweetIt);
    console.log('got here')
    

    I get:

    The streambot is starting events.js:197 throw new TypeError('listener must be a function'); ^

    TypeError: listener must be a function
        at addListener (events.js:197:11)
    

    I have an earlier method that I will try again.. This one definitely works!

    var stream = T.stream('user','with=followings'); 
    stream.on('tweet',tweetIt);
    

    By using this my twit bot will tweet/generate an image every time the account that it follows tweets.

    However it also seems to tweet when the bot itself tweets..

    So: Account it follows tweets, my bots tweets an an image but then because my bot has tweeted an image, it tweets another and so on.

    Would you know of a way to make it just tweet once when the accout if follows tweets?

    Thanks again for your time and patience!

  • Answer ✓

    That is JavaScript Twitter library territory, not something I know anything about.

    But it should be easy enough to disable the listening and just call the tweet method once per invocation, for debugging.

    Your problem is just invoking processing from JavaScript. Focus on that.

  • Thanks for your help Koogs, much appreciated.

    I'll keep working on it and if anyone else want to chip in, feel free!

Sign In or Register to comment.