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
# http://jasminsms.com
import urllib2
import urllib

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

# Send an SMS-MT with minimal parameters
urllib2.urlopen("http://127.0.0.1:1401/send?%s" % urllib.urlencode(baseParams)).read()

# Send an SMS-MT with defined originating address
baseParams['from'] = 'Jasmin GW'
urllib2.urlopen("http://127.0.0.1:1401/send?%s" % urllib.urlencode(baseParams)).read()

In PHP:

<?php
// Sending simple message using PHP
// http://jasminsms.com

$baseurl = 'http://127.0.0.1:1401/send'

$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
# http://jasminsms.com

require 'net/http'

uri = URI('http://127.0.0.1:1401/send')
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):

<?php
// Receiving simple message using PHP through HTTP Post
// This example will store every received SMS to a SQL table
// http://jasminsms.com


$MO_SMS = $_POST;

$db = pg_connect('host=127.0.0.1 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']), 
                     pg_escape_string($MO_SMS['from']), 
                     pg_escape_string($MO_SMS['to']), 
                     pg_escape_string($MO_SMS['origin-connector']), 
                     pg_escape_string($MO_SMS['priority']), 
                     pg_escape_string($MO_SMS['coding']), 
                     pg_escape_string($MO_SMS['validity']), 
                     pg_escape_string($MO_SMS['content'])
                     );
pg_query($Q);
pg_close($db);

// 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:

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

$MO_SMS = $_POST;

// 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";
else
    $RESPONSE = $martches[2]." forecast: Sunny 21°C, 13Knots NW light wind";

// Send $RESPONSE back to the user ($MO_SMS['from'])
$baseurl = 'http://127.0.0.1:1401/send'
$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.

Routing

c.f. MO router manager and MT router manager for routing scenarios. c.f. The message router for details about routing.