This is My Blog

2009/03/15
|

Extracting meta-data from SMPP TLV’s with Kannel

I have been relatively quiet the past few weeks with various operations going on, but seeing as I have a few minutes to spare I thought I would do a quick post explaining how to extract SMPP TLV’s using the new Kannel meta data features which are in the current CVS branch.

These are scheduled for the 1.5.0 stable release of Kannel, so for now you’ll need to do a CVS checkout of the main branch (as described in my previous posts).

I will be demonstrating how to set a test TLV and then extracting this data when a reply is received. Once you have setup your ‘smpp-tlv’ group, you simply need to specify an additional parameter in your smsbox request. This parameter is named ‘meta-data’. The format for this variable is ‘?<bind_type>?<dataset>’ currently the only supported bind type is ‘smpp’ but it was done this way to allow future support.

The dataset argument, is a URL encoded key/value pair string. So for these tests I have configured a smpp-tlv group called ‘my-custom-var’. In order to set this, I need to build a string like this for meta-data purposes.


?smpp?my-custom-var=This+is+a+cool+var

As you can see my-custom-var is set as a URL encoded variable, this will still need to be URL-encoded for submission in the meta-data parameter sent to smsbox.

So just to demonstrate, using PHP how you would build this string, you would use.

$bind_type = "smpp";
$my_custom_var = "This is a cool var";
$meta_data = "?".$bind_type."?my-custom-var=".urlencode($my_custom_var);
/* Now we need to urlencode this string */
$meta_data = urlencode($meta_data);
/* So URL would look like */
/* cgi-bin/sendsms?to=...&from=...&meta-data=$meta_data */

So now, when you set this to smsbox, it will be sent via the smpp bind as a TLV. Easy!

Now on the converse, if your smpp bind returns a TLV in a deliver_sm or other PDU, this will also passed to your URL (if you have configured it. Here below is a basic function for parsing the meta data out of this string.

/* This function will take the meta-data string and return an associative array */
function parse_metadata($var) {
        $pos = strpos($var, "?", 1) + 1;
        /* Simply stripping out bind type 'smpp', this can be modified if you need it :) */
        $var = substr($var, $pos);
        $data = parse_str($var, $out);
        return $out;
}

/* Example: */
$meta_data = parse_metadata($_GET['meta-data']);

echo "My custom var = ".$meta_data['my-custom-var'];

I hope this helps you with your SMPP TLV requirements. Good luck, don’t forget to email me if you need any assistance with Kannel/SMS, etc and I will do my best to help you as best I can.

Technorati Tags: , , ,

Tags: , , ,

2 Comments

  1. john

    2009/10/14

    How if
    ?smpp?my-custom-var=This+is+a+cool+var&my-other-var=This+also+need

    How modify to get my-other-var

  2. Donald Jackson

    2009/12/28

    If you have configured the TLV correctly in Kannel, you should be able to retrieve it from the array like: $meta_data['my-other-var'];


Would you like to share your thoughts?

Would you like to share your thoughts?

Leave a Reply