Академический Документы
Профессиональный Документы
Культура Документы
By marcwan I recently had the oppportunity (necessity) to set up a web application that interacted with many of the users through SMS messages in addition to the more traditional HTML interface. While there are a number of possible software solutions for GSM modems on Windows, on Unix-like platforms the most commonly used one is Kannel. It also has the advantage of being open source and thus very, very free. However, setting, configuring, and using Kannel tends to be a bit tricky. Im writing this article (almost a HOWTO) in an attempt to help out anybody whos undertaking the process themselves and might be able to get some tips and tricks from this. I expect this to not be a terribly popular article, but if I ever need to set this stuff up myself in the future, then Ill have it written down somewhere at least! Most of the instructions here will work on any Unix platform, such as Linux, FreeBSD, or Mac OS X. Its worth noting that I got nearly everything working with OS X, only to be thwarted at the very end because Macs no longer have serial ports to use GSM modems. You could, however, easily use some of the more advaned HTTP based SMS services on a Mac server of some sort.
The Scenario
The web site written was a discounting service oriented around cell phones. Users would send the site a message, and then receive a discount at local venues. They could go to the site on the intarwebs to see their total savings and learn about more venues. The usage pattern was always:
User sends us a message. We might send them a reply after processing it. We might also periodically need to be able to send the user a message for other purporses, such as party invitations, etc (based on their preferences).
# # # # #
I chose to install to /usr/local/kannel just because Im that kind of guy who likes to keep everything reasonably separated and organised. Youre free to put it anywhere. Compile and install.
# make # sudo make install password: **************
Configuration Files
You can be forgiven for thinking Kannel is trivially easy thus farit really is that easy to download, compile, and install. Unfortunately, here is where things get tricky. You now need to set up a configuration file. This file has a zillion options to support all of the possible and powerful ways in which Kannel can be used. I will be showing strictly how I set it up for the GSM modem we had in the office (Its a Siemens GSM modem connected to a serial port, and works quite well). The basic smskannel.conf (in the gw/ directory) has much of the information we want, but well need to add a few things for our GSM modems and to interact with our web server correctly. Configuration is divided into a few key groups, each representing the key parts of the kannel system, including the server that handles sending and receving the actual SMSes (bearerbox) and the system that handles the final dispatching to your scripts (smsbox).
Youll want to change the password of course, but everything else is nearly standard. We are assuming that all communication to the kannel server will come from the same physical computer (127.0.0.1). You can set a log file if you are going to be running kannel as a service on your server, or you can just redirect stdout to some file. Be aware that kannel has various log levels, ranging from 0, which displays information that is only of interest when youre in the development and debugging phases, to 4, which only displays critical errors and problems. I tend to develop at level 0 and run live servers at level 1. Disk space is cheap.
The my-number field contains the number of your GSM modems SIM chip. Again, I only allow connections from my local server, and the Ubuntu Linux serial port is on /dev/ttyS0.
The global-sender field is the outgoing-number of your GSM modem, which for me is the same as the my-number field above.
This group is what allows your web applications to send SMS messages using Kannel. They do this via simple HTTP requests, and configuration here basically requires a user name and password:
group = sendsms-user username = kanneluser password = df89asj89I23hvcxSDasdf3298jvkjc839 concatenation= true max-messages = 10
Since the password is semi-plain and unprotected here, I tend to use one that is complicated and nearly impossible to remember, but quite different from any other passwords that I actualy use for login accounts and the like.
This particular configuration has Kannel set up to use an HTTP GET request to send the message to my application. The param phone contains the phone number of the sender and the text parameter contains their entire message. NOTE: The max-messages value was particularly tricky and critical for me: When I first set up Kannel and tested sending messages, I would always get back '<Empty reply from service provider>'. Setting max-messages to 0 tells Kannel to never send a reply directly from the incoming message (you can, of course, initiate your own response later, of course).
# # # # #
I tend to run the last two commands in two separate shell windows when developing/debugging so that I can see the output from the two programs clearly and use the information to help me figure out whats going on (level 0 really tells you a lot).
Receiving Messages
Kannel will simply call the URL you told it to in the sms-service group and you can process this with whatever HTTP server environment you want. Were using LAMP right now, but, again, any will do. The incoming phone number and message are in GET parameters. You can, if you want, configure the sms-service to send them as POST messages as well.
function sendSmsMessage($in_phoneNumber, $in_msg) { $url = '/cgi-bin/sendsms?username=' . CONFIG_KANNEL_USER_NAME . '&password=' . CONFIG_KANNEL_PASSWORD . '&charset=UCS-2&coding=2' . "&to={$in_phoneNumber}" . '&text=' . urlencode(iconv('utf-8', 'ucs-2', $in_msg)); $results = file('http://' . CONFIG_KANNEL_HOST . ':'
. CONFIG_KANNEL_PORT . $url); }
To make this work, of course, you need to have allow_url_fopen set to On.
Thats It
Thats pretty much it. This has been a pretty dry article, but it does contain everything you need to get Kannel up and running and operational. The manual actually does contain everything you could possibly want to know, so keep digging in there if youre stuck. Finally, there are mailing lists at kannel.org which tend to be quite helpful as well.