Mon. Dec 23rd, 2024

“Standing on the shoulders of giants” is how I created a twitter bot and it was all thanks to this code…

var Twit = require('twit')
var T = new Twit({
    consumer_key:         ' ... ',
    consumer_secret:      ' ... ',
    access_token:         ' ... ',
    access_token_secret:  ' ... ',
})
var users = ["10228272", "155659213", "783214"];
var stream = T.stream('statuses/filter', {follow: users});
stream.on('tweet', function (tweet) {
    if (users.indexOf(tweet.user.id_str) > -1) {
        console.log(tweet.user.name + ": " + tweet.text);
        T.post('statuses/retweet/:id', { id: tweet.id_str }, function (err, data, response) {
            console.log(data)
        })
    }
})

And this article here.

I then got the twitter ID’s from gettwitterid.com and changed the above code after “Var users =”

I used a command line tool called “Screen” to run the twitter bot in the background on my headless Ubuntu server. It means you can come back to it at any point by using the command ‘screen -r’

By Simon