Skip to content

Create a group Twitter account

Twitter rocks. It’s useful for all kinds of things, but especially for chronicling a live event as it happens, including the pre-event discussion and post-conference wrapup.

We’re very excited to be hosting NewB Camp here in Providence, RI on February 23rd. In preparation for the event, Sara created a NewBCamp Twitter account and I coded up this quick script to pull in all tweets related to the conference.

It examines all of your followers tweets for a particular phrase or tag, and then reposts those tweets containing the tag to its own timeline with the author’s name prepended. I’m running this as a cron job on my hosting account. You can see it in action here.

This is a quick hack. It has a couple of issue that I’m aware of:

  • Someone has to log in and manually add followers.
  • The Twitter API only returns the previous 20 friends posts, and it’s possible we might miss some if we have so many friends that the post rate exceeds 20/50 secs (our permitted API request rate).

I do hope that you find this useful for creating your own Twitter event monitor!

<?php

include(“twitter.php”); // Twitter API class
// Available: http://twitter-development-talk.googlegroups.com/web/api_class.phps.txt

// configurable options
$twitter_user = “newbcamp”; // Twitter username
$twitter_pass = “passwordgoeshere”; // Twitter password
$tag = “newbcamp”; // tag for friends to use

$twitter = new Twitter($twitter_user, $twitter_pass);

$last_post = json_decode($twitter->getUserTimeline(“json”,$twitter_user,1), true);
$last_post = $last_post[0][‘created_at’]; // get the datetime stamp of the last post to the account

// get new posts from friends since last update
$friends_posts = json_decode($twitter->getFriendsTimeline(“json”,$twitter_user,$last_post), true);

foreach($friends_posts as $key => $post) {
        if (stripos($post[‘text’],$tag)) { // if the tag is present
                if ($post[‘user’][’screen_name’] != $twitter_user) { // no infinite loops, please
                        $new_post = $post[‘user’][’screen_name’] . “: “ . $post[‘text’];
                        // post the new post to the newbcamp account with the user’s name prepended
                        $twitter->updateStatus($new_post);
                }
        }
}

?>

Related posts

{ 9 } Comments

  1. Alan Levine | January 22, 2008 at 6:49 pm | Permalink

    Oh this is very cool, and you share lovely code. I am now distracted from my lists of tasks to go and play with this.

    thanks

  2. Shawn Collins | February 14, 2008 at 11:35 pm | Permalink

    Our programmer is trying to get this set for us and just sent me the following:

    “Now that i am running the cron job, I actually see what the problem with this is. In the following line:

    $last_post = json_decode($twitter->getUserTimeline(“json”,$twitter_user,1), true);

    …. and in this line ….

    // get new posts from friends since last update
    $friends_posts = json_decode($twitter->getFriendsTimeline(“json”,$twitter_user,$last_post), true);

    … there is a call to a function called “json_decode” which function doesn’t exist in the code, nor in the include file mentioned in the text. Actually, I think that they changed the code for the include file (because it is not hosted on the same server) and that is why we now have a discrepancy in the code which in turn produces an error. ”

    Any insight on that?

  3. Hilary | February 15, 2008 at 11:17 am | Permalink

    Hi Shawn,

    The “json_decode” function is part of the standard PHP 5.2 install. Check it out on php.net.

    Your programmer can either upgrade PHP, or write her own function to parse the JSON output from the Twitter API. :)

    Hilary

  4. Gil Creque | March 4, 2008 at 4:08 pm | Permalink

    I found another great article on how to do this

    http://www.steveouting.com/group-twittering-instructions.html

    One of the main things I read was getting auto-follow turned on by tech support.

  5. Shai Gluskin | June 15, 2008 at 10:14 pm | Permalink

    Hi Hilary,

    This is fantastic. I’d love to get this into the current Drupal Twitter module which allows posting to a group Twitter via a Drupal web site. Which is great. But people should be able to post from their own Twitter account to a group or event account as you’ve done here.

    I am getting the following error however: “Warning: Invalid argument supplied for foreach()”. I don’t think it is the same problem mentioned above because the file is on a server running Php 5.2.

    Any ideas?

    thanks,

    Shai Gluskin

  6. Shai Gluskin | June 15, 2008 at 11:08 pm | Permalink

    Okay, my problem in comment #5 was solved by replacing my email address with my username. I’m used to the two being interchangable, but obviously they are different at Twitter’s db and this code is clearly calling for the username.

    Now my problem is that it just doesn’t work. No messages pulled in, but no errors when the script runs :).

    I’m confused by this part of the instructions, so maybe that is where the problem is: “# Someone has to log in and manually add followers.”

    What?

    What I thought that meant is that, in order for a person’s twitters to be posted to the group account, that person needs to sign up as a “follower” of the group account. Please confirm. Then, as long as a person includes the designated tag in the twitter, that post will get reposted, prepended by the person’s Twitter name, to the group twitter.

    I presume that the group account does not need to “follow” its followers?

    Any insights on the problem would be helpful. This functionality is way cool and very needed.

    Shai

  7. Hilary | June 16, 2008 at 2:26 pm | Permalink

    Hi Shai,

    The group twitter user will only pull tweets from the people that it is following. You should log in and have that user follow the set of twitter users you would like in the group.

    Alternatively, you can e-mail support@twitter.com and ask them to turn on auto-following for your account. That way, you’ll be set up to follow everyone who follows you.

    Good luck!

    Hilary

  8. Shai Gluskin | June 16, 2008 at 3:56 pm | Permalink

    Hilary,

    Thanks much. It seems like the auto-friending should be part of the API, but oh well, not terrible.

    I had to adjust the code a tad to make it run off of my server. The Boolien in response to “if” testing if the message had the right tag would always return false.

    I’m running php off a mosso.com server which runs PHP Version 5.2.0-8+etch5~pu1. That’s a mouthful.

    Here is what I did.

    Original code:

    if (stripos($post[‘text’],$tag)) {

    What I did to make it work (for me)

    $tagexists = stripos($post['text'], $tag);
    if ($tagexists !== false) {

    Hmmm, it seems like it should work either way!

    This is great stuff. Twitter has so much potential. I’m going to use this for a group crime reporting Twitter for my neighborhood.

    All the best,

    Shai

  9. Shai Gluskin | June 16, 2008 at 9:07 pm | Permalink

    I’m really excited now. Between Drupal’s twitter module and the code posted here by Holly, I can create a community crime reporting Twitter in which Twitter members, as well as non-Twitterers (via the Drupal site) will be able to create posts to the twitter.

    Sometimes things just all come together.

    Shai

Post a Comment

Your email is never published nor shared. Required fields are marked *