I needed a script send an SMS today, and I found a very handy post at sudocode to send one via Google Voice, using PHP. I wanted to make it a little more command-line-friendly, so I rewrote it (ham-handedly) in Ruby and added some options parsing to it. It’s designed for — and only tested on — OS X, but may work fine elsewhere.
To use it, copy the code below into a text file, save it as voicesms.rb (or download it directly using the link at the top of the code), and run chmod a+x voicesms.rb from the command line to make it executable.
You can set default account name, password and send-to numbers at the top of the script. These can be overridden by command line options at runtime. The standard syntax is:
voicesms.rb -n +15552525543,+15554342221 -m “The message to send” -u GVoiceUsername:GVoicePassword
Parameters can be in any order. The -n parameter takes one or more phone numbers in international format (+XX country code at the beginning, +1 for US numbers), separated by commas (no spaces).
The -m option (message) is required and can’t be set by default in the script. Just use -m “and a quoted message” and it will handle the rest.
-u defines a username/password combination for Google Voice. It’s probably most convenient to set these at the top of the script and ignore this parameter, but the option is there.
-h will provide the help information.
The point of the script is to let me automate SMS messages from my system, so its primary invocation will be from other scripts. However, it will function just fine as a command line texting utility, in which case you’d probably want to alias the core functions in your .bash_profile. Set your default username and password in the script, and maybe a default destination number. If you want to send to a different numbers, you might want to make several aliases which include different -n # parameters, one for each destination. The alias will look something like:
alias sms=”~/scripts/voicesms.rb -m”
alias smsjohn=”~/scripts/voicesms.rb -n +15554443333 -m”
Get it? Then you can just type smsjohn "And your message" to send the message straight to John. Whoever that is.
What it does
It’s pretty simple, just a series of POST and GET requests to the Google API. It uses Google’s client authentication to get an initial auth code. Then it uses the auth code to get an auth token. Then, it posts your information to the API with the proper headers (the auth token) to complete the call. All of your data is sent securely over SSL (https) connections.