Build an SMS Service with Infobip

ibires (Installation) is a set of building blocks to

You want this if you want to get your code right before sending your first SMS.

ibires is short for InfoBIp REport Simulator.

Overview

  1. Write the test.
  2. Get your function right until the test passes.
  3. Your function will work with the real server as the test resembles the server’s functionality.

You

  • don’t risk to send any real SMS (as long you don’t forget @responses.activate)
  • don’t need an account with Infobip.
  • don’t need a web connection.

Since your test passes, you can safely assume that your function will work out of the box from the very first SMS on (presuming your login and balance permit).

En Détail

import responses
from ibires.core import send
from ibires.simulate import sms_server

def send_messages(messages, connection):
    for to, text in messages.items():
        response = send(to, text, **connection)
        assert response['status']['id'] == 7

@responses.activate
def test_myfun():
    messages = {
                '919999999999': 'Green apples are no oranges',
                '919999999998': 'It is raining now!',
                '919999999997': 'Don\'t forget the homework!'
               }

    # first define connecting options
    # leave this username if you want to simulate only
    connection = {
                  'url': 'https://api.infobip.com/sms/1/text/single',
                  'user': 'Aladdin',
                  'password': 'open sesame',
                  'sender': '919999999999'
                  }

    # this and the @responses.activate decorator simulate
    # the behaviour of https://api.infobip.com/sms/1/text/single
    sms_server(**connection)

    # MAIN CALL
    send_messages(messages, connection)
    
    # here wo don't use any assert statement as such
    # it is sufficient that an error will be raised if
    # we don't get a HTTP 200 response

Run py.test to see whether the test passes.

If it does, your function send_messages() is likely to run without any glitches right from the very first message.

Access the Report Generators Directly

You may want to generate the report’s directly:

from ibires.reports import gen_accepted, gen_dlr

# The report you get when you post the message goes like:
>>> gen_accepted(error = False)

{'messages': [{'messageId': '68506e2f-f6fb-48bb-ae26-f342ccfd0e16',
   'smsCount': 1,
   'status': {'description': 'Message sent to next instance',
    'groupId': 1,
    'groupName': 'PENDING',
    'id': 7,
    'name': 'PENDING_ENROUTE'},
   'to': '919999999999'}]}

# The final delivery report (either we pull it or it is pushed to a URI):
>>> gen_dlr(delivered = True)

{'results': [{'doneAt': '2016-10-04 12:14:24.079+0200',
   'error': {'description': 'No error',
    'groupId': 0,
    'groupName': 'OK',
    'id': 0,
    'name': 'NO_ERROR',
    'permanent': False},
   'messageId': 'be2606e9-087f-4885-87bb-5d0fa10acd1f',
   'price': {'currency': 'EUR', 'pricePerMessage': 0.01},
   'sentAt': '2016-10-04 12:14:24.079+0200',
   'smsCount': 1,
   'status': {'description': 'Message delivered to operator',
    'groupId': 3,
    'groupName': 'DELIVERED',
    'id': 2,
    'name': 'DELIVERED_TO_OPERATOR'},
   'to': '919999999999'}]}

# Oh, here, things have gone wrong:
>>> gen_dlr(delivered = False)

{'results': [{'doneAt': '2016-10-04 12:14:24.079+0200',
   'error': {'description': 'Operator error occured.',
    'groupId': 3,
    'groupName': 'OPERATOR_ERRORS',
    'id': 20,
    'name': 'EC_SS_INCOMPATIBILITY',
    'permanent': False},
   'messageId': '598b5d9b-328f-42c9-9b08-adc7baa18b56',
   'price': {'currency': 'EUR', 'pricePerMessage': 0.01},
   'sentAt': '2016-10-04 12:14:24.079+0200',
   'smsCount': 1,
   'status': {'description': 'Route not available',
    'groupId': 5,
    'groupName': 'REJECTED',
    'id': 19,
    'name': 'REJECTED_ROUTE_NOT_AVAILABLE'},
   'to': '919999999999'}]}

Indices and tables