def send_message_via_backend(msg, backend=None, onerror=lambda: None): """send sms using a specific backend msg - outbound message object backend - MobileBackend object to use for sending; if None, use the default backend guessing rules onerror - error handler; mostly useful for logging a custom message to the error log """ try: msg.text = clean_text(msg.text) except Exception: logging.exception("Could not clean text for sms dated '%s' in domain '%s'" % (msg.date, msg.domain)) try: if not backend: backend = msg.outbound_backend # note: this will handle "verified" contacts that are still pending # verification, thus the backend is None. it's best to only call # send_sms_to_verified_number on truly verified contacts, though if backend.domain_is_authorized(msg.domain): backend.send(msg) else: raise BackendAuthorizationException("Domain '%s' is not authorized to use backend '%s'" % (msg.domain, backend._id)) try: msg.backend_api = backend.__class__.get_api_id() msg.backend_id = backend._id except Exception: pass msg.save() return True except Exception: onerror() return False
def send_message_via_backend(msg, backend=None, orig_phone_number=None): """send sms using a specific backend msg - outbound message object backend - backend to use for sending; if None, msg.outbound_backend is used orig_phone_number - the originating phone number to use when sending; this is sent in if the backend supports load balancing """ sms_load_counter("outbound", msg.domain)() try: msg.text = clean_text(msg.text) except Exception: logging.exception("Could not clean text for sms dated '%s' in domain '%s'" % (msg.date, msg.domain)) try: # We need to send SMS when msg.domain is None to support sending to # people who opt in without being tied to a domain if msg.domain and not domain_has_privilege(msg.domain, privileges.OUTBOUND_SMS): raise Exception( ("Domain '%s' does not have permission to send SMS." " Please investigate why this function was called.") % msg.domain ) phone_obj = PhoneBlacklist.get_by_phone_number_or_none(msg.phone_number) if phone_obj and not phone_obj.send_sms: if msg.ignore_opt_out and phone_obj.can_opt_in: # If ignore_opt_out is True on the message, then we'll still # send it. However, if we're not letting the phone number # opt back in and it's in an opted-out state, we will not # send anything to it no matter the state of the ignore_opt_out # flag. pass else: msg.set_system_error(SMS.ERROR_PHONE_NUMBER_OPTED_OUT) return False if not backend: backend = msg.outbound_backend if backend.domain_is_authorized(msg.domain): backend.send(msg, orig_phone_number=orig_phone_number) else: raise BackendAuthorizationException( "Domain '%s' is not authorized to use backend '%s'" % (msg.domain, backend.pk) ) msg.backend_api = backend.hq_api_id msg.backend_id = backend.couch_id msg.save() return True except Exception: should_log_exception = True if backend: should_log_exception = should_log_exception_for_backend(backend) if should_log_exception: log_sms_exception(msg) return False
def send_message_via_backend(msg, backend=None, orig_phone_number=None): """send sms using a specific backend msg - outbound message object backend - backend to use for sending; if None, msg.outbound_backend is used orig_phone_number - the originating phone number to use when sending; this is sent in if the backend supports load balancing """ try: msg.text = clean_text(msg.text) except Exception: logging.exception("Could not clean text for sms dated '%s' in domain '%s'" % (msg.date, msg.domain)) try: # We need to send SMS when msg.domain is None to support sending to # people who opt in without being tied to a domain if msg.domain and not domain_has_privilege(msg.domain, privileges.OUTBOUND_SMS): raise Exception( ("Domain '%s' does not have permission to send SMS." " Please investigate why this function was called.") % msg.domain ) phone_obj = PhoneBlacklist.get_by_phone_number_or_none(msg.phone_number) if phone_obj and not phone_obj.send_sms: if msg.ignore_opt_out and phone_obj.can_opt_in: # If ignore_opt_out is True on the message, then we'll still # send it. However, if we're not letting the phone number # opt back in and it's in an opted-out state, we will not # send anything to it no matter the state of the ignore_opt_out # flag. pass else: msg.set_system_error(SMS.ERROR_PHONE_NUMBER_OPTED_OUT) return False if not backend: backend = msg.outbound_backend if backend.domain_is_authorized(msg.domain): backend.send(msg, orig_phone_number=orig_phone_number) else: raise BackendAuthorizationException( "Domain '%s' is not authorized to use backend '%s'" % (msg.domain, backend.pk) ) msg.backend_api = backend.hq_api_id msg.backend_id = backend.couch_id msg.save() return True except Exception: should_log_exception = True if backend: should_log_exception = should_log_exception_for_backend(backend) if should_log_exception: log_sms_exception(msg) return False
def send_message_via_backend(msg, backend=None, orig_phone_number=None): """send sms using a specific backend msg - outbound message object backend - MobileBackend object to use for sending; if None, use msg.outbound_backend orig_phone_number - the originating phone number to use when sending; this is sent in if the backend supports load balancing """ try: msg.text = clean_text(msg.text) except Exception: logging.exception("Could not clean text for sms dated '%s' in domain '%s'" % (msg.date, msg.domain)) try: if not domain_has_privilege(msg.domain, privileges.OUTBOUND_SMS): raise Exception( ("Domain '%s' does not have permission to send SMS." " Please investigate why this function was called.") % msg.domain ) phone_obj = PhoneNumber.get_by_phone_number_or_none(msg.phone_number) if phone_obj and not phone_obj.send_sms: if msg.ignore_opt_out and phone_obj.can_opt_in: # If ignore_opt_out is True on the message, then we'll still # send it. However, if we're not letting the phone number # opt back in and it's in an opted-out state, we will not # send anything to it no matter the state of the ignore_opt_out # flag. pass else: msg.set_system_error(SMS.ERROR_PHONE_NUMBER_OPTED_OUT) return False if not backend: backend = msg.outbound_backend # note: this will handle "verified" contacts that are still pending # verification, thus the backend is None. it's best to only call # send_sms_to_verified_number on truly verified contacts, though if not msg.backend_id: msg.backend_id = backend._id if backend.domain_is_authorized(msg.domain): backend.send(msg, orig_phone_number=orig_phone_number) else: raise BackendAuthorizationException("Domain '%s' is not authorized to use backend '%s'" % (msg.domain, backend._id)) try: msg.backend_api = backend.__class__.get_api_id() except Exception: pass msg.save() create_billable_for_sms(msg) return True except Exception: log_sms_exception(msg) return False
def send_message_via_backend(msg, backend=None, orig_phone_number=None, onerror=lambda: None): """send sms using a specific backend msg - outbound message object backend - MobileBackend object to use for sending; if None, use msg.outbound_backend orig_phone_number - the originating phone number to use when sending; this is sent in if the backend supports load balancing onerror - error handler; mostly useful for logging a custom message to the error log """ try: msg.text = clean_text(msg.text) except Exception: logging.exception( "Could not clean text for sms dated '%s' in domain '%s'" % (msg.date, msg.domain)) try: if not backend: backend = msg.outbound_backend # note: this will handle "verified" contacts that are still pending # verification, thus the backend is None. it's best to only call # send_sms_to_verified_number on truly verified contacts, though if not msg.backend_id: msg.backend_id = backend._id if backend.domain_is_authorized(msg.domain): backend.send(msg, orig_phone_number=orig_phone_number) else: raise BackendAuthorizationException( "Domain '%s' is not authorized to use backend '%s'" % (msg.domain, backend._id)) try: msg.backend_api = backend.__class__.get_api_id() except Exception: pass msg.save() return True except Exception: onerror() return False
def send_message_via_backend(msg, backend=None, orig_phone_number=None): """send sms using a specific backend msg - outbound message object backend - MobileBackend object to use for sending; if None, use msg.outbound_backend orig_phone_number - the originating phone number to use when sending; this is sent in if the backend supports load balancing """ try: msg.text = clean_text(msg.text) except Exception: logging.exception("Could not clean text for sms dated '%s' in domain '%s'" % (msg.date, msg.domain)) try: if not backend: backend = msg.outbound_backend # note: this will handle "verified" contacts that are still pending # verification, thus the backend is None. it's best to only call # send_sms_to_verified_number on truly verified contacts, though if not msg.backend_id: msg.backend_id = backend._id if backend.domain_is_authorized(msg.domain): backend.send(msg, orig_phone_number=orig_phone_number) else: raise BackendAuthorizationException("Domain '%s' is not authorized to use backend '%s'" % (msg.domain, backend._id)) try: msg.backend_api = backend.__class__.get_api_id() except Exception: pass msg.save() create_billable_for_sms(msg) return True except Exception: log_sms_exception(msg) return False
def send_message_via_backend(msg, backend=None, orig_phone_number=None): """send sms using a specific backend msg - outbound message object backend - MobileBackend object to use for sending; if None, use msg.outbound_backend orig_phone_number - the originating phone number to use when sending; this is sent in if the backend supports load balancing """ try: msg.text = clean_text(msg.text) except Exception: logging.exception( "Could not clean text for sms dated '%s' in domain '%s'" % (msg.date, msg.domain)) try: if not domain_has_privilege(msg.domain, privileges.OUTBOUND_SMS): raise Exception( ("Domain '%s' does not have permission to send SMS." " Please investigate why this function was called.") % msg.domain) phone_obj = PhoneNumber.get_by_phone_number_or_none(msg.phone_number) if phone_obj and not phone_obj.send_sms: if msg.ignore_opt_out and phone_obj.can_opt_in: # If ignore_opt_out is True on the message, then we'll still # send it. However, if we're not letting the phone number # opt back in and it's in an opted-out state, we will not # send anything to it no matter the state of the ignore_opt_out # flag. pass else: msg.set_system_error(SMS.ERROR_PHONE_NUMBER_OPTED_OUT) return False if not backend: backend = msg.outbound_backend # note: this will handle "verified" contacts that are still pending # verification, thus the backend is None. it's best to only call # send_sms_to_verified_number on truly verified contacts, though if not msg.backend_id: msg.backend_id = backend._id if backend.domain_is_authorized(msg.domain): backend.send(msg, orig_phone_number=orig_phone_number) else: raise BackendAuthorizationException( "Domain '%s' is not authorized to use backend '%s'" % (msg.domain, backend._id)) try: msg.backend_api = backend.__class__.get_api_id() except Exception: pass msg.save() create_billable_for_sms(msg) return True except Exception: log_sms_exception(msg) return False