def test_get_sms_list(self): sms_list = self.backend._get_sms_list(SMSRequest(**req_data)) assert len(sms_list) == 3 for to, sms in zip(req_data['to'].split(';'), sms_list): assert sms.to[0] == check_cell_phone_number(to) assert sms.msg == truncate_sms(req_data['msg']) assert sms.signature == req_data['signature'][:len(sms.signature)]
def _get_sms_list(self, sms_request): if not sms_request: return [] sms_list = [] reference = (datetime.now().strftime('%Y%m%d%H%M%S') + u''.join(sms_request.to[:1])) for msisdn in sms_request.to: sms_list.append(SMSRequest(msisdn, sms_request.msg, sms_request.signature, reliable=sms_request.reliable, reference=reference)) return sms_list
def send(self, sms_request, account_dict): """ Send an SMS message to one or more recipients, and create entries in the SMS table for each successful attempt. """ capacity = self.get_url_capacity() sender = u'[%s]' % self.get_slug( ) if not sms_request.signature else sms_request.signature reference = datetime.datetime.now().strftime( '%Y%m%d%H%M%S') + u''.join(sms_request.to[:1]) all_succeeded = True # Split SMSes into batches depending on the capacity requests = [] while len(sms_request.to) > 0: requests.append( SMSRequest(sms_request.to[:capacity], sms_request.msg, sms_request.signature, reliable=sms_request.reliable, reference=reference)) del sms_request.to[:capacity] # Send each batch for request in requests: url = self.get_send_url(request, account_dict) logger.debug('Sending SMS using: %s' % url) # Make request to provider result = u'' if url is not None: try: sock = urllib2.urlopen(url) result = sock.read() sock.close() except: return False logger.debug('Result: %s' % result) # Validate result, create log entry if successful if not self.validate_send_result(result): all_succeeded = False else: for dest in request.to: SMS.objects.create(sender=sender, content=sms_request.msg, to=dest, backend=self.get_slug(), direction=DIRECTION_OUTBOUND, gateway_ref=reference) return all_succeeded
def send(self, sms_request, account_dict): """ Send an SMS message to one or more recipients, and create entries in the SMS table for each successful attempt. """ capacity = self.get_url_capacity() sender = u'[{}]'.format(self.get_slug( )) if not sms_request.signature else sms_request.signature reference = self.get_send_reference(sms_request) all_succeeded = True # Split SMSes into batches depending on the capacity requests = [] while len(sms_request.to) > 0: requests.append( SMSRequest(sms_request.to[:capacity], sms_request.msg, sms_request.signature, reliable=sms_request.reliable, reference=reference)) del sms_request.to[:capacity] # Send each batch for request in requests: url = self.get_send_url(request, account_dict) # Make request to provider result = '' if url is not None: try: sock = urlopen(url) result = sock.read().decode('utf_8') sock.close() except: return False # Validate result, create log entry if successful if not self.validate_send_result(result): all_succeeded = False else: for dest in request.to: SMS.objects.create(sender=sender, content=sms_request.msg, to=dest, backend=self.get_slug(), direction=DIRECTION_OUTBOUND, gateway_ref=self.get_gateway_ref( reference, result)) return all_succeeded
def send(to, msg, signature, using=None, reliable=False): """ Send an SMS message immediately. * 'to' is a semicolon separated list of phone numbers with an international prefix (+32... etc). * 'msg' is the message itself as a unicode object (max 160 characters). * 'signature' is where the message comes from. Depends on the backend in use. * 'using' is an optional parameter where you can specify a specific account to send messages from. """ # Don't send empty smses if not msg: return from smsgateway.backends import get_backend from smsgateway.sms import SMSRequest account_dict = get_account(using) backend = get_backend(account_dict['backend']) sms_request = SMSRequest(to, msg, signature, reliable=reliable) return backend.send(sms_request, account_dict)
def test_initialize_with_sms_request(self): sms_request = SMSRequest(**req_data) assert self.backend._initialize(sms_request) is True
def test_initialize_with_sms_request(self): sms_request = SMSRequest(**req_data) self.assert_(self.backend._initialize(sms_request) == True)