Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

SMS from Perl using HTTP request

by JacobAnderson (Initiate)
on Oct 17, 2012 at 13:31 UTC ( #999542=CUFP: print w/ replies, xml ) Need Help??

Hey guys. For my project, I needed to find a way to send SMS messages from Perl. It turns out, it is easy to send messages from Perl using HTTP requestes and a software called Ozeki NG SMS Gateway.

To send SMS messages from Perl using HTTP requests, first you need to download, install and configure Ozeki NG SMS Gateway software to your computer. Then import the source code provided below into a new project you write in Perl. After you imported it you can configure the code. You need to customize the values in the source code below.

#!/usr/bin/perl ############################################### ## Ozeki NG - SMS Gateway Perl example ## ############################################### use HTTP::Request; use LWP::UserAgent; use URI::Escape; ############################################### ### Ozeki NG informations ### ############################################### $host = "127.0.0.1"; $port = "9501"; $username = "admin"; $password = "abc123"; $recipient = "+00123456"; $message = "Test Message from Perl"; ############################################### ### Putting together the final HTTP Request ### ############################################### $url = "http://" . $host; $url .= ":" . $port; $url .= "/api?action=sendmessage&"; $url .= "username=" . uri_escape($username); $url .= "&password=" . uri_escape($password); $url .= "&recipient=" . uri_escape($recipient); $url .= "&messagetype=SMS:TEXT"; $url .= "&messagedata=" . uri_escape("HELLO WORLD"); ################################################ #### Sending the message ### ################################################ $request = HTTP::Request->new(GET=>$url); $useragent = LWP::UserAgent->new; $response = $useragent->request($request); ################################################ ### Verifying the response ### ################################################ if ($response->is_success) { print "Message successfully sent" } else { print "Message not sent! Please check your settings!" }

You can find more info here: http://ozekisms.com/index.php?owpn=608 I hope this will be as useful to you as it is for me! Jacob

Comment on SMS from Perl using HTTP request
Download Code
Re: SMS from Perl using HTTP request
by blue_cowdawg (Monsignor) on Oct 17, 2012 at 14:15 UTC

    This is good stuff.

    Most pages (SMS targets, whatever you want to call them) usually these days at least in the USA have an email address associated with them. Typically in the form of ##########@provider.tld which allows you to use normal email mechanisms to deliver SMS. That said, SMS is limited to 140 characters so whatever scripting you do needs to have logic to break up long messages into chunks of 140 characters or less.

    Most of the Nagios installations I've installed make use of that mechanism.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: SMS from Perl using HTTP request
by jmlynesjr (Friar) on Oct 17, 2012 at 16:11 UTC

    Works great via email also. Try phonenumber@txt.att.net, @mms.att.net, @tmomail.net, @vmobl.com, @cingularme.com, @messaging.sprintpcs.com, @vtext.com, or @messaging.nextel.com. Email addresses from the October 2012 issue of Nuts and Volts magazine.

    James

Re: SMS from Perl using HTTP request
by Your Mother (Canon) on Oct 17, 2012 at 17:17 UTC

    Here it is tarted up a bit. Semi-tested. Let me know if the license is not appropriate. Refs: strictures, LWP::UserAgent, URI, Getopt::Long, and Pod::Usage.

    #!/usr/bin/env perl use strictures; use LWP::UserAgent; use URI; use Getopt::Long; use Pod::Usage; GetOptions( help => \my $help, "host=s" => \my $host, "port=i" => \my $port, "user=s" => \my $user, "password=s" => \my $password, "recipient=s" => \my $recipient, "message=s" => \my $message, ); pod2usage( -verbose => 2 ) if $help; $host ||= "127.0.0.1"; $port ||= 9501; $user ||= $ENV{OZEKI_NG_SMS_USER}; $password ||= $ENV{OZEKI_NG_SMS_PASSWORD}; pod2usage( -verbose => 2, -message => "\nYou're missing required arguments.\n" ) unless $recipient and $user and $password; my $size = $message ? length $message : 0; pod2usage( -verbose => 0, -message => "\nMessage is required, up to 140 characters.\n +" ) unless $size > 0 and $size <= 140; my $endpoint = URI->new("/api"); $endpoint->scheme("http"); $endpoint->host($host); $endpoint->port($port); $endpoint->query_form( action => "sendmessage", username => $user, password => $password, recipient => $recipient, messagetype => "SMS:TEXT", messagedata => $message ); my $response = LWP::UserAgent ->new( agent => "OHAIbot/3.14") ->get($endpoint); if ( $response->is_success ) { print "Successfully sent.\n"; } else { print join "\n\n", "Nice going...", $endpoint, $response->decoded_content; } exit 0; __END__ =pod =head1 Name Ozeki NG - SMS Gateway Perl example. =head1 Prerequisite Install Ozeki NG SMS Gateway, L<http://ozekisms.com>. =head1 Synopsis ./ozeki-ng-sms -user ME -pass S3CR37 -r "1234567890" -m "OHAI DER" =head2 Options C<user>, C<password>, C<recipient>, and C<message> are required. -help -host # Default is 127.0.0.1 -port # Default is 9501 -user # or set OZEKI_NG_SMS_USER -password # or set OZEKI_NG_SMS_PASSWORD -recipient # SMS number -message # Up to 140 characters =head1 See also L<http://perlmonks.org/?node_id=999542>. =head1 License Artistic 2.0. =cut

    Update, added prereq to Pod.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://999542]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2014-12-25 03:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (159 votes), past polls