Teach Your Raspberry Pi To Send SMS Messages

Voipbuster logo Here's a short shell script which enables you to send SMS messages to any mobile phone number. This is done with a little help from Voipbuster, which provides a simple API for us to use.

First of all you'll have to sign up for an account at Voipbuster and deposit some money on your account. They offer plenty of payment options, there's bound to be one among them which works best for you. The cheapest option will costs only just over one Euro. After that you can send text messages to just about any mobile phone number in the world for just a few cents per SMS, until your credit runs out. Don't worry about obligations, there simply aren't any.
If you've got a VoIP enabled modem, phone, or application on your computer you can call many land line destinations for free up to 120 days as a bonus. Fees to non free destinations are fairly low too. Consult the rates pages for details. However this is not Raspberry Pi related, consider it to be a bonus for using the SMS API.

#!/bin/sh

# sendsms
#
# Sends an SMS to a phone number through Voipbuster.
# Use:
#      sendsms +3161234567 "Message to be sent"
#
# Author: San Bergmans
#         www.sbprojects.net
#

# Configuration variables
USER="voipbuster_username"
PASS="voipbuster_password"
FROM="from"

guide ()
{
    echo "Usage:   $0 phone_number sms text"
    echo "Example: $0 +31612345678 This text is sent\n"
}

# Parameter $1 is the destination number in international notation
if [ $# -eq 0 ]; then
    echo "No parameters given"
    guide
    exit 1
else
    TO=$1
fi

# The rest of the parameter string is used as SMS text
shift
SMS_TEXT="$*"
SMS_TEXT=`echo $SMS_TEXT|cut -b -160`

if [ "$SMS_TEXT" = "" ]; then
    echo "No text message given"
    guide
    exit 1
fi

# Form the URL, including constants and parameters
URL="https://www.voipbuster.com/myaccount/sendsms.php?username=$USER&password=$PASS&from=$FROM&to=\"$TO\"&text=$SMS_TEXT"

# Send SMS and print the result string from the XML file returned by voipbuster
wget -q "$URL" -O - | grep resultstring | sed -e 's,<resultstring>,,' -e 's,</resultstring>,,' -e 's," ",a,' -e 's,\t,,'

There are three configuration variables in this script. USER is the Voipbuster user name you have used to sign up with. And PASS is the accompanying password. The FROM variable can be used as caller ID. For that to work you should connect your own mobile number to your Voipbuster account and prove that it is really yours.

To use the script simply call it, with the destination phone number as first parameter. This phone number must be in international notation. The rest of the command line can hold your text message, which may be up to 160 characters long. Longer messages will be truncated to 160 characters. Example: sendsms +3161234567 "Message to be sent"
Keep in mind that some symbols have special meanings for the Linux command shell. For instance lines containing a single quote, which are quite common in the English language, may be very confusing for Linux. Therefore you best use double quotes to delimit your text message to avoid confusion.

The API takes a couple of seconds to respond. And when it does respond it sends an XML reply. This reply is filtered by the script to extract the result string, which can be either success or failure. You can be sure that sending the SMS failed if the result string says failure. However success doesn't mean that the message has been sent correctly. You may send a text message to a non existent phone number for example and still receive a success reply.

Room For Improvement

I've got a few ideas on how to improve this script if you want to. You may add a log feature to the script, which writes a time stamp, the destination phone number, the sent message and the API reply to a log file.

You can also add a list of phone numbers you often use. Then you can make the script accept a name, instead of a phone number. It's the script's job to translate this name into a phone number before it can send the message.

Another thing you can add to the script is a way to send one message to multiple phone numbers. Or split longer messages into multiple 160 character long messages.

This is your chance to practice your scripting skills.