Programming examples

Subsequent chapters present how to send and receive messages through Jasmin HTTP API and some more advanced use cases, such as manipulating receipts and complex routings, will look like.

It is assumed the reader has already installed Jasmin and at least read the HTTP API and The message router chapters and knows enough about Jasmin’s architecture/design concepts.

Sending SMS

Sending a SMS is done through the HTTP API:

# Python example
import urllib2
import urllib

baseParams = {'username':'foo', 'password':'bar', 'to':'+336222172', 'content':'Hello'}

# Send an SMS-MT with minimal parameters
urllib2.urlopen("" % urllib.urlencode(baseParams)).read()

# Send an SMS-MT with defined originating address
baseParams['from'] = 'Jasmin GW'
urllib2.urlopen("" % urllib.urlencode(baseParams)).read()


// Sending simple message using PHP

$baseurl = ''

$params = '?username=foo'
$params.= '&password=bar'
$params.= '&to='.urlencode('+336222172')
$params.= '&content='.urlencode('Hello world !')

$response = file_get_contents($baseurl.$params);

In Ruby:

# Sending simple message using Ruby

require 'net/http'

uri = URI('')
params = { :username => 'foo', :password => 'bar',
           :to => '+336222172', :content => 'Hello world' }
uri.query = URI.encode_www_form(params)

response = Net::HTTP.get_response(uri)

c.f. HTTP API for more details about sending SMS with receipt enquiry, long content etc ...

Receiving SMS

Receiving a SMS is done through the HTTP API, this a PHP script pointed by Jasmin for every received SMS (using routing):

// Receiving simple message using PHP through HTTP Post
// This example will store every received SMS to a SQL table


$db = pg_connect('host= port=5432 dbname=sms_demo user=jasmin password=jajapwd');
if (!$db)
    // We'll not ACK the message, Jasmin will resend it later
    die("Error connecting to DB");

$QUERY = "INSERT INTO sms_mo(id, from, to, cid, priority, coding, validity, content) ";
$QUERY.= "VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');";

$Q = sprintf($QUERY, pg_escape_string($MO_SMS['id']), 

// Acking back Jasmin is mandatory
echo "ACK/Jasmin";

In the above example, there’s an error handling where the message is not ACKed if there’s a database connection problem, if it occurs, the script will return “Error connecting to DB” when Jasmin HTTP thrower is waiting for a “ACL/Jasmin”, this will lead to a message re-queue and later re-delivery to the same script, this behaviour is explained in Processing.

Another example of an interactive SMS application:

// Will filter received messages, if the syntax is correct (weather <city name>)
// it will provide a `fake` weather forecast back to the user.


// Acking back Jasmin is mandatory
echo "ACK/Jasmin";

// Syntax check
if (!preg_match('/^(weather) (.*)/', $MO_SMS['content'], $matches))
    $RESPONSE = "SMS Syntax error, please type 'weather city' to get a fresh weather forecast";
    $RESPONSE = $martches[2]." forecast: Sunny 21°C, 13Knots NW light wind";

// Send $RESPONSE back to the user ($MO_SMS['from'])
$baseurl = ''
$params = '?username=foo'
$params.= '&password=bar'
$params.= '&to='.urlencode($MO_SMS['from'])
$params.= '&content='.urlencode($RESPONSE)

$response = file_get_contents($baseurl.$params);

// Note:
// If you need to check if the message is really delivered (or at least, taken by Jasmin for delivery)
// you must test for $response value, it must begin with "Success", c.f. HTTP API doc for more details

c.f. HTTP API for more details.