iro/anbieter/smstrade.py
changeset 25 7295e27e8c99
parent 23 0180b538ed74
child 37 6e5bd561ddd0
equal deleted inserted replaced
24:2119564de7a9 25:7295e27e8c99
    18 import ConfigParser
    18 import ConfigParser
    19 import xmlrpclib
    19 import xmlrpclib
    20 import base64
    20 import base64
    21 import gsm0338
    21 import gsm0338
    22 import urllib, httplib
    22 import urllib, httplib
       
    23 
       
    24 import logging
       
    25 logger=logging.getLogger("smstrade")
    23 
    26 
    24 class UnknownStatusCode(Exception):
    27 class UnknownStatusCode(Exception):
    25     def __init__(self,code):
    28     def __init__(self,code):
    26         self.code=code
    29         self.code=code
    27 
    30 
    86         self.from_=cp.get(self.section, 'from')
    89         self.from_=cp.get(self.section, 'from')
    87         self.debug=cp.get(self.section, 'debug')
    90         self.debug=cp.get(self.section, 'debug')
    88 
    91 
    89     def sendSMS(self,sms,recipients):
    92     def sendSMS(self,sms,recipients):
    90         """send SMS with $sms to $recipients"""
    93         """send SMS with $sms to $recipients"""
    91         import logging
    94         logger.debug('smstrade.sendSMS(%s,%s)'%(sms,  str(recipients)))
    92         logging.debug('smstrade.sendSMS(%s,%s)'%(sms,  str(recipients)))
       
    93         sended = []
    95         sended = []
    94         key = self.key
       
    95         route = unicode(self.route)
    96         route = unicode(self.route)
    96         message = sms.content
    97         message = sms.content
    97         from_ = unicode(self.from_)
       
    98         timestamp = None
    98         timestamp = None
    99         for recipient in recipients:
    99         for recipient in recipients:
   100             try:
   100             try:
   101                 tel = telnumber(recipient)                
   101                 tel = telnumber(recipient)                
   102                 if tel in sended:                                             #only send message once per recipient
   102                 if tel in sended:                                             #only send message once per recipient
   105                 to ='00'+tel.land+tel.number
   105                 to ='00'+tel.land+tel.number
   106                 if tel.land == '49':
   106                 if tel.land == '49':
   107                     route=unicode("basic")
   107                     route=unicode("basic")
   108                 else:
   108                 else:
   109                     route=unicode("economy")
   109                     route=unicode("economy")
   110                 smsSendStatus = self.__send(key, route, to, message, from_, timestamp)	
   110                 smsSendStatus = self.__send(route, to, message, timestamp)	
       
   111                 logger.debug('smstrade._send(...)=%i(%s)'%(int(smsSendStatus),str(smsSendStatus)))
   111                 if int(smsSendStatus) in(100, 999):
   112                 if int(smsSendStatus) in(100, 999):
   112                     self.updateStatus(arranged=recipient)
   113                     self.updateStatus(arranged=recipient)
   113                 else:
   114                 else:
   114                     self.updateStatus(failed=recipient)
   115                     self.updateStatus(failed=recipient)
   115             except (NotATelNumber,NoValidStatusCode,InternetConnectionError):
   116             except (NotATelNumber,NoValidStatusCode,InternetConnectionError):
   116                 self.updateStatus(failed=recipient)
   117                 self.updateStatus(failed=recipient)
   117 
   118 
   118     def __send(self, key, route, to, message, from_=None, timestamp=None):
   119     def __send(self, route, to, message,  timestamp=None):
   119         """ This function is the main part of the request to the sms service.    
   120         """ This function is the main part of the request to the sms service.    
   120         The function has to return a unicode formated string that will represent the answer of the sms service
   121         The function has to return a unicode formated string that will represent the answer of the sms service
   121         to the request."""
   122         to the request."""
   122         import logging
   123         logger.debug('smstrade._send(%s,%s,%s,%s)'%( route, to, message, timestamp))
   123         logging.debug('smstrade._send(%s,%s,%s,%s,%s,%s)'%( key, route, to, message, from_,  timestamp))
   124         parameters= {"key": self.key,
   124         parameters= {"key": key,
       
   125                 "route": route,
   125                 "route": route,
   126                 "to": to,
   126                 "to": to,
   127                 "message": message,
   127                 "message": message,
   128                 "charset":"utf-8", 
   128                 "charset":"utf-8", 
   129                 "debug": self.debug,
   129                 "debug": self.debug,
   130                 }
   130                 }
   131         
   131         
   132         if from_ is not None:
   132         if self.from_ is not None:
   133             parameters["from"] = from_
   133             parameters["from"] = self.from_
   134         
   134         
   135         if timestamp is not None:
   135         if timestamp is not None:
   136             parameters["senddate"] = unicode(timestamp)
   136             parameters["senddate"] = unicode(timestamp)
   137 
   137 
   138         parameters["concat_sms"] = "1" if len(message) > 160 else "0"
   138         parameters["concat_sms"] = "1" if len(message) > 160 else "0"
   139         params = "&".join( ["%s=%s" % (urllib.quote(k),urllib.quote(v.encode("utf-8"))) for (k, v) in parameters.items()])
   139         params = "&".join( ["%s=%s" % (urllib.quote(k),urllib.quote(v.encode("utf-8"))) for (k, v) in parameters.items()])
   140         logging.debug('smstrade._send-parameters:%s\n\t->%s'%(str(parameters), str(params)) )
   140         logger.debug('smstrade._send-parameters:%s\n\t->%s'%(str(parameters), str(params)) )
   141         headers = {"Content-type": "application/x-www-form-urlencoded",
   141         headers = {"Content-type": "application/x-www-form-urlencoded",
   142             "Accept": "text/plain"}
   142             "Accept": "text/plain"}
   143         conn = httplib.HTTPConnection("%s:%i" % (self.gateway, self.gatewayPort))
   143         conn = httplib.HTTPConnection("%s:%i" % (self.gateway, self.gatewayPort))
   144         try:
   144         try:
   145             conn.request(self.method, self.script, params, headers)
   145             conn.request(self.method, self.script, params, headers)