AliZaidi
AliZaidi

Reputation: 27

Twilio WhatsApp messages are in 'queued' status

Okay so I got approval from WhatsApp and Twilio (after Facebook Business verification) to use the WhatsApp API for sending out appointment reminders to my clients. I configured the message templates and they got approved too. Check the image below:

Twilio approval

I have written a code in Python where I pick my data from a PostgreSQL server hosted on cloud (using psycopg2) and then it sends out messages to the phone numbers fetched using a query. Here is the code:

from twilio.rest import Client
import psycopg2
import time

account_sid = 'AC54xxxxxxxxxxxxxxxxxxxxxxxxxxx'
auth_token = 'f1384yyyyyyyyyyyyyyyyyyyyyyyyyyy'

connection_string = ""

conn = psycopg2.connect(user = "xxxx",
                    password = "yyyyyy",
                    host = "zzzzzzzzzzzzzzz.zzzzzzzzzzzz",
                    port = "ABCD",
                    database = "some_db")

cur = conn.cursor()
cur.execute("""query to pick data""")

rows = cur.fetchall()

client_phone_list = []
phone_list_not_received = []
session_date_list = []
session_time_list = []
client_first_name_list = []

for row in rows:
    session_date_list.append(row[0])
    session_time_list.append(row[1])
    client_first_name_list.append(row[2])
    client_phone_list.append(row[3])

cur.close()
conn.close()

client = Client(account_sid, auth_token)

message_reminder_template = """Hello {},

This is a reminder about your session today at {}. Please be on time to utilize the full length of 
the session and avoid distress :)

We look forward to taking care of you!"""

for i in range(len(client_phone_list)):
    first_name = client_first_name_list[i]
    appointment_time = session_time_list[i]
    message_body = message_reminder_template.format(first_name, appointment_time)
    print(message_body)

message = client.messages.create(body = str(message_body),
                                 from_ = 'whatsapp:+1(mytwilionumber)',
                                 to = 'whatsapp:+91'+client_phone_list[i])
time.sleep(10)
text_status = message.status
print(text_status)

Whenever I run this code the message status returned is always 'queued'. I have checked that I am not using the 'Test Credentials' but the 'Live Credentials'.

I have also checked the error_code and error_message which returns as NULL. So there is no error but the messages are not getting sent. How can I change that?

Any help would be hugely appreciated.

Also note that the message body used in the code above is approved as a template from WhatsApp.

Upvotes: 1

Views: 4014

Answers (1)

philnash
philnash

Reputation: 73075

Twilio developer evangelist here.

At the point that you make the API request to send the message the status will be returned to code as "queued". That's this point in your code here:

message = client.messages.create(body = str(message_body),
                                 from_ = 'whatsapp:+1(mytwilionumber)',
                                 to = 'whatsapp:+91'+client_phone_list[i])

What you do next will not work though:

time.sleep(10)
text_status = message.status
print(text_status)

Waiting 10 seconds and then reading the status from the message object that was returned when you created the message will still return "queued".

If you want to fetch the message status after 10 seconds to see if it has been sent then you will need to make a second call to the messages API, like so:

time.sleep(10)
latest_message = client.messages(message.sid).fetch()
print(latest_message.status)

For a more efficient method of keeping track of the status of your messages, check out this tutorial on receiving webhooks for message status updates.

Let me know if that helped at all.

Upvotes: 1

Related Questions