iro/anbieter/smstrade.py
changeset 20 0d7ffb9b2c7f
parent 19 fcf8489f1c2f
child 21 e6302069d772
equal deleted inserted replaced
19:fcf8489f1c2f 20:0d7ffb9b2c7f
    86         self.from_=cp.get(self.section, 'from')
    86         self.from_=cp.get(self.section, 'from')
    87         self.debug=cp.get(self.section, 'debug')
    87         self.debug=cp.get(self.section, 'debug')
    88 
    88 
    89     def sendSMS(self,sms,recipients):
    89     def sendSMS(self,sms,recipients):
    90         """send SMS with $sms to $recipients"""
    90         """send SMS with $sms to $recipients"""
       
    91         import logging
       
    92         logging.debug('smstrade.sendSMS(%s,%s)'%(sms,  str(recipients)))
    91         sended = []
    93         sended = []
    92         key = self.key
    94         key = self.key
    93         route = unicode(self.route)
    95         route = unicode(self.route)
    94         message = sms.content
    96         message = sms.content
    95         from_ = unicode(self.from_)
    97         from_ = unicode(self.from_)
    98             try:
   100             try:
    99                 tel = telnumber(recipient)                
   101                 tel = telnumber(recipient)                
   100                 if tel in sended:                                             #only send message once per recipient
   102                 if tel in sended:                                             #only send message once per recipient
   101                     continue
   103                     continue
   102                 sended.append(tel)	
   104                 sended.append(tel)	
   103                 to = unicode(tel.land+tel.number).strip()
   105                 to ='00'+tel.land+tel.number
   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(key, route, to, message, from_, timestamp)	
   110                 smsSendStatus = self.__send(key, route, to, message, from_, timestamp)	
   115 
   117 
   116     def __send(self, key, route, to, message, from_=None, timestamp=None):
   118     def __send(self, key, route, to, message, from_=None, timestamp=None):
   117         """ This function is the main part of the request to the sms service.    
   119         """ 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
   120         The function has to return a unicode formated string that will represent the answer of the sms service
   119         to the request."""
   121         to the request."""
       
   122         import logging
       
   123         logging.debug('smstrade._send(%s,%s,%s,%s,%s,%s)'%( key, route, to, message, from_,  timestamp))
   120         parameters= {"key": key,
   124         parameters= {"key": key,
   121                 "route": route,
   125                 "route": route,
   122                 "to": to,
   126                 "to": to,
   123                 "message": message,
   127                 "message": message,
   124                 "charset":"utf-8", 
   128                 "charset":"utf-8", 
   131         if timestamp is not None:
   135         if timestamp is not None:
   132             parameters["senddate"] = unicode(timestamp)
   136             parameters["senddate"] = unicode(timestamp)
   133 
   137 
   134         parameters["concat_sms"] = "1" if len(message) > 160 else "0"
   138         parameters["concat_sms"] = "1" if len(message) > 160 else "0"
   135         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)) )
   136         headers = {"Content-type": "application/x-www-form-urlencoded",
   141         headers = {"Content-type": "application/x-www-form-urlencoded",
   137             "Accept": "text/plain"}
   142             "Accept": "text/plain"}
   138         conn = httplib.HTTPConnection("%s:%i" % (self.gateway, self.gatewayPort))
   143         conn = httplib.HTTPConnection("%s:%i" % (self.gateway, self.gatewayPort))
   139         try:
   144         try:
   140             conn.request(self.method, self.script, params, headers)
   145             conn.request(self.method, self.script, params, headers)
   141             response = conn.getresponse()
   146             response = conn.getresponse()
   142             data = response.read()
   147             data = response.read()
   143         except socket.gaierror:
   148         except socket.gaierror:
   144             raise InternetConnectionError("%s:%i" % (self.gateway, self.gatewayPort))
   149             raise InternetConnectionError("%s:%i" % (self.gateway, self.gatewayPort))
   145         else:
   150         finally:
   146             conn.close()
   151             conn.close()
   147             
   152             
   148         try:
   153         try:
   149             return StatusCode(int(data))
   154             return StatusCode(int(data))
   150         except UnknownStatusCode:
   155         except UnknownStatusCode: