Showing posts with label Twitter. Show all posts
Showing posts with label Twitter. Show all posts

6 Mar 2011

Twitter OAuth Perl Module

Since Twitter changed their authentication method for using their API to OpenAuth, sending an automated tweet has become a whole lot more difficult.
In response to this I decided to code my own PERL module for OAuth.

Limitations of the module


Just before I post the code below I will mention that all this module currently does is allow you to post tweets, it does not handle retrieving access tokens or any other OAuth jazz. This module is really for single user twitter apps, like RSS feeds and twitter bots, or other situations where you already have all the required access tokens. If you register an app on twitter as a single access app, twitter will give you all the OAuth tokens you need. Point your browser at http://twitter.com/apps/new and sign in as your twitter bot or RSS feed posting account.

The Code


package Jimtweet;
#####JimTweet 0.1 By James Januszka 2010
#####Email:jimjanuszka@gmail.com
#####Twitter:@jamesjanuszka
use strict;
use warnings;
use LWP;
use HTTP::Headers;
use URI::Encode qw(uri_encode);
use URI::Escape qw(uri_escape);
use Digest::HMAC_SHA1;

####Constructor####
sub new {
 my $self={};
 $self->{OAUTH_VERSION}=uri_escape("1.0");
 $self->{OAUTH_SIGNATURE_METHOD}=uri_escape("HMAC-SHA1");
 $self->{OAUTH_TIMESTAMP}=undef;
 $self->{OAUTH_NONCE}=undef;
 $self->{AGENT}="jimtweet/0.1";
 $self->{URL}="http://api.twitter.com/1/statuses/update.xml";
 $self->{BROWSER}=LWP::UserAgent->new(agent =>$self->{AGENT});
 $self->{CONSUMER_KEY}=undef;
 $self->{CONSUMER_SECRET}=undef;
 $self->{OAUTH_TOKEN}=undef;
 $self->{OAUTH_TOKEN_SECRET}=undef;
 $self->{STATUS}=undef;
 bless($self);
 return $self;
} 

sub consumer_key{
 my $self=shift;
        if (@_) { $self->{CONSUMER_KEY}=uri_escape(shift) }
        return $self->{CONSUMER_KEY};
}
sub consumer_secret{
 my $self = shift;
        if (@_) { $self->{CONSUMER_SECRET}=uri_escape(shift) }
        return $self->{CONSUMER_SECRET};
}
sub oauth_token{
 my $self = shift;
        if (@_) { $self->{OAUTH_TOKEN}=uri_escape(shift) }
        return $self->{OAUTH_TOKEN};
}
sub oauth_token_secret{
 my $self = shift;
        if (@_) { $self->{OAUTH_TOKEN_SECRET}=uri_escape(shift) }
        return $self->{OAUTH_TOKEN_SECRET};
}

sub update_status(@){
 sleep(2);
 my $self = shift;
        if (@_) { $self->{STATUS}=uri_escape(shift) }
        my $seconds = time(); 
        $self->{OAUTH_TIMESTAMP}=uri_escape($seconds);
        $self->{OAUTH_NONCE}=$self->{OAUTH_TIMESTAMP};
       
       my $query=qq(oauth_consumer_key=$self->{CONSUMER_KEY}&oauth_nonce=$self->{OAUTH_NONCE}&oauth_signature_method=$self->{OAUTH_SIGNATURE_METHOD}&oauth_timestamp=$self->{OAUTH_TIMESTAMP}&oauth_token=$self->{OAUTH_TOKEN}&oauth_version=$self->{OAUTH_VERSION}&status=$self->{STATUS});
       
       my $sig="POST&";
$sig .=uri_encode($self->{URL},1);
$sig .="&";
$sig .=uri_encode($query,1);


my $sig_key=$self->{CONSUMER_SECRET};
$sig_key .="&";
$sig_key .=$self->{OAUTH_TOKEN_SECRET};

my $hmac = Digest::HMAC_SHA1->new($sig_key);
$hmac->add($sig);
my $oauth_signature_base64=$hmac->b64digest;
$oauth_signature_base64 .="=";
my $utf8_oauth_signature_base64=uri_escape($oauth_signature_base64);

my $debug="-v";
my $curlpost=qq(status=$self->{STATUS});

my $header=qq(OAuth oauth_nonce="$self->{OAUTH_NONCE}", oauth_signature_method="$self->{OAUTH_SIGNATURE_METHOD}", oauth_timestamp="$self->{OAUTH_TIMESTAMP}", oauth_consumer_key="$self->{CONSUMER_KEY}", oauth_token="$self->{OAUTH_TOKEN}", oauth_signature="$utf8_oauth_signature_base64", oauth_version="$self->{OAUTH_VERSION}");
my $req=HTTP::Request->new(POST => $self->{URL});
$req->content_type('application/x-www-form-urlencoded');
$req->content($curlpost);

$req->header('Authorization' => $header);  # set

my $res = $self->{BROWSER}->request($req);
return 1
}

####Footer####
1;  #so the require or use succeeds 


Usage


The first thing you need to do is include the module in your perl script, then you have to set up a new tweet object.

my $tweet=Jimtweet->new();

Next you have to specify your OAuth tokens

$tweet->consumer_key($consumer_key);
$tweet->consumer_secret($consumer_secret); 
$tweet->oauth_token($oauth_token);
$tweet->oauth_token_secret($oauth_token_secret);

Finally to send a tweet

$tweet->update_status($status);

The module sets up the nonce and timestamp for you as well as setting up the OAuth signature that many people have trouble with.

Well thats it I hope you all like it.

6 Sept 2008

Getmail for twitter

Getmail a nice little Perl script that when setup with a cron job automatically scans your inbox on behalf of you or one of your twitter apps for emails from twitter and responds accordingly.

There are two types of email messages it responds to the first, is the email you get from twitter when someone sends you a direct message, the second is the one that twitter sends you when someone follows you.


How it works

The Getmail script works as an imap client and accesses the inbox of whichever twitter bot it is monitoring. It uses the Mail::IMAPClient perl module. Once it has successfully connected to the mail sever it selects a mail box, ie your inbox, or a specific mailbox of your choosing.

Once inside it looks for any recent mail. To put it another way it checks for any mail that has the imap RECENT flag attached to it. There is a slight problem with this method in that a message is only flagged as RECENT if no other imap clients have seen it, bare that in mind if you want to use this on your personal mail box, it will stop the script from seeing any mail.

When the script finds a message that is RECENT it checks the headers for the twitter custom headers (for example "X-Twitteremailtype"), and checks their value.

If the X-Twitteremailtype value is "is_following" then it calls a subroutine called follow. This subroutine doesn't do anything yet, it's up to you to fill it with commands. The most obvious would be to auto-follow.

If the  X-Twitteremailtype is "direct_message" then it calls an empty subroutine called directMsg, once again it's up to you the user of the script to define what that subroutine does.

Any other emails it finds in the inbox it just ignores, which is probably a good thing ;)


Possible Applications of this groovy script


You are free to do with this script what ever you wish, of course. This script will obviously be useful if you want to auto-follow people who follow you, or your twitter app. Some people may find use for the directMsg subroutine.

My reason for writting this script was so I co redesign echobot to auto-follow new users and to check direct messages without having to query twitter.


The Source Code

#!/usr/bin/perl

# Getmail.pl (For Twitter)
# By James Januszka, @jamesjanuszka, http://blog.jamesjanuszka.com
# Do as you will with this script but don't blame me if it f**k's up

use strict; # Spank me!
use warnings; # Stop or I'll shoot!
use Mail::IMAPClient;
my $host = "imap.smurfmail.net"; # Contact your email admin for this address
my $username = "pappa\@smurf.com"; # Don't forget to escape characters. Example \@
my $pass = "I <3 Smurfette"; # If you don't know this then, er ... Good Luck!
my $folder ="INBOX";# Which folder you want to look in. Example INBOX 
my $imap;

sub login{
 # login
  $imap = Mail::IMAPClient->new(
            Server => $host,
            User    => $username,
            Password=> $pass,
            )or die "Cannot connect to $host as $username: $@";
}

sub checkMail{
 # Check your mail
 $imap->select($folder) or die "Could not select: $@\n";
 my @recent = $imap->recent;# This checks for recent mail, if you check this mailbox with another client then run this script, it won't have any recent mail. Sorry thats the IMAP specification for you.
 foreach (@recent){
  my $messageType = $imap->get_header($_, "X-Twitteremailtype");
  my $messageFrom = $imap->get_header($_, "X-Twittersenderscreenname");
  my $messageString = $imap->body_string($_);
  if ($messageType eq "direct_message"){
   directMsg("$messageString");
  }
  if ($messageType eq "is_following"){
   follow($messageFrom);
  } 
 }
 
}

sub directMsg{
 # Do what you do with your direct messages here. 
 my @string = split(/\r/, "@_");# Split message email by returns, I used /r because /n was giving me problems
 print "$string[0] \n";
}

sub follow{
 # Do what you do when someone follows you. Ooh could this be a way of Auto-following someone ? ;)
 print "##########################FOLLOW @_#########################\n";
}


sub logout {
 #logout
 $imap->logout or die "Could not logout: $@\n";
}

# It's all very well writing pretty subroutines, but you need to remember to call them 
login;
checkMail;
logout;


picture credit gaetanlee
 

18 Jul 2008

Project Honeybot

Twitter is a great platform for expressing ideas and keeping up with what is going on in the world, but there is also a downside and thats spam. There seems to be alot of twitter users which are just spam bots they just keep following people hopeing to sell you viagra. I think I have found away to name and shame them.

Enter Honeybot

Honeybot is my counter defense to this, what it does is constantly posts to the public time line a warning not to follow it. Now I believe that spambots constantly scrape the public timeline and follow whoevers on there. So they should in theory follow Honeybot automaticly.

The second thing it does is keeps a record of its followers, allowing me to create a public blacklist of it's followers.

Creating a Blacklist of Spambots

What I intend to do is post the list of followers on my website where people can see if they are being followed by spammers. It's basicly a copy of the xml file of followers you can obtain from the twitter API, but I am going to parse it into a more readable format.

Source Code

below is a copy of the source code so you can see how it works:

#!/usr/bin/perl -w
## Honeybot - This bot attempts to attract spam bots and creates a spambot blacklist
## James Januszka, 2008, http://blog.jamesjanuszka.com
##
use strict;
use LWP;
use LWP::Simple;
use URI;
my $agent ='Honeybot/0.1';my $statusurl = 'http://twitter.com/statuses/update.xml';
my $followerurl = 'http://twitter.com/statuses/followers.xml?lite=true';
my $server ='twitter.com:80';
my $realm ="Twitter API";
my $username = "enteryourusername";
my $password ="enteryourpassword";
my $file="followers.xml";
my $string ="Please do not follow this bot as any followers will be added to the blacklist. See http://tinyurl.com/5bomg4 for more details.".int(rand(100000));
my $browser = LWP::UserAgent->new(agent =>$agent);
$browser->credentials($server,$realm,$username=>$password);
print "Content-type: text/html\n\n";

&updatestatus();
&getfollowers();

sub updatestatus {
my $response =$browser->post($statusurl,['status'=>$string]);
die "OMG, WTF: ",$response->header('WWW-Authenticate'), $response->status_line unless $response->is_success;
}

sub getfollowers {
system("wget --http-user=$username --http-passwd=$password -O $file -U $agent $followerurl");
}

I hope that this will be a useful service for twitter users, if you have any comments about how I can improve Honeybot let me know.

7 May 2008

Echobot now living at Bobsbasement

I've had a word with my friend BOfH, and he has kindly agreed to host Echobot on his server at www.bobsbasement.co.uk. This now means that Echobot is available 24/7 and replies to it's messages every 10 minutes.

Now that Echobot has settled in, I can begin working on my other projects. Watch this space for project Tubeupdate and GMail Twitterfier.

6 May 2008

Echobot has moved

I finally got my web host, Servage, to host Echobot. Now Echobot checks it's messages 24 hours a day 7 days a week. The only problem is instead of checking it's messages every 10 minutes, it now checks them hourly on the hour. I feel Echobot doesn't need real-time interactivity(whoa, Management speak 2.0!) so this should not be a problem.

25 Apr 2008

Echobot is now in testing

Echobot is a bot that I have written for twitter. It was written in PERL script. The way it works is, when you send it a direct message from twitter, it sends that message back to you. It might seem like a strange thing to do, but it has its uses.

The main reason I wanted to create such a bot was that I wanted to send messages from my PC to my mobile phone, for example one day I was looking for the address of my nearest branch of a particular building society. When I found it on the internet I wanted to text that information to my phone, so when I got to London I knew what the address was. I'm sure there could be other uses for Echobot but we will have to wait and see.

How to use Echobot

In order to use Echobot you have to follow it on twitter, and receive notifications from it on your mobile. Then I have to get it to follow you (I have yet to find a way to make it auto-follow people). Once that is setup all you have to do is sent it a direct message and it will send you it back.

For example you would type this in the textbox on the twitter website

d echobot Hello how are you doing?
Currently Echobot checks its messages every ten minutes, and I am running it on my PC as a crontab. So Echobot is only awake whilst my PC is on.

Here is the link for Echobots profile on twitter, www.twitter.com/echobot.
I am currently setting up a home page for Echobot at www.jamesjanuszka.com/content/echobot Where you can also download the source code.

I have also posted the code below
#!/usr/bin/perl -w
#
# Echobot - This bot echos back any tweets you send it
#
# James Januszka, 2008, http://echobot.trinician.com/ and
# http://jims-tech.blogspot.com
#
#    THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
#    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#    MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
#    DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS
#    BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
#    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
#    TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
#    ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
#    OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
#    OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#    POSSIBILITY OF SUCH DAMAGE.
#
use strict;
use LWP;
use URI;

my $agent ='Echobot/0.1'; # Useragent
my $statusurl = 'http://twitter.com/statuses/update.xml'; # Url for sending updates to
my $repliesurl='http://twitter.com/direct_messages.xml'; # Url for direct message inbox
my $deleteurl='http://twitter.com/direct_messages/destroy/'; # Url to delete messages
my $server ='twitter.com:80'; # Twitter's Server name and port number for HTTP authentication
my $realm ="Twitter API"; # Twitter's Realm name for HTTP authentication
my $username = ""; # Your bots username for Twitter
my $password =""; # You bots password for Twitter
my $browser = LWP::UserAgent->new(agent =>$agent);
$browser->credentials($server,$realm,$username=>$password);

sub directmessage {
my ($greeting) = @_;
my $response =$browser->post($statusurl,['status'=>$greeting]);
die "OMG, WTF: ",$response->header('WWW-Authenticate'), $response->status_line unless $response->is_success;
}

sub getreplies{
my $response = $browser->get($repliesurl);
die "OMG, WTF: Can't get $repliesurl --", $response->status_line unless $response->is_success;
if ($response->content =~m//) {
my @out = split(//, $response->content);
foreach (@out){
my $string = $1 if ($_=~ /(.*?)<\/text>/s);
my $sender =$1 if ($_=~ /(.*?)<\/sender_screen_name>/s);
my $id=$1 if ($_=~/(.*?)<\/id>/s); 
$string = "d ".$sender." ".$string; 
&directmessage ("$string");
&deletemessage($id);
}
} else {print "No more messages. \n";}
}

sub deletemessage{
my ($message)=@_;
my $url=$deleteurl.$message.".xml"; # $deleteurl + message id number + .xml
my $response = $browser->get($url);
die "OMG, WTF: Can't get $url --", $response->status_line unless $response->is_success;
}

getreplies();