(request, domain)
An API to send SMS.
    Expected post parameters:
        phone_number - the phone number to send to
        contact_id - the _id of a contact to send to (overrides phone_number)
        vn_id - the couch_id of a PhoneNumber to send to (overrides contact_id)
        text - the text of the message
        backend_id - the name of the MobileBackend to use while sending
An API to send SMS.
    Expected post parameters:
        phone_number - the phone number to send to
        contact_id - the _id of a contact to send to (overrides phone_number)
        vn_id - the couch_id of a PhoneNumber to send to (overrides contact_id)
        text - the text of the message
        backend_id - the name of the MobileBackend to use while sending
An API to send SMS . Expected post parameters : phone_number - the phone number to send to contact_id - the _id of a contact to send to ( overrides phone_number ) vn_id - the couch_id of a PhoneNumber to send to ( overrides contact_id ) text - the text of the message backend_id - the name of the MobileBackend to use while sending
def api_send_sms(request, domain):
    """
    An API to send SMS.
    Expected post parameters:
        phone_number - the phone number to send to
        contact_id - the _id of a contact to send to (overrides phone_number)
        vn_id - the couch_id of a PhoneNumber to send to (overrides contact_id)
        text - the text of the message
        backend_id - the name of the MobileBackend to use while sending
    """
    if request.method == "POST":
        phone_number = request.POST.get("phone_number", None)
        contact_id = request.POST.get("contact_id", None)
        vn_id = request.POST.get("vn_id", None)
        text = request.POST.get("text", None)
        backend_id = request.POST.get("backend_id", None)
        chat = request.POST.get("chat", None)
        contact = None

        if (phone_number is None and contact_id is None and not vn_id) or (text is None):
            return HttpResponseBadRequest("Not enough arguments.")

        vn = None
        if vn_id:
            vn = PhoneNumber.by_couch_id(vn_id)
            if not vn:
                return HttpResponseBadRequest("PhoneNumber not found.")

            if vn.domain != domain:
                return HttpResponseBadRequest("PhoneNumber not found.")

            phone_number = vn.phone_number
            contact = vn.owner
        elif contact_id is not None:
            try:
                contact = get_contact(domain, contact_id)
                assert contact is not None
                assert contact.domain == domain
            except Exception:
                return HttpResponseBadRequest("Contact not found.")

            vn = get_two_way_number_for_recipient(contact)
            if not vn:
                return HttpResponseBadRequest("Contact has no phone number.")
            phone_number = vn.phone_number

        try:
            chat_workflow = string_to_boolean(chat)
        except Exception:
            chat_workflow = False

        if chat_workflow:
            chat_user_id = request.couch_user.get_id
        else:
            chat_user_id = None

        logged_event = MessagingEvent.create_event_for_adhoc_sms(
            domain, recipient=contact,
            content_type=(MessagingEvent.CONTENT_CHAT_SMS if chat_workflow
                else MessagingEvent.CONTENT_API_SMS))

        args = [contact.doc_type, contact.get_id] if contact else []
        logged_subevent = logged_event.create_subevent_for_single_sms(*args)

        metadata = MessageMetadata(
            chat_user_id=chat_user_id,
            messaging_subevent_id=logged_subevent.pk,
        )
        if backend_id is not None:
            success = send_sms_with_backend_name(domain, phone_number, text, backend_id, metadata)
        elif vn is not None:
            success = send_sms_to_verified_number(vn, text, metadata)
        else:
            success = send_sms(domain, None, phone_number, text, metadata)

        if success:
            logged_subevent.completed()
            logged_event.completed()
            return HttpResponse("OK")
        else:
            logged_subevent.error(MessagingEvent.ERROR_INTERNAL_SERVER_ERROR)
            return HttpResponse("ERROR")
    else:
        return HttpResponseBadRequest("POST Expected.")
def api_send_sms ( request , domain ) : if request . method == "POST" : phone_number = request . POST . get ( "phone_number" , None ) contact_id = request . POST . get ( "contact_id" , None ) vn_id = request . POST . get ( "vn_id" , None ) text = request . POST . get ( "text" , None ) backend_id = request . POST . get ( "backend_id" , None ) chat = request . POST . get ( "chat" , None ) contact = None if ( phone_number is None and contact_id is None and not vn_id ) or ( text is None ) : return HttpResponseBadRequest ( "Not enough arguments." ) vn = None if vn_id : vn = PhoneNumber . by_couch_id ( vn_id ) if not vn : return HttpResponseBadRequest ( "PhoneNumber not found." ) if vn . domain != domain : return HttpResponseBadRequest ( "PhoneNumber not found." ) phone_number = vn . phone_number contact = vn . owner elif contact_id is not None : try : contact = get_contact ( domain , contact_id ) assert contact is not None assert contact . domain == domain except Exception : return HttpResponseBadRequest ( "Contact not found." ) vn = get_two_way_number_for_recipient ( contact ) if not vn : return HttpResponseBadRequest ( "Contact has no phone number." ) phone_number = vn . phone_number try : chat_workflow = string_to_boolean ( chat ) except Exception : chat_workflow = False if chat_workflow : chat_user_id = request . couch_user . get_id else : chat_user_id = None logged_event = MessagingEvent . create_event_for_adhoc_sms ( domain , recipient = contact , content_type = ( MessagingEvent . CONTENT_CHAT_SMS if chat_workflow else MessagingEvent . CONTENT_API_SMS ) ) args = [ contact . doc_type , contact . get_id ] if contact else [ ] logged_subevent = logged_event . create_subevent_for_single_sms ( * args ) metadata = MessageMetadata ( chat_user_id = chat_user_id , messaging_subevent_id = logged_subevent . pk , ) if backend_id is not None : success = send_sms_with_backend_name ( domain , phone_number , text , backend_id , metadata ) elif vn is not None : success = send_sms_to_verified_number ( vn , text , metadata ) else : success = send_sms ( domain , None , phone_number , text , metadata ) if success : logged_subevent . completed ( ) logged_event . completed ( ) return HttpResponse ( "OK" ) else : logged_subevent . error ( MessagingEvent . ERROR_INTERNAL_SERVER_ERROR ) return HttpResponse ( "ERROR" ) else : return HttpResponseBadRequest ( "POST Expected." )