Mike Silverman documenting innovation at work

16Nov/110

Sending Text Messages with Perl and Google Voice

It is much easier to send a SMS text message with Perl than you might think. You wouldn't know unless you searched but there is a module out there called Google::Voice. The module allows you to use your Google Voice account from Perl. It's not installed by default with Perl so you will need to install it. The easiest way to do this would be to type

perl -MCPAN -e "install Google::Voice"

This will install Google::Voice and all of it's dependencies.

Now to write a text message is so simple!

use Google::Voice;

#Google login info
my $username    = 'myuser@host.com';
my $password    = "mypasword";

#text message info
my $send_number = "14443216789";
my $send_text   = "I'm a text message!";

#Do Not Edit Below Here!

#create Google::Voice object and login
my $gv_obj = Google::Voice->new->login($username, $password);

#send the text!
$gv_obj->send_sms($send_number => $send_text);

Now say you want to send a text message by calling the script from outside Perl. i.e.

sendtext.pl -p 14443216789 -t "I'm a text message!"

Make sure the text is surrounded by quotes otherwise you will only be sending the first word.

The following code does just that.

use Google::Voice;
use Getopt::Long;
use strict;

#Set your login info here!
my $username    = 'myuser@host.com';
my $password    = "mypasword";

#Do Not Edit Below Here!

my ($help_opt, $phone_opt, $text_opt);
GetOptions("h|help"     => \$help_opt,
	   "p|phone=s"  => \$phone_opt,
           "t|text=s"   => \$text_opt,
          );
die <<HELP_MSG
sendtext.pl -p <phone_number> -t <text message>

usage:  -h     this help message
        -p     phone number you want to send to
        -t     text message to send
HELP_MSG
    #show help message if user types -h or does not include
    #phone number or text message text
    if (! $phone_opt or ! $text_opt or $help_opt);

#create Google::Voice object and login
my $gv_obj = Google::Voice->new->login($username, $password);

#send the text!
$gv_obj->send_sms($phone_opt => $text_opt);

There are other methods available in Google::Voice to allow you to interact with the service. I recommend reading the documentation.

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

Trackbacks are disabled.