Bot that tweets when a specific account tweets (not working!)

nodejs
javascript

#1

Hey everybody,

Apologies if this question is not in the correct section.

I’m working on a project where I have a twitter bot that tweets(Not as a reply) every time a certain account tweets.

I can get it working when I set up a test account but when I do it with the real account (a popular one) my bot will just keep tweeting continuously and not when the specified account tweets.

I am basing my code on Daniel Shiffman’s twitter bot tutorials on YouTube:

My Code is below:

console. log('The streambot is starting');
  var Twit = require('twit'); 
  var config = require('./config'); 
  var T = new Twit(config);


  var stream = T.stream('statuses/filter', { follow: '(//TWITTERID OF ACCOUNT GOES HERE' });  
  stream.on('tweet',thisTweet); 

  function thisTweet(){

    var randomWords= "//a list of random words"

    var splitrandom = randomWords.split(",");

  //picks a random word from list
  var oneWord = splitrandom [Math.floor(Math.random() * splitrandom .length)];

tweetIt(oneWord);

 }

  function tweetIt(txt){

    var tweet = {
    status : txt 
   }


   T.post('statuses/update',tweet,tweeted);

  }

  function tweeted(err, data, response) {
    if (err){ 
        console.log("something went wrong!");
    }else{
        console.log("It Worked");

    }
  }

Any help would be great!

P.S I should also mention that I am doing this through node, javascript using the Twit API…but if anyone else has other suggestions to achieve the same thing I’ll happily try them.


#2

From the Streaming API documentation:

follow

A comma-separated list of user IDs, indicating the users whose Tweets should be delivered on the stream. Following protected users is not supported. For each user specified, the stream will contain:

  • Tweets created by the user.
  • Tweets which are retweeted by the user.
  • Replies to any Tweet created by the user.
  • Retweets of any Tweet created by the user.
  • Manual replies, created without pressing a reply button (e.g. “@twitterapi I agree”).

The stream will not contain:

  • Tweets mentioning the user (e.g. “Hello @twitterapi!”).
  • Manual Retweets created without pressing a Retweet button (e.g. “RT @twitterapi The API is great”).
  • Tweets by protected users.

So - if you’re reacting to every Tweet and it is a popular account you’re following, I’m guessing that you’re seeing a lot of replies and retweets etc. You’d have to add some logic around the onTweet section to check whether the Tweet received is one you “should” be reacting to i.e. it was sent by the user you’re following.


#3

Hey Andy,

Thanks for taking the time to reply, much appreciated.

This (finally!) makes a lot of sense and gives me something to work on. I am still quite new to coding but I think you mean I should work on stream.on( ‘tweet’) section;

I’m thinking along theses lines

var stream = T.stream(‘statuses/filter’, { follow: ‘(//TWITTERID OF ACCOUNT GOES HERE’ });
stream.on(‘tweet’,thisTweet);

if (Account I am following tweets)
{
function tweet (my bot tweets)
} Else {

Do nothing (my bot does not tweet or react to any retweets etc of Account I am following)

}

Thanks again for your help!


#4

Yep, that’s exactly it - I haven’t dug into the JS code enough to know that the logic in (Account I am following tweets) should be, but hopefully you can figure that out!


#5

Great Andy,

I’ll start working on it…and post my results…or possibly post more questions!!

Thanks again!


#6

Hey Andy,

Still working on this and not getting there!

What I have is

var stream = T.stream('statuses/filter', { follow: '(//TWITTERID OF ACCOUNT GOES HERE' });  
  stream.on('tweet',thisTweet); 

var userID =TWITTERID OF ACCOUNT GOES HERE


if ('statuses/filter' !=  userID){
	console.log("error")
} else {
	thisTweet;
}

Basically trying to make it that thisTweet is only called if the account I am following Tweets and nothing else. But I am having the same problem as before, it seems to be calling thisTweet on all instances of the other accounts tweet (retweets etc)

Any suggestions would be greatly appreciated!


#7

You’re nearly there but you’re not quite checking for the user ID match in the right way. Here’s an example of how I might do this (try this code and see what you think…)

// define the ID of the user we are interested in
var userID = 'some number';

// open a stream following events from that user ID
var stream = T.stream('statuses/filter', { follow: ( userID ) });

	stream.on('tweet', function (tweet) {
		// compare the user ID inside the Tweet object we passed in
		// to check it matches
		if (tweet.user.id == userID) {
			console.log("this was sent by the user we want to track")
			// now do something else
		} else {
			console.log(tweet.user.id + " - " + tweet.user.screen_name)
			// so we can ignore it
		}
	});

#8

Hey Andy,

That seems to be it!

Thanks so much for your help…really appreciate it!


#9

Awesome, glad to have helped you along. Enjoy!


#10

I’m working on a similar thing, but popular users don’t tweet very often. However, they get tweeted very often, and the Stream API just returns a sample of these tweets. How can I ensure that the sample I receive doesn’t exclude the rare and original tweet from the popular user I want to follow?