Pages

Friday, March 4, 2011

How to receive and save SMS through Kannel

This is the relevant configuration part of the kannel.conf file;

group = sms-service
keyword = default
#keyword-regex = .*
catch-all = yes
max-messages = 0
get-url = "http://localhost:23456/sms.php?ORIGINATOR=%p&RECEIVER=%P&SMS=%a&SOURCE_PRV=%o"



At local host (same as where i run my kannel server) i have this code in the file sms.php;

$query = "insert into sms set sender='".$_GET["ORIGINATOR"]."', receiver='".$_GET["RECEIVER"]."', msg='".$_GET["SMS"]."', source_prv='".$_GET["SOURCE_PRV"]."'";
$result = mysql_query($query);
if(!$result) die(sql_error());

Make sure that you have defined your database connectivity parameters before this code. What kannel does is just he receive a SMS it calls the URL defined as the get-url in the .conf file. And passing the parameters in GET method. Now you are able to receive and save a SMS through kannel.

28 comments:

  1. Hello, thanks for this. However I'm having a bit of trouble. Can you help me?

    Thanks

    ReplyDelete
  2. Sure if it is something I can help you...

    ReplyDelete
    Replies
    1. plz tell me in sms service group how can i use keyword-regex for sending my keyword with my desired message e-g my key is
      WIKI Q1: how we achieve data encapsulation?

      how i can set it in keyword-regex .

      Delete
  3. hello., can i ask something? how will i define my database? i did a database using user guide in kannel. it goes like this.

    group = mysql-connection
    id = mydlr
    host = localhost
    username = root
    password = passw0rd
    database = db_kannel
    max-connections = 1

    group = dlr-db
    id = mydlr
    table = tbl_dlr
    field-smsc = smsc
    field-timestamp = timestamp
    field-destination = destination
    field-source = source
    field-service = service
    field-url = url
    field-mask = mask
    field-status = status
    field-boxc-id = boxc

    is this correct?

    ReplyDelete
    Replies
    1. You mean the database for saving the message?

      Delete
    2. Defining the database is a PHP work. Please refer some online tutorials for database connectivity according to your DB (mysql, mssql, etc..) .
      For kannel it need only four columns... sender, receiver, message and source_prv. Message date, time can be defined if want. They are simply just PHP coding.
      And somebody had asked me about message filtering. Do it in PHP level. I don’t think you can do it in kannel.

      Delete
  4. plz tell what is localhost:23456 ? is this phpmyadmin port adress?
    i also want send msg what will be php code

    ReplyDelete
    Replies
    1. Where you are using this port? at kannel? Phpmyadmin just runes on the normal web port (80 for http 443 for https)
      If this is something related to your sms gateway, at your kannel.conf file you can define the port number which you will be using to communicate with kannel.

      And here is the sample php code that you can send sms through kannel

      file_get_contents('http://127.0.0.1:13013/cgi-bin/sendsms?username=yourkannelusername&password=yourkannelpw&to='.$phone_num.'&text=your+text+here');

      127.0.0.1 is just localhost.(Apache and Kannel both running on the same server.)
      13013 is the default port for kannel

      Delete
    2. Refer this too on your port issue,

      http://www.speedguide.net/port.php?port=23456

      Delete
  5. thanks but i need little help sir.

    ReplyDelete
  6. Sir i have face problem when implement MO type message .
    how receive message in to my application from mobile.
    I understand the kennele configuration but in get-url there are some parameters these parameter how to get the url.
    Is this process use SMPP or HTPP .
    and how I send sms from mobile to port and also how I test it.

    ReplyDelete
    Replies
    1. You must connect your sim card through an interface (in my case I use an USB dongle). It will do the modem part for you. Then what kannel will do is access it same like you read your smses through your computer by connecting your mobile phone and put it to your database. So then you dont have to worry about the protocol, your modem or the dongle will take care of it. :)

      Delete
  7. This is one of the best tutorials I have come across the internet on setting up kannel. Thanks a lot. Can you write a sample code about recieving sms into mysql database. It will be very helpful

    ReplyDelete
  8. hello sir...i just want to ask if there's another way how can i save the received message continuously??i mean every time i text the system the kannel will automatically save the message in the database...

    ReplyDelete
    Replies
    1. Yes.. you dont have to implement any other thing. This process is automated. Every time kannel receives a message, it will call the URL automatically. So then you must be able to write a PHP code to get the parameters from GET method and write it to the mysql (or whatever) database.

      Delete
  9. Hello Suhada! Amazing article you have here, very helpful. I have this little problem hope you can help. Not sure what's the problem, so please refer to the config-group

    group = sms-service
    # keyword = default
    # keyword-regex = .*
    get-url="http://admin.kannel.com/index.php?c=sms&m=receive&from=%p&to=%P&text=%a&rtime=%t"
    catch-all = true
    max-messages = 0
    concatenation = true

    ReplyDelete
  10. When you receive a SMS Kannel will call the URL in get-url. So it should be your own page with the coding to retrieve the message and save on DB, file etc..

    ReplyDelete
  11. Hello sir , i got cms error 321 "memory out of index " ,cant access the message. please can you help me on this.

    ReplyDelete
    Replies
    1. CMS errors are from your modem, not from your Kannel GW. Please refer below guide,
      http://www.ozekisms.com/index.php?owpn=380
      You may try changing your modem or addressing the issue by referring to the vendor manual.

      Delete
  12. This comment has been removed by the author.

    ReplyDelete
  13. Hi Suhada, is it possible to do this for a symbian OS phone e.g. a Nokia X2 phone?

    ReplyDelete
  14. I've got an SMSC, SMSC simulator and a Kennel being set up. I'm now able to send a message as an http request from my Java program to the SMSC. What I want to know is that, how can I receive an sms from SMSC, in Kennel?

    ReplyDelete
    Replies
    1. Forgot to mention in the above post, I don't need to save anything in my db. How can I check a message has been received in Kennel (from SMSC) ?

      Delete
  15. Bonjour j'ai un souci avec kannel l'orque j'envoie le mot à partir de mon téléphone kannel ne me retourne rien

    ReplyDelete
  16. Bonjour j'ai un souci avec kannel l'orque j'envoie le mot à partir de mon téléphone kannel ne me retourne rien

    ReplyDelete