(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
send sms using a specific backend
send sms using a specific backend
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)
            metrics_counter("commcare.sms.outbound_message", tags={
                'domain': msg.domain,
                'status': 'ok',
                'backend': _get_backend_tag(backend),
            })
        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 as e:
        metrics_counter("commcare.sms.outbound_message", tags={
            'domain': msg.domain,
            'status': 'error',
            'backend': _get_backend_tag(backend),
        })
        should_log_exception = True

        if backend:
            should_log_exception = should_log_exception_for_backend(backend, e)

        if should_log_exception:
            log_sms_exception(msg)

        return False
def send_message_via_backend ( msg , backend = None , orig_phone_number = None ) : 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 ) metrics_counter ( "commcare.sms.outbound_message" , tags = { 'domain' : msg . domain , 'status' : 'ok' , 'backend' : _get_backend_tag ( backend ) , } ) 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 as e : metrics_counter ( "commcare.sms.outbound_message" , tags = { 'domain' : msg . domain , 'status' : 'error' , 'backend' : _get_backend_tag ( backend ) , } ) should_log_exception = True if backend : should_log_exception = should_log_exception_for_backend ( backend , e ) if should_log_exception : log_sms_exception ( msg ) return False