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();

No comments:

Post a Comment