iro/anbieter/smstrade.py
changeset 23 0180b538ed74
parent 22 8b363361ba55
child 37 6e5bd561ddd0
equal deleted inserted replaced
22:8b363361ba55 23:0180b538ed74
    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         route = unicode(self.route)
    96         route = unicode(self.route)
    95         message = sms.content
    97         message = sms.content
    96         timestamp = None
    98         timestamp = None
    97         for recipient in recipients:
    99         for recipient in recipients:
   104                 if tel.land == '49':
   106                 if tel.land == '49':
   105                     route=unicode("basic")
   107                     route=unicode("basic")
   106                 else:
   108                 else:
   107                     route=unicode("economy")
   109                     route=unicode("economy")
   108                 smsSendStatus = self.__send(route, to, message, timestamp)	
   110                 smsSendStatus = self.__send(route, to, message, timestamp)	
       
   111                 logger.debug('smstrade._send(...)=%i(%s)'%(int(smsSendStatus),str(smsSendStatus)))
   109                 if int(smsSendStatus) in(100, 999):
   112                 if int(smsSendStatus) in(100, 999):
   110                     self.updateStatus(arranged=recipient)
   113                     self.updateStatus(arranged=recipient)
   111                 else:
   114                 else:
   112                     self.updateStatus(failed=recipient)
   115                     self.updateStatus(failed=recipient)
   113             except (NotATelNumber,NoValidStatusCode,InternetConnectionError):
   116             except (NotATelNumber,NoValidStatusCode,InternetConnectionError):
   115 
   118 
   116     def __send(self, route, to, message,  timestamp=None):
   119     def __send(self, route, to, message,  timestamp=None):
   117         """ 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.    
   118         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
   119         to the request."""
   122         to the request."""
   120         import logging
   123         logger.debug('smstrade._send(%s,%s,%s,%s)'%( route, to, message, timestamp))
   121         logging.debug('smstrade._send(%s,%s,%s,%s)'%( route, to, message, timestamp))
       
   122         parameters= {"key": self.key,
   124         parameters= {"key": self.key,
   123                 "route": route,
   125                 "route": route,
   124                 "to": to,
   126                 "to": to,
   125                 "message": message,
   127                 "message": message,
   126                 "charset":"utf-8", 
   128                 "charset":"utf-8", 
   133         if timestamp is not None:
   135         if timestamp is not None:
   134             parameters["senddate"] = unicode(timestamp)
   136             parameters["senddate"] = unicode(timestamp)
   135 
   137 
   136         parameters["concat_sms"] = "1" if len(message) > 160 else "0"
   138         parameters["concat_sms"] = "1" if len(message) > 160 else "0"
   137         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()])
   138         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)) )
   139         headers = {"Content-type": "application/x-www-form-urlencoded",
   141         headers = {"Content-type": "application/x-www-form-urlencoded",
   140             "Accept": "text/plain"}
   142             "Accept": "text/plain"}
   141         conn = httplib.HTTPConnection("%s:%i" % (self.gateway, self.gatewayPort))
   143         conn = httplib.HTTPConnection("%s:%i" % (self.gateway, self.gatewayPort))
   142         try:
   144         try:
   143             conn.request(self.method, self.script, params, headers)
   145             conn.request(self.method, self.script, params, headers)