def sms(request): message = Responder().respond(request) if not message: return HttpResponse(status=403) response = HttpResponse(strip_accents(unicode(message[:160]))) response['X-Vumi-HTTPRelay-Reply'] = 'true' response['Content-Length'] = len(response.content) return response
def send_sms(self, from_tel, to_tel, message, message_type="Unknown"): message = strip_accents(message) if is_not_empty(from_tel): organization_setting = OrganizationFinder( ).find_organization_setting(from_tel) is_there_orgnization_with_to_number = OrganizationFinder( ).find_organization_setting(to_tel) != None if (is_there_orgnization_with_to_number): return False smsc = None if organization_setting is not None and organization_setting.outgoing_number is not None: smsc = organization_setting.outgoing_number.smsc if smsc is None: logger.error("No SMSC configured for %s" % organization_setting.organization.org_id) raise NoSMSCException() socket.setdefaulttimeout(120) logger.debug("Posting sms to %s" % settings.VUMI_API_URL) if settings.USE_NEW_VUMI: client = VumiApiClient( connection=Connection(smsc.vumi_username, smsc.vumi_username, base_url=settings.VUMI_API_URL)) sms_response = client.send_sms( to_addr=to_tel, from_addr=from_tel, content=message.encode('utf-8'), transport_name=smsc.vumi_username) return sms_response[0] else: try: client = VumiClient(None, None, connection=Connection( smsc.vumi_username, smsc.vumi_username, base_url=settings.VUMI_API_URL)) resp = client.send_sms(to_msisdn=to_tel, from_msisdn=from_tel, message=message.encode('utf-8')) response_object = json.loads(resp.content) if response_object: log_sms(to_tel, from_tel, message, organization_setting.organization, response_object[0].get('id'), smsc.vumi_username, message_type) return True except (URLError, VumiInvalidDestinationNumberException) as err: logger.exception('Unable to send sms. %s' % err) return False except socket.timeout: logger.exception( 'Request timed-out. Organization: %s, From: %s, To: %s.' % (organization_setting, from_tel, to_tel)) return False return False
def send_sms(self, from_tel, to_tel, message, message_type="Unknown", message_tracker=None): message = strip_accents(message) if is_empty(from_tel): raise NoSMSCException() organization_setting = OrganizationFinder().find_organization_setting( from_tel) is_there_orgnization_with_to_number = OrganizationFinder( ).find_organization_setting(to_tel) != None if (is_there_orgnization_with_to_number): return False smsc = None if organization_setting is not None and organization_setting.outgoing_number is not None: smsc = organization_setting.outgoing_number.smsc if smsc is None: raise NoSMSCException() socket.setdefaulttimeout(120) logger.debug("Posting sms to %s" % settings.VUMI_API_URL) if settings.USE_NEW_VUMI: client = VumiApiClient( connection=Connection(smsc.vumi_username, smsc.vumi_username, base_url=settings.VUMI_API_URL)) sms_response = client.send_sms(to_addr=to_tel, from_addr=from_tel, content=message.encode('utf-8'), transport_name=smsc.vumi_username) result = sms_response[0] else: try: client = VumiClient(None, None, connection=Connection( smsc.vumi_username, smsc.vumi_username, base_url=settings.VUMI_API_URL)) resp = client.send_sms(to_msisdn=to_tel, from_msisdn=from_tel, message=message.encode('utf-8')) response_object = json.loads(resp.content) if response_object: log_sms(to_tel, from_tel, message, organization_setting.organization, response_object[0].get('id'), smsc.vumi_username, message_type) result = True except (URLError, VumiInvalidDestinationNumberException) as err: logger.exception('Unable to send sms. %s' % err) result = False except socket.timeout: logger.exception( 'Request timed-out. Organization: %s, From: %s, To: %s.' % (organization_setting, from_tel, to_tel)) result = False if result and message_tracker is not None: increment_dict = {'send_message_count': 1} if smsc.vumi_username in settings.SMSC_WITHOUT_STATUS_REPORT: increment_dict.update({couter_map.get(message_type): 1}) message_tracker.increment_message_count_for(**increment_dict) return result