Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

You're sitting at a table with your cow-orkers while the head beancounter goes through his drone about how the bar on the chart for last quater is clearly larger than the bar for the quarter before, and you're starting to wonder if you can slip a certain almond-tasting chemical into the beancounter's coffee.

No worry. You covertly slip your 802.11-enabled PDA out of your pocket and browse to your web server, where you've kept the CGI below for this very situation. Not long after, you receive an SMS on your cell telling you about some emergency or other. You glance at your phone, bolt out of the room, and take an early lunch.

This CGI is for use under mod_perl2, but it shouldn't be hard to modify for regular CGI use.

The send_sms() routine gets a string containing the message to send, and returns true on success, false on failure. It's coded to use Sprint's web form for sending the message. You'll have to change it for your provider. See also: WWW::SMS.

The constant MESSAGES is an arrayref containing the messages to send. Be sure they are <= 160 chars (SMS limit). You can set which message to send with the 'emg' parameter ("emg=0" is the first message, "emg=1" is the second, etc.). The first message is used by default.

You'll need to replace the TO and FROM constants with your phone number.

#!/usr/bin/perl use strict; use warnings; use constant TO => 'xxxx'; use constant FROM => 'xxxx'; use constant MAX_CHARS => 160; use constant PAGE => ' +mpose'; use constant AGENT => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.5) Gecko/20041118 +' . 'Firefox/1.0'; use constant MESSAGES => [ # Each message must be <= 160 chars (SMS l +imit) 'Run away', ]; sub send_sms { my $msg = shift; use WWW::Mechanize; my $mech = WWW::Mechanize->new; $mech->get( PAGE ); $mech->submit_form( form_name => 'composeForm', fields => { phoneNumber => TO, callBackNumber => FROM, message => $msg, characters => MAX_CHARS - length($msg), }, ); return $mech->success(); } { use Apache::RequestUtil; my $r = Apache->request; my %in = $r->args(); my $msg_num = $in{emg} || 0; send_sms( substr( MESSAGES->[$msg_num], 0, 160 ) ) or die "Couldn't send message\n"; $r->content_type( 'text/plain' ); $r->print( MESSAGES->[$msg_num] ); }

Update: Added ikegami's change to make sure send_sms() never gets a message over 160 chars.

Update (2): Added readmore tags.

"There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

In reply to Get Out of a Dull Meeting by hardburn

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (6)
    As of 2021-05-09 13:52 GMT
    Find Nodes?
      Voting Booth?
      Perl 7 will be out ...

      Results (100 votes). Check out past polls.