Trace: rapidsms

RapidSMS and Kannel

Install RapidSMS

python3 -m venv $HOME/rapidsms
source $HOME/rapidsms/bin/activate
pip install rapidsms
$ cd $HOME
$ virtualenv $HOME/smsbot-venv
$ source $HOME/smsbot-venv/bin/activate
$ pip install Django
$ django-admin.py startproject --template=https://github.com/rapidsms/rapidsms-project-template/zipball/release-0.21.1 --extension=py,rst smsbot
$ cd $HOME/smsbot
$ pip install -r requirements/base.txt

Comment these lines of $HOME/smsbot/settings.py:

(...)
INSTALLED_APPS = (
    # "rapidsms.contrib.echo",
(...)
 RAPIDSMS_HANDLERS = (
    # 'rapidsms.contrib.echo.handlers.echo.EchoHandler',
    # 'rapidsms.contrib.echo.handlers.ping.PingHandler',
(...)

Setup database:

$ python manage.py syncdb
$ python manage.py migrate

Install Kannel

I have a SIM800C based GSM Module for my RPi, if you have one maybe you need to read first this to get start, Kannel also will work with more common USB modems.

Install Kannel:

$ sudo apt-get install kannel kannel-extras

Configure Kannel default value in file /etc/default/kannel

#START_WAPBOX=0
START_SMSBOX=1

Configure the file /etc/kannel/kannel.conf with this content, you need to change the configure following your requeriments:

 CONFIGURATION FOR USING SMS KANNEL WITH RAPIDSMS
#
# For any modifications to this file, see Kannel User Guide
# If that does not help, see Kannel web page (http://www.kannel.org) and
# various online help and mailing list archives
#
# Notes on those who base their configuration on this:
#  1) check security issues! (allowed IPs, passwords and ports)
#  2) groups cannot have empty rows inside them!
#  3) read the user guide

include = "/etc/kannel/modems.conf"

#---------------------------------------------
# CORE
#
# There is only one core group and it sets all basic settings
# of the bearerbox (and system). You should take extra notes on
# configuration variables like 'store-file' (or 'store-dir'),
# 'admin-allow-ip' and 'access.log'

group = core
admin-port = 13000
smsbox-port = 13001
admin-password = CHANGE-ME
status-password = CHANGE-ME
admin-deny-ip = "*.*.*.*"
admin-allow-ip = "127.0.0.1"
box-deny-ip = "*.*.*.*"
box-allow-ip = "127.0.0.1"
log-file = "/var/log/kannel/bearerbox.log"
log-level = 0

#---------------------------------------------
# SMSC CONNECTIONS
#
# SMSC connections are created in bearerbox and they handle SMSC specific
# protocol and message relying. You need these to actually receive and send
# messages to handset, but can use GSM modems as virtual SMSCs

# Here is a sample SMSC for use with the /usr/lib/kannel/test/fakesmsc command

group = smsc
smsc = at
smsc-id = piojos
my-number = +506CENSORED
modemtype = auto
device = /dev/ttyAMA0

#---------------------------------------------
# SMSBOX SETUP
#
# Smsbox(es) do higher-level SMS handling after they have been received from
# SMS centers by bearerbox, or before they are given to bearerbox for delivery

group = smsbox
bearerbox-host = 127.0.0.1
sendsms-port = 13013
sendsms-chars = "0123456789 +-"
log-file = "/var/log/kannel/smsbox.log"
log-level = 0
access-log = "/var/log/kannel/smsbox-access.log"
reply-couldnotfetch = "Your message could not be processed at this time.  Please try again later. (err=couldnotfetch)"
reply-requestfailed = "Your message could not be processed at this time.  Please try again later. (err=requestfailed)"
reply-couldnotrepresent = "Your message could not be processed at this time.  Please try again later. (err=couldnotrepresent)"
http-request-retry = 3
http-queue-delay = 10

# SEND-SMS USERS
#
# These users are used when Kannel smsbox sendsms interface is used to
# send PUSH sms messages, i.e. calling URL like
# http://kannel.machine:13013/cgi-bin/sendsms?username=tester&password=foobar...

# This is the username and password that RapidSMS uses to deliver SMSes to
# Kannel.  It must also set the 'smsc' variable in the query string, so that
# Kannel knows which SMSC to use to route the message.

group = sendsms-user
username = rapidsms
password = CHANGE-ME
user-deny-ip = "*.*.*.*"
user-allow-ip = "127.0.0.1;"

#---------------------------------------------
# SERVICES
#
# These are 'responses' to sms PULL messages, i.e. messages arriving from
# handsets. The response is based on message content. Only one sms-service is
# applied, using the first one to match.

# The 'ping-kannel' service let's you check to see if Kannel is running,
# even if RapidSMS is offline for some reason.

group = sms-service
keyword = ping-kannel
text = "Kannel is online and responding to messages."

# There should be always a 'default' service. This service is used when no
# other 'sms-service' is applied.  These relay incoming messages from any
# configured SMSCs to the appropriate HTTP backend URLs in RapidSMS.
# By setting 'accepted-smsc', we are assured that messages are routed to
# the appropriate backend in RapidSMS.

group = sms-service
keyword = default
catch-all = yes
accepted-smsc = piojos
# don't send a reply here (it'll come through sendsms):
max-messages = 0
get-url = http://192.168.1.12:8000/backend/kannel-piojos/?id=%p&text=%a&charset=%C&coding=%c

Copy modem configuration file and restart Kannel:

$ sudo cp /usr/share/doc/kannel/examples/modems.conf /etc/kannel/
$ sudo systemctl restart kannel

Reconfigure RapidSMS

Edit $HOME/smsbot/smsbot/settings.py to allow RapidSMS fetch SMS:

INSTALLED_BACKENDS = {
#    "message_tester": {
#        "ENGINE": "rapidsms.backends.database.DatabaseBackend",
#    },
    "kannel-piojos" : {
        "ENGINE":  "rapidsms.backends.kannel.KannelBackend",
        "sendsms_url": "http://127.0.0.1:13013/cgi-bin/sendsms",
        "sendsms_params": {"smsc": "piojos",
                           "from": "+506CENSORED", # not set automatically by SMSC
                           "username": "rapidsms",
                           "password": "CHANGE-ME"}, # or set in localsettings.py
        "coding": 0,
        "charset": "ascii",
        "encode_errors": "ignore", # strip out unknown (unicode) characters
        "delivery_report_url": "http://192.168.1.12:8000",

    },
}

And configure file $HOME/smsbot/smsbot/urls.py:

    url(r"^backend/kannel-piojos/$",
        KannelBackendView.as_view(backend_name="kannel-piojos")),

Now configure RapidSMS to send delivery reports, configure file $HOME/smsbot/smsbot/settings.py:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
    'django.contrib.humanize',
    'django.contrib.sitemaps',
    # External apps
    "django_tables2",
    "selectable",
    # RapidSMS
    "rapidsms",
    "rapidsms.backends.database",
    "rapidsms.contrib.handlers",
    "rapidsms.contrib.httptester",
    "rapidsms.contrib.messagelog",
    "rapidsms.contrib.messaging",
    "rapidsms.contrib.registration",
    "rapidsms.backends.kannel",
    #"rapidsms.contrib.echo",
    "rapidsms.contrib.default",  # Must be last
)

And edit $HOME/smsbot/smsbot/urls.py:

    url(r'^kannel/', include('rapidsms.backends.kannel.urls')),

Start RapidSMS

$ cd $HOME/smsbot
$ python manage.py runserver 192.168.1.12:8000

You can enter to http://192.168.1.12:8000

Maybe you want to start automatically the service with systemd, you can read this to get ready

Change default response

Edit the file $HOME/smsbot/smsbot/settings.py and add this line:

DEFAULT_RESPONSE = "Parece que me llego un mensaje que no entiendo"

Test it

You can send a SMS with word ping, no uppercase to the SIM number, you will get a pong response, in web interface you will see the log of received and outgoing SMS. If you send any other word you will receive a SMS with “Parece que me llego un mensaje que no entiendo”.

Throubleshooting

You can make researching in log files:

$ sudo tail -f /var/log/kannel/bearerbox.log
$ sudo tail -f /var/log/kannel/smsbox.log

If you read this message in /var/log/kannel/bearerbox.log:

2014-11-21 10:41:55 [1912] [6] INFO: AT2[/dev/ttyACM0]: opening device
2014-11-21 10:41:55 [1912] [6] ERROR: AT2[/dev/ttyACM0]: open failed! ERRNO=13
2014-11-21 10:41:55 [1912] [6] ERROR: System error 13: Permission denied
2014-11-21 10:41:55 [1912] [6] ERROR: AT2[/dev/ttyACM0]: at2_device_thread: open_at2_device failed.
2014-11-21 10:41:55 [1912] [6] ERROR: System error 13: Permission denied
2014-11-21 10:41:55 [1912] [6] ERROR: AT2[/dev/ttyACM0]: Couldn't connect (retrying in 10 seconds).

And can't receive or send SMS, follow this steps:

$ sudo usermod -a -G dialout kannel
$ sudo chown  kannel  /dev/ttyACM0

References

rapidsms.txt · Last modified: 2023/09/13 14:27
Public Domain Except where otherwise noted, content on this wiki is licensed under the following license: Public Domain