iro/anbieter/smstrade.py
changeset 18 f0d31c70744d
parent 15 c04a21066aad
child 19 fcf8489f1c2f
equal deleted inserted replaced
17:44a3eda179b6 18:f0d31c70744d
    16 from sipgate import  NoValidStatusCode
    16 from sipgate import  NoValidStatusCode
    17 from telnumber import telnumber, NotATelNumber
    17 from telnumber import telnumber, NotATelNumber
    18 import ConfigParser
    18 import ConfigParser
    19 import xmlrpclib
    19 import xmlrpclib
    20 import base64
    20 import base64
       
    21 import gsm0338
    21 import urllib, httplib
    22 import urllib, httplib
    22 
    23 
    23 class UnknownStatusCode(Exception):
    24 class UnknownStatusCode(Exception):
    24     def __init__(self,code):
    25     def __init__(self,code):
    25         self.code=code
    26         self.code=code
    88     def sendSMS(self,sms,recipients):
    89     def sendSMS(self,sms,recipients):
    89         """send SMS with $sms to $recipients"""
    90         """send SMS with $sms to $recipients"""
    90         sended = []
    91         sended = []
    91         key = self.key
    92         key = self.key
    92         route = unicode(self.route)
    93         route = unicode(self.route)
    93         message = unicode(sms.content)
    94         message = sms.content
    94         from_ = unicode(self.from_)
    95         from_ = unicode(self.from_)
    95         timestamp = None
    96         timestamp = None
    96         for recipient in recipients:
    97         for recipient in recipients:
    97             try:
    98             try:
    98                 tel = telnumber(recipient)                
    99                 tel = telnumber(recipient)                
    99                 if tel in sended:                                             #only send message once per recipient
   100                 if tel in sended:                                             #only send message once per recipient
   100                     continue
   101                     continue
   101                 sended.append(tel)	
   102                 sended.append(tel)	
   102                 to = unicode((tel.number)).strip()						                
   103                 to = unicode(tel.land+tel.number).strip()
       
   104                 if tel.land == '49':
       
   105                     route=unicode("basic")
       
   106                 else:
       
   107                     route=unicode("economy")
   103                 smsSendStatus = self.__send(key, route, to, message, from_, timestamp)	
   108                 smsSendStatus = self.__send(key, route, to, message, from_, timestamp)	
   104                 if int(smsSendStatus) in(100, 999):
   109                 if int(smsSendStatus) in(100, 999):
   105                     self.updateStatus(arranged=recipient)
   110                     self.updateStatus(arranged=recipient)
   106                 else:
   111                 else:
   107                     self.updateStatus(failed=recipient)
   112                     self.updateStatus(failed=recipient)
   114         to the request."""
   119         to the request."""
   115         parameters= {"key": key,
   120         parameters= {"key": key,
   116                 "route": route,
   121                 "route": route,
   117                 "to": to,
   122                 "to": to,
   118                 "message": message,
   123                 "message": message,
       
   124                 "charset":"utf-8", 
   119                 "debug": self.debug,
   125                 "debug": self.debug,
   120                 }
   126                 }
   121         
   127         
   122         if from_ is not None:
   128         if from_ is not None:
   123             parameters["from"] = from_
   129             parameters["from"] = from_
   124         
   130         
   125         if timestamp is not None:
   131         if timestamp is not None:
   126             parameters["senddate"] = unicode(timestamp)
   132             parameters["senddate"] = unicode(timestamp)
   127 
   133 
   128         parameters["concat_sms"] = "1" if len(message) > 160 else "0"
   134         parameters["concat_sms"] = "1" if len(message) > 160 else "0"
   129 
   135         
   130         params = urllib.urlencode(dict([k, v.encode('iso-8859-1')] for k, v in parameters.items()))
   136         params = "&".join( ["%s=%s" % (urllib.quote(k),urllib.quote(v.encode("utf-8"))) for (k, v) in parameters.items()])
   131         headers = {"Content-type": "application/x-www-form-urlencoded",
   137         headers = {"Content-type": "application/x-www-form-urlencoded",
   132             "Accept": "text/plain"}
   138             "Accept": "text/plain"}
   133         conn = httplib.HTTPConnection("%s:%i" % (self.gateway, self.gatewayPort))
   139         conn = httplib.HTTPConnection("%s:%i" % (self.gateway, self.gatewayPort))
   134         try:
   140         try:
   135             conn.request(self.method, self.script, params, headers)
   141             conn.request(self.method, self.script, params, headers)
   136             response = conn.getresponse()
   142             response = conn.getresponse()
   137 
       
   138             data = response.read()
   143             data = response.read()
   139         except socket.gaierror:
   144         except socket.gaierror:
   140             raise InternetConnectionError("%s:%i" % (self.gateway, self.gatewayPort))
   145             raise InternetConnectionError("%s:%i" % (self.gateway, self.gatewayPort))
   141         else:
   146         else:
   142             conn.close()
   147             conn.close()
   143 
   148             
   144         try:
   149         try:
   145             return StatusCode(int(data))
   150             return StatusCode(int(data))
   146         except UnknownStatusCode:
   151         except UnknownStatusCode:
   147             # this happens if the sms will be send delayed
   152             # this happens if the sms will be send delayed
   148             return StatusCode(999)
   153             return StatusCode(999)