Created
February 23, 2011 11:57
-
-
Save jgorset/840333 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
from httplib import HTTPConnection | |
from base64 import b64encode | |
from xml.etree import ElementTree | |
from django.conf import settings | |
class GateBackend: | |
"""Aspiro Gate Backend.""" | |
@classmethod | |
def send(self, recipient, sender, price, country, message): | |
""" | |
Send an SMS and return initial delivery status code. | |
Delivery status codes: | |
201 -- Message is received and is being processed. | |
202 -- Message is aknowledged by operator and is being processed. | |
203 -- Message is billed and delivered. | |
204 -- Message is billed. | |
299 -- Temporary undefined status. Message is being processed. | |
901 -- Customer has too low credit, the message cannot be delivered. | |
902 -- The message expired and could not be delivered. | |
903 -- The customers number is temporarily barred. | |
904 -- The phone number is permanently barred. | |
905 -- The number is barred from overcharged messages. | |
906 -- The correct operator for phone number could not be found and the message could not be delivered. | |
907 -- The message is billed and the operator confirms billing, but an error occurred when trying to deliver the message. | |
909 -- There was a problem with the message format, operator rejected the message. | |
910 -- Customer is too young to receive this content. | |
911 -- The customer's oprator was not correct, trying to resend the message to the correct operator. | |
912 -- User balance has been reached for the shortcode, and operator denies more overcharged messages. | |
913 -- Service rejected by the user. | |
951 -- User is blacklisted due to request from Operator or User. Message cannot be sent to user. | |
999 -- General error. Covers all errors not specified with a unique error code. | |
See Gate API Documentation: http://merlin.aspiro.com/avalon-doc/api/gate-api.pdf | |
""" | |
credentials = '%s:%s' % (settings.GATE_USERNAME, settings.GATE_PASSWORD) | |
headers = {'Authorization': 'Basic %s' % b64encode(credentials)} | |
body = '<?xml version="1.0" encoding="UTF-8" ?>' | |
body += '<gate>' | |
body += ' <targetNumber>%s</targetNumber>' % recipient | |
body += ' <senderNumber>%s</senderNumber>' % sender | |
body += ' <country>%s</country>' % country | |
body += ' <price>%s</price>' % price | |
body += ' <sms>' | |
body += ' <content><![CDATA[%s]]></content>' % message | |
body += ' </sms>' | |
body += '</gate>' | |
connection = HTTPConnection('www.mobile-entry.com', timeout=10) | |
connection.request('POST', '/gate/service', body, headers) | |
response = connection.getresponse() | |
try: | |
xml = ElementTree.XML(response) | |
return xml.find('status').text | |
except: | |
raise StandardError('API response malformed') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment