ievv_sms — SMS sending - multiple backends supported

Requirements

$ pip install gsm0338

Setup

Add it to your INSTALLED_APPS setting:

INSTALLED_APPS = [
    # Other apps ...
    'ievv_opensource.ievv_sms'
]

For development, you probably also want to use the ievv_opensource.ievv_sms.backends.debugprint.Backend backend. You configure that with the following setting:

IEVV_SMS_DEFAULT_BACKEND_ID = 'debugprint'

Sending SMS

Send an SMS using the default backend with:

from ievv_opensource.ievv_sms.sms_registry import send_sms
send_sms(phone_number='12345678', message='This is a test')

Send using another backend using the backend_id argument:

from ievv_opensource.ievv_sms.sms_registry import send_sms
send_sms(phone_number='12345678', message='This is a test',
         backend_id='some_backend_id')

See ievv_opensource.ievv_sms.sms_registry.send_sms() for more details.

Creating a custom backend

See the example in AbstractSmsBackend.

Specifying the default backend

Just set the backend ID (see get_backend_id()) of a backend in the IEVV_SMS_DEFAULT_BACKEND_ID setting.

Core API

send_sms()

ievv_opensource.ievv_sms.sms_registry.send_sms(phone_number, message, backend_id=None, **kwargs)[source]

Send SMS message.

Just a shortcut for Registry.send() (Registry.get_instance().send(...)).

Parameters
  • phone_number (str) – The phone number to send the message to.

  • message (str) – The message to send.

  • backend_id (str) – The ID of the backend to use for sending. If this is None, we use the default backend (see get_default_backend_id()).

  • **kwargs – Extra kwargs for the AbstractSmsBackend constructor.

Returns

An instance of a subclass of AbstractSmsBackend.

Return type

AbstractSmsBackend

AbstractSmsBackend

class ievv_opensource.ievv_sms.sms_registry.AbstractSmsBackend(phone_number, message, send_as=None, **kwargs)[source]

Base class for SMS backends.

An instance of this class is created each time an SMS is created.

This means that you can store temporary information related to building the SMS on self.

Example (simple print SMS backend):

class PrintBackend(sms_registry.AbstractSmsBackend):
    @classmethod
    def get_backend_id(cls):
        return 'print'

    def send(self):
        print(
            'Phone number: {phone_number}. '
            'Message: {message}'.format(
                phone_number=self.cleaned_phone_number,
                message=self.cleaned_message
            )
        )

To use the PrintBackend, add it to the registry via an AppConfig for your Django app:

from django.apps import AppConfig

class MyAppConfig(AppConfig):
    name = 'myapp'

    def ready(self):
        from ievv_opensource.ievv_sms import sms_registry
        from myapp import sms_backends
        sms_registry.Registry.get_instance().add(sms_backends.PrintBackend)

Now you can use the backend to send an SMS:

from ievv_opensource.ievv_sms import sms_registry
sms_registry.Registry.get_instance().send(
    phone_number='12345678',
    message='This is a test',
    backend_id='print')

You can also set the backend as the default backend for SMS sending by adding IEVV_SMS_DEFAULT_BACKEND_ID = 'print' to your django settings. With this setting you can call send() without the backend_id argument, and the SMS will be sent with the print backend.

All the arguments are forwarded from Registry.send() / Registry.make_backend_instance().

Parameters
  • phone_number (str) – The phone number to send the message to.

  • message (str) – The message to send.

  • **kwargs – Extra kwargs. Both for future proofing, and to make it possible for backends to support extra kwargs.

Registry

class ievv_opensource.ievv_sms.sms_registry.Registry[source]

Registry of AbstractSmsBackend objects.

Ensures there is only one instance created. Make sure to use super() in subclasses.

Backends

Debug/develop backends

PSWIN backend

DebugSmsMessage backend