October 25, 2010

Create a Twitter ReTweet Bot

Personally, I needed a Twitter RT bot that can be used to collect various Tweets around a keyword or a hashtag.

Just simply coded using PHP, MySQL and abraham's twitteroauth.

The program flow is:

1) Get the search results,
2) and retweet a tweet if it is not retweeted yet.

It runs as a cronjob.

See the below code:
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $key, $secret);
$response = $connection->get('search', array('q'=>'#hashtag OR "some keyword"'));
foreach ($response as $status) {

  for($i=0; $i < count($status); $i++)
  {
    $tweetid = $status[$i];
    $result = mysql_fetch_array(mysql_query("select tid from tweets where tid = ".$tweetid.";"));
    if(empty($result['tid'])) {
      mysql_query("insert into tweets (tid) values (".$tweetid.");");
      $connection->post('statuses/retweet/'.$tweetid);
    }
  }

}

No comments:

Post a Comment