def handle_car(self, message, parser): bad_format = _('bad-car-format', "Error, car message must take the form: car [license plate]") license_plate = parser.next_word(bad_format) if len(license_plate) != 8: raise ParseException(_('car-invalid-license', "Invalid license plate, it must be 8 letters")) # otherwise, associate this connection and car car = Car.register_connection(license_plate, message.connection) return _('car-add-success', "You are now reporting mileage for the car with license {{ license_plate }}", dict(license_plate=car.license_plate))
def handle_miles(self, message, parser): bad_format = _('bad-miles-format', "Error, mileage report message must take the form: miles [mileage]") miles = parser.next_int(bad_format) # look up whether this connection has a car car = Car.for_connection(message.connection) if not car: _('miles-no-car', "You are not registered to be using a car, send: car [license plate] to register") # add mileage car.add_mileage(miles, message.connection) return _('miles-success', "You have recorded {{ miles }} miles for the car with license {{ license_plate }}", dict(miles=miles, license_plate=car.license_plate))
def handle(self, message): # activate our default language translation.activate(settings.DEFAULT_SMS_LANGUAGE) # if the number looks like something informational or SPAM, ignore identity = message.connection.identity # first case, alpha num identity try: number = int(identity) except: # ignore this message, the sender isn't numeric return False # now test that it is 10 digits or more, ignore anything shorter if len(identity) < 10: return False # ok sender looks ok, let's get on with it try: response = self.handle_message(message) if response: message.respond(response) else: # pragma: no cover message.respond( _( 'unrecognized', "That message is not recognized, must start with miles or car" )) except ParseException as e: message.respond(str(e)) except: import traceback traceback.print_exc() message.respond( _( 'error', "An unexpected error occurred, please check your message and try again" )) return True
def handle_car(self, message, parser): bad_format = _( 'bad-car-format', "Error, car message must take the form: car [license plate]") license_plate = parser.next_word(bad_format) if len(license_plate) != 8: raise ParseException( _('car-invalid-license', "Invalid license plate, it must be 8 letters")) # otherwise, associate this connection and car car = Car.register_connection(license_plate, message.connection) return _( 'car-add-success', "You are now reporting mileage for the car with license {{ license_plate }}", dict(license_plate=car.license_plate))
def handle_message(self, message): """ Messages have the following keywords: 'miles' or 'car' """ parser = Parser(message.text) keyword = parser.next_keyword(KEYWORDS, _('unrecognized', "That message is not recognized, must start with miles or car")) if keyword in ['car', 'c']: return self.handle_car(message, parser) elif keyword in ['miles', 'm']: return self.handle_miles(message, parser)
def handle_miles(self, message, parser): bad_format = _( 'bad-miles-format', "Error, mileage report message must take the form: miles [mileage]" ) miles = parser.next_int(bad_format) # look up whether this connection has a car car = Car.for_connection(message.connection) if not car: _( 'miles-no-car', "You are not registered to be using a car, send: car [license plate] to register" ) # add mileage car.add_mileage(miles, message.connection) return _( 'miles-success', "You have recorded {{ miles }} miles for the car with license {{ license_plate }}", dict(miles=miles, license_plate=car.license_plate))
def handle (self, message): # activate our default language translation.activate(settings.DEFAULT_SMS_LANGUAGE) # if the number looks like something informational or SPAM, ignore identity = message.connection.identity # first case, alpha num identity try: number = int(identity) except: # ignore this message, the sender isn't numeric return False # now test that it is 10 digits or more, ignore anything shorter if len(identity) < 10: return False # ok sender looks ok, let's get on with it try: response = self.handle_message(message) if response: message.respond(response) else: # pragma: no cover message.respond(_('unrecognized', "That message is not recognized, must start with miles or car")) except ParseException as e: message.respond(str(e)) except: import traceback traceback.print_exc() message.respond(_('error', "An unexpected error occurred, please check your message and try again")) return True
def handle_message(self, message): """ Messages have the following keywords: 'miles' or 'car' """ parser = Parser(message.text) keyword = parser.next_keyword( KEYWORDS, _('unrecognized', "That message is not recognized, must start with miles or car")) if keyword in ['car', 'c']: return self.handle_car(message, parser) elif keyword in ['miles', 'm']: return self.handle_miles(message, parser)
from rapidsms.apps.base import AppBase from .models import * from nsms.text import gettext as _ from nsms.parser import Parser, ParseException from rapidsms.models import Backend, Connection from django.utils import translation from rapidsms_httprouter.router import get_router from django.template import Template, Context from django.conf import settings import re KEYWORDS = [ 'm', 'miles', 'c', 'car' ] _('unrecognized', "That message is not recognized, must start with miles or car") _('car-invalid-license', "Invalid license plate, it must be 8 letters") _('car-add-success', "You are now reporting mileage for the car with license {{ license_plate }}") _('bad-car-format', "Error, car message must take the form: car [license plate]") _('miles-no-car', "You are not registered to be using a car, send: car [license plate] to register") _('miles-success', "You have recorded {{ miles }} for the car with license {{ license_plate }}") _('bad-miles-format', "Error, mileage reports must take the form: miles [mileage]") _('error', "An unexpected error occurred, please check your message and try again") class App(AppBase): def handle (self, message): # activate our default language translation.activate(settings.DEFAULT_SMS_LANGUAGE) # if the number looks like something informational or SPAM, ignore
def handle(self, message): profile = get_sms_profile(message.connection) # handle activating the appropriate SMS language # if this connection has a profile, use that language if profile: translation.activate(profile.language) # otherwise use the default SMS language else: translation.activate(settings.DEFAULT_SMS_LANGUAGE) parser = Parser(message.text) # check whether this is a lang message keyword = parser.next_keyword(['lang']) if keyword: if not profile: message.respond( _('lang-unknown-user', "Sorry, your phone number is not registered.")) return True # just querying the language language = parser.next_word() if not language: message.respond( _('lang-current-lang', "Your language is set to {{ language }}.", dict(language=profile.get_language_display()))) return True else: # lowercase the language they passed us language = language.lower() lang_mapping = dict() for lang in settings.LANGUAGES: code = lang[0].lower() lang_mapping[code] = code parts = code.split('_') if not parts[0] in lang_mapping: lang_mapping[parts[0]] = code # this language doesn't exit if not language.lower() in lang_mapping: message.respond( _( 'lang-unknown-language', "Sorry, the language code '{{ code }}' is not supported.", dict(code=language.lower()))) return True profile.language = lang_mapping[language.lower()] profile.save() # activate the new language translation.activate(profile.language) message.respond( _('lang-set-success', "Success, your language is now set to {{ language }}.", dict(language=profile.get_language_display()))) return True return False
from rapidsms.apps.base import AppBase from nsms.parser import Parser from nsms.text import gettext as _ from nsms.utils import get_sms_profile from django.conf import settings from django.utils import translation # hackery to make sure our translation database contains all our the strings used in this app _('lang-current-lang', "Your language is set to {{ language }}.") _('lang-unknown-language', "Sorry, the language code '{{ code }}' is not supported.") _('lang-set-success', "Success, your language is now set to {{ language }}.") class App(AppBase): """ This app is responsible for setting and changing a user's preferred language. In order for this app to do it's magic, you need to define what your SMS profile in your settings_common.py: SMS_PROFILE = 'chws.chw' The language app will respond to messages that begin with the keyword 'lang'. Without an argument it will respond with the currently selected language, with an argument it will try to find a configured language with that name and set it, eg: lang en[_us] lang rw """ def handle(self, message):
def handle (self, message): """ Tries to look up a profile for the user and set the default language. Also takes care of handing the 'lang' keyword """ profile = get_sms_profile(message.connection) # handle activating the appropriate SMS language # if this connection has a profile, use that language if profile: translation.activate(profile.language) # otherwise use the default SMS language else: translation.activate(settings.DEFAULT_SMS_LANGUAGE) parser = Parser(message.text) # check whether this is a lang message keyword = parser.next_keyword(['lang']) if keyword: if not profile: message.respond(_('lang-unknown-user', "Sorry, your phone number is not registered.")) return True # just querying the language language = parser.next_word() if not language: message.respond(_('lang-current-lang', "Your language is set to {{ language }}.", dict(language=App.get_language_display(profile.language)))) return True else: # lowercase the language they passed us language = language.lower() lang_mapping = dict() for lang in settings.LANGUAGES: code = lang[0].lower() lang_mapping[code] = code parts = code.split('_') if not parts[0] in lang_mapping: lang_mapping[parts[0]] = code # this language doesn't exit if not language.lower() in lang_mapping: message.respond(_('lang-unknown-language', "Sorry, the language code '{{ code }}' is not supported.", dict(code=language.lower()))) return True profile.language = lang_mapping[language.lower()] profile.save() # activate the new language translation.activate(profile.language) message.respond(_('lang-set-success', "Success, your language is now set to {{ language }}.", dict(language=App.get_language_display(profile.language)))) return True return False
def handle(self, message): return message.respond( _( 'help', "Unrecognized message, please contact your supervisor for more information" ))
from rapidsms.apps.base import AppBase from nsms.text import gettext as _ import re _('help', "Unrecognized message, please contact your supervisor for more information") class App(AppBase): """ This app is responsible for returning a help message for any message which reaches it. This makes sense for applications which have a shortcode dedicated to them. """ def handle(self, message): return message.respond( _( 'help', "Unrecognized message, please contact your supervisor for more information" ))
def handle(self, message): return message.respond(_("help", "Unrecognized message, please contact your supervisor for more information"))
from rapidsms.apps.base import AppBase from nsms.text import gettext as _ import re _("help", "Unrecognized message, please contact your supervisor for more information") class App(AppBase): """ This app is responsible for returning a help message for any message which reaches it. This makes sense for applications which have a shortcode dedicated to them. """ def handle(self, message): return message.respond(_("help", "Unrecognized message, please contact your supervisor for more information"))
from rapidsms.apps.base import AppBase from nsms.parser import Parser from nsms.text import gettext as _ from nsms.utils import get_sms_profile from django.conf import settings from django.utils import translation # hackery to make sure our translation database contains all our the strings used in this app _('lang-current-lang', "Your language is set to {{ language }}.") _('lang-unknown-language', "Sorry, the language code '{{ code }}' is not supported.") _('lang-set-success', "Success, your language is now set to {{ language }}.") class App(AppBase): """ This app is responsible for setting and changing a user's preferred language. In order for this app to do it's magic, you need to define what your SMS profile in your settings_common.py: SMS_PROFILE = 'rapidsms.models.Contact' The language app will respond to messages that begin with the keyword 'lang'. Without an argument it will respond with the currently selected language, with an argument it will try to find a configured language with that name and set it, eg: lang en[_us] lang rw """ @classmethod def get_language_display(cls, lang):
from rapidsms.apps.base import AppBase from .models import * from nsms.text import gettext as _ from nsms.parser import Parser, ParseException from rapidsms.models import Backend, Connection from django.utils import translation from rapidsms_httprouter.router import get_router from django.template import Template, Context from django.conf import settings import re KEYWORDS = ['m', 'miles', 'c', 'car'] _('unrecognized', "That message is not recognized, must start with miles or car") _('car-invalid-license', "Invalid license plate, it must be 8 letters") _( 'car-add-success', "You are now reporting mileage for the car with license {{ license_plate }}" ) _('bad-car-format', "Error, car message must take the form: car [license plate]") _( 'miles-no-car', "You are not registered to be using a car, send: car [license plate] to register" ) _( 'miles-success', "You have recorded {{ miles }} for the car with license {{ license_plate }}" )