Vitalii Bashynskyi
Vitalii Bashynskyi

Reputation: 45

How can I send a sms in Django?

I encountered a problem when trying to send sms using the SMSC service in Django project.

My Celery task for sending email and sms:

 def order_created_retail(order_id):
    # Task to send an email when an order is successfully created
    order = OrderRetail.objects.get(id=order_id)
    subject = 'Order №{}.'.format(order_id)
    message_mail = 'Hello, {}! You have successfully placed an order{}. Manager will contact you shortly'.format(order.first_name, order.id)
    message_sms = 'Your order №{} is accepted! Wait for operator call'
    mail_sent = send_mail(
        subject,
        message_mail,
        '[email protected]',
        [order.email]
    )
    smsc = SMSC()

    sms_sent = smsc.send_sms(
        [order.phone],
        str(message_sms)
    )
    return mail_sent, sms_sent

Email sends correctly, but for sms I get that error:

Task orders.tasks.order_created_retail[f05458b1-65e8-493b-9069-fbaa55083e7a] raised unexpected: TypeError('quote_from_bytes() expected bytes')

function from SMSC library:

    def send_sms(self, phones, message, translit=0, time="", id=0, format=0, sender=False, query=""):
        formats = ["flash=1", "push=1", "hlr=1", "bin=1", "bin=2", "ping=1", "mms=1", "mail=1", "call=1", "viber=1", "soc=1"]

        m = self._smsc_send_cmd("send", "cost=3&phones=" + quote(phones) + "&mes=" + quote(message) + \
                    "&translit=" + str(translit) + "&id=" + str(id) + ifs(format > 0, "&" + formats[format-1], "") + \
                    ifs(sender == False, "", "&sender=" + quote(str(sender))) + \
                    ifs(time, "&time=" + quote(time), "") + ifs(query, "&" + query, ""))

        # (id, cnt, cost, balance) или (id, -error)

        if SMSC_DEBUG:
            if m[1] > "0":
                print("Сообщение отправлено успешно. ID: " + m[0] + ", всего SMS: " + m[1] + ", стоимость: " + m[2] + ", баланс: " + m[3])
            else:
                print("Ошибка №" + m[1][1:] + ifs(m[0] > "0", ", ID: " + m[0], ""))

        return m

What am I doing wrong?

Thanks!

Upvotes: 0

Views: 391

Answers (1)

Vitalii Bashynskyi
Vitalii Bashynskyi

Reputation: 45

to solve this problem, I started investigating the functions that were giving out the error.

It turned out that I was passing an incorrect value. the function was expecting a string. And it took me a long time to figure out why editing didn't help.

It turns out that you have to RESET CELERY every time you make an edit.

Upvotes: 1

Related Questions