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

21 Apr 2008

Image Rotator 1.0

I was working on the TCE Website today and the white space between the "nav bar" and the "contact us" section was looking empty.

I thought it would be nice if visitors could have a random image each time they loaded a page, this would require a nice little PHP script I wrote called Image Rotator.

The way this works is thus, firstly you setup a directory containing a pool of images, then you give them the same filename with a key after it. For example image0.png, image1.png, image2.png ... The script then displays one of those images at random whenever it is executed.

Now for the source


// Image Rotator 1.0
// James Januszka 2008
// http://jims-tech.blogspot.com

// Variables
$dir="images"; // Directory containing images
$files = scandir($dir);
$string = "sidepic"; // Prefix of each image
$extention ="jpg"; // Image Filetype
$alt = "Closeup of Food"; // Alt text for the image
$width = 180; // Width of image in pixels
$height = 250; // Height of image in pixels

foreach ($files as $file) // Loop through each file in the directory
{

if ($file=ereg($string, $file)) // Regular expression to see if the filename contains $string
{
$j =$j+1;

}

}
$count = $j -1; // Ugly Hack; the rand() function generates a number between 0 and $count

srand(time());
$random = (rand()%$count);
$image="$dir/$string$random.$extention"; // Generates a path to the image(directory+prefix+random+extention)
echo("*img src='$image' alt='$alt' width=$width height=$height*"); //Displays image ;) (*I had to remove the
angle brackets from this line so blogger would display it!)


It may look complicated at first, but its really simple. At the top of the code I define the variables used in the script, most of these are do to with the rendering of HTML code that displays the images. Next it scans through the image directory and builds an array containing the images, finally it displays an image at random.

One of the cool things about the script is this routine;

foreach ($files as $file) // Loop through each file in the directory
{

if ($file=ereg($string, $file)) // Regular expression to see if the filename contains $string
{
$j =$j+1;

}

}

It scans through the directory and uses a regular expression to find and count how many files are prefixed with $string, ignoring all other files in the directory, it needs to know how many pictures there are for the pseudo-random generator to work.

The final part of this script, shown below, generates a pseudo-random number and uses that to pick and display an image.

srand(time());
$random = (rand()%$count);
$image="$dir/$string$random.$extention"; // Generates a path to the image(directory+prefix+random+extention)
echo("*img src='$image' alt='$alt' width=$width height=$height*"); //Displays image ;) (*I had to remove the
angle brackets from this line so blogger would display it!)

Well thats the script, you are free to use it as you will, and you can make any changes that you desire, just as long as you credit me.

17 Apr 2008

Candle Printer

I just came across an interesting product from China. Candle Printer Model UN-SO-M01. It's a printer that can print your own designs onto candles. It connects to your PC via a USB cable.

This product is really going to appeal to the arts and crafts crowd, currently there are no importers of this or similar products in the West. Spotting a gap in the market, I approached Sinounic with a view to importing a few samples to flog on Ebay.

The response I got was enthusiastic, they showed be some specs, and I must say I thought I had something pretty hot here. Everything was going fine until we came to the price, 1600USD per unit. Even if I negotiated it would still be ludicrous.

Oh well I reckon the big boys are going to pick this up, and by Christmas it will be in all those crappy gadget booklets you get in the Sunday papers.