-
Notifications
You must be signed in to change notification settings - Fork 1
Creating an application
##Create our first Rapidsms application
RapidSMS applications are Django apps which contain custom logic for processing incoming and outgoing messages. When the router receives an incoming or outgoing message, it triggers a series of phases through which its associated applications can process the message. Any number of RapidSMS applications can be used in a project.
Each RapidSMS application defines a class that extends from rapidsms.apps.base.AppBase, kept in the ** app.py** submodule of a Django app. The Django app also contains models, views, and methods required by the application.
As an example, lets create an application which interact with the user. The user send JOIN and the application ansuers WELCOME TO RAPIDSMS and the user ansuers thank you or whatever so the system start for asking questions and the user ansuers them.
The models.py is designed as below:
A model is the single, definitive source of data about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table.
The basics:
- Each model is a Python class that subclasses django.db.models.Model._
- Each attribute of the model represents a database field._
- With all of this, Django gives you an automatically-generated database-access API;
for more information about Models and how to use them , visit https://docs.djangoproject.com/en/1.5/topics/db/models/
In models.py
from django.db import models
# Create your models here.
class Questions(models.Model):
question=models.CharField(max_length=50)
def __unicode__(self):
return self.question
class History(models.Model):
tel_num=models.CharField(max_length=20)
question=models.CharField(max_length=50)
response=models.CharField(max_length=50)
status=models.IntegerField(default=0)
def __unicode__(self):
return self.tel_num
After designing the model.py, this is the time then for designing the admin.py which will help to edit questions, to displays all asked questions and their responses into the system. Often, you’ll want to customize how the admin form looks and works. You’ll do this by telling Django the options you want when you register the object.
in admin.py we have:
from django.contrib import admin
from sendsms.models import Questions
#admin.site.register(Questions)
class ConvAdmin(admin.ModelAdmin):
#fields = ['question', 'reponse']
fields = ['question']
admin.site.register(Questions, ConvAdmin)
For more information about Django Admin form, go to https://docs.djangoproject.com/en/1.5/intro/tutorial02/
Now lets start with the custom logic which handles the messages. App.py:
from rapidsms.apps.base import AppBase
from sendsms.models import Questions, History
class App(AppBase):
def handle(self, message):
tel=message.peer
msg=message.text.split(" ")
if msg[0]=='JOIN':
for q in Questions.objects.all():
h=History(question=q.question,tel_num=tel,status=0)
h.save()
message.respond("WELCOME TO RAPIDSMS")
return True
elif History.objects.filter(tel_num=tel):
for q in (History.objects.filter(tel_num=tel)):
if(q.status==1):
q.reponse=message.text
q.status=2
q.save()
if(q.status==0):
q.status=1
q.save()
quest=q.question
message.respond(" %s " %(quest))
return True
message.respond("CONGRATULATION! YOU ARE REGISTERED IN OUR SYSTEM !")
return True
else:
message.respond("SORY YOU ARE NOT REGISTERED! TO REGISTER YOURSELF SEND JOIN !")
return True
If you wrote well this code or if it is semantically correct,your Rapidsms app works very well.
NOTE
Each app must follow the standards,coding ethics, coding efficiency which are:
- Easy of extension
- Easy to read
- Easy to customize
- Code efficient
- Easy to understand
- Tested
- Multi Language Support
Notice that for now, only the extension,understandability, readable are concerned. We will treat further the remaining coding ethics.