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 process_incoming(msg): sms_load_counter("inbound", msg.domain)() v, has_domain_two_way_scope = get_inbound_phone_entry(msg) if v: if any_migrations_in_progress(v.domain): raise DelayProcessing() msg.couch_recipient_doc_type = v.owner_doc_type msg.couch_recipient = v.owner_id msg.domain = v.domain msg.location_id = get_location_id_by_verified_number(v) msg.save() elif msg.domain_scope: if any_migrations_in_progress(msg.domain_scope): raise DelayProcessing() msg.domain = msg.domain_scope msg.save() opt_in_keywords, opt_out_keywords, pass_through_opt_in_keywords = get_opt_keywords( msg) domain = v.domain if v else None if is_opt_message(msg.text, opt_out_keywords): if PhoneBlacklist.opt_out_sms(msg.phone_number, domain=domain): metadata = MessageMetadata(ignore_opt_out=True) text = get_message(MSG_OPTED_OUT, v, context=(opt_in_keywords[0], )) if v: send_sms_to_verified_number(v, text, metadata=metadata) elif msg.backend_id: send_sms_with_backend(msg.domain, msg.phone_number, text, msg.backend_id, metadata=metadata) else: send_sms(msg.domain, None, msg.phone_number, text, metadata=metadata) elif is_opt_message(msg.text, opt_in_keywords): if PhoneBlacklist.opt_in_sms(msg.phone_number, domain=domain): text = get_message(MSG_OPTED_IN, v, context=(opt_out_keywords[0], )) if v: send_sms_to_verified_number(v, text) elif msg.backend_id: send_sms_with_backend(msg.domain, msg.phone_number, text, msg.backend_id) else: send_sms(msg.domain, None, msg.phone_number, text) else: if is_opt_message(msg.text, pass_through_opt_in_keywords): # Opt the phone number in, and then process the message normally PhoneBlacklist.opt_in_sms(msg.phone_number, domain=domain) handled = False is_two_way = v is not None and v.is_two_way if msg.domain and domain_has_privilege(msg.domain, privileges.INBOUND_SMS): if ((is_two_way or has_domain_two_way_scope) and is_contact_active( v.domain, v.owner_doc_type, v.owner_id)): handled = load_and_call(settings.SMS_HANDLERS, v, msg.text, msg) if not handled and not is_two_way: handled = process_pre_registration(msg) if not handled: handled = process_sms_registration(msg) # If the sms queue is enabled, then the billable gets created in remove_from_queue() if (not settings.SMS_QUEUE_ENABLED and msg.domain and domain_has_privilege(msg.domain, privileges.INBOUND_SMS)): create_billable_for_sms(msg)
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 process_incoming(msg): sms_load_counter("inbound", msg.domain)() v, has_domain_two_way_scope = get_inbound_phone_entry(msg) if v: if any_migrations_in_progress(v.domain): raise DelayProcessing() msg.couch_recipient_doc_type = v.owner_doc_type msg.couch_recipient = v.owner_id msg.domain = v.domain msg.location_id = get_location_id_by_verified_number(v) msg.save() elif msg.domain_scope: if any_migrations_in_progress(msg.domain_scope): raise DelayProcessing() msg.domain = msg.domain_scope msg.save() opt_in_keywords, opt_out_keywords, pass_through_opt_in_keywords = get_opt_keywords(msg) domain = v.domain if v else None if is_opt_message(msg.text, opt_out_keywords): if PhoneBlacklist.opt_out_sms(msg.phone_number, domain=domain): metadata = MessageMetadata(ignore_opt_out=True) text = get_message(MSG_OPTED_OUT, v, context=(opt_in_keywords[0],)) if v: send_sms_to_verified_number(v, text, metadata=metadata) elif msg.backend_id: send_sms_with_backend(msg.domain, msg.phone_number, text, msg.backend_id, metadata=metadata) else: send_sms(msg.domain, None, msg.phone_number, text, metadata=metadata) elif is_opt_message(msg.text, opt_in_keywords): if PhoneBlacklist.opt_in_sms(msg.phone_number, domain=domain): text = get_message(MSG_OPTED_IN, v, context=(opt_out_keywords[0],)) if v: send_sms_to_verified_number(v, text) elif msg.backend_id: send_sms_with_backend(msg.domain, msg.phone_number, text, msg.backend_id) else: send_sms(msg.domain, None, msg.phone_number, text) else: if is_opt_message(msg.text, pass_through_opt_in_keywords): # Opt the phone number in, and then process the message normally PhoneBlacklist.opt_in_sms(msg.phone_number, domain=domain) handled = False is_two_way = v is not None and v.is_two_way if msg.domain and domain_has_privilege(msg.domain, privileges.INBOUND_SMS): handled = load_and_call(settings.CUSTOM_SMS_HANDLERS, v, msg.text, msg) if not handled and v and v.pending_verification: from . import verify handled = verify.process_verification(v, msg, create_subevent_for_inbound=not has_domain_two_way_scope) if ( not handled and (is_two_way or has_domain_two_way_scope) and is_contact_active(v.domain, v.owner_doc_type, v.owner_id) ): handled = load_and_call(settings.SMS_HANDLERS, v, msg.text, msg) if not handled and not is_two_way: handled = process_pre_registration(msg) if not handled: handled = process_sms_registration(msg) # If the sms queue is enabled, then the billable gets created in remove_from_queue() if ( not settings.SMS_QUEUE_ENABLED and msg.domain and domain_has_privilege(msg.domain, privileges.INBOUND_SMS) ): create_billable_for_sms(msg)