src/utils.py
def send_sms(request, to_number, body, callback_urlname="sms_status_callback"):
    """
    Create :class:`OutgoingSMS` object and send SMS using Twilio.
    """
    client = TwilioRestClient(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
    from_number = settings.TWILIO_PHONE_NUMBER

    message = OutgoingSMS.objects.create(
        from_number=from_number,
        to_number=to_number,
        body=body,
    )

    status_callback = None
    if callback_urlname:
        status_callback = build_callback_url(request, callback_urlname, message)

    logger.debug("Sending SMS message to %s with callback url %s: %s.",
                 to_number, status_callback, body)

    if not getattr(settings, "TWILIO_DRY_MODE", False):
        sent = client.sms.messages.create(
            to=to_number,
            from_=from_number,
            body=body,
            status_callback=status_callback
        )
        logger.debug("SMS message sent: %s", sent.__dict__)

        message.sms_sid = sent.sid
        message.account_sid = sent.account_sid
        message.status = sent.status
        message.to_parsed = sent.to
        if sent.price:
            message.price = Decimal(force_text(sent.price))
            message.price_unit = sent.price_unit
        message.sent_at = sent.date_created
        message.save(update_fields=[
            "sms_sid", "account_sid", "status", "to_parsed",
            "price", "price_unit", "sent_at"
        ])
    else:
        logger.info("SMS: from %s to %s: %s", from_number, to_number, body)
    return message
def send_sms(request, to_number, body, callback_urlname="sms_status_callback"):
    """
    Create :class:`OutgoingSMS` object and send SMS using Twilio.
    """
    client = TwilioRestClient(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
    from_number = settings.TWILIO_PHONE_NUMBER

    message = OutgoingSMS.objects.create(
        from_number=from_number,
        to_number=to_number,
        body=body,
    )

    status_callback = None
    if callback_urlname:
        status_callback = build_callback_url(request, callback_urlname, message)

    logger.debug("Sending SMS message to %s with callback url %s: %s.",
                 to_number, status_callback, body)

    if not getattr(settings, "TWILIO_DRY_MODE", False):
        sent = client.sms.messages.create(
            to=to_number,
            from_=from_number,
            body=body,
            status_callback=status_callback
        )
        logger.debug("SMS message sent: %s", sent.__dict__)

        message.sms_sid = sent.sid
        message.account_sid = sent.account_sid
        message.status = sent.status
        message.to_parsed = sent.to
        if sent.price:
            message.price = Decimal(force_text(sent.price))
            message.price_unit = sent.price_unit
        message.sent_at = sent.date_created
        message.save(update_fields=[
            "sms_sid", "account_sid", "status", "to_parsed",
            "price", "price_unit", "sent_at"
        ])
    else:
        logger.info("SMS: from %s to %s: %s", from_number, to_number, body)
    return message
Create :class:`OutgoingSMS` object and send SMS using Twilio.