One API call to send SMS to multiple users (Twilio)

Viewed 2437

I have a list of ~50 phone numbers I need to send the same SMS to via Twilio's API. How do I do that (via Python)?

I can do it in a for loop like so:

from twilio.rest import Client
client = Client(SID, AT)

for number in number_list:
    client.messages.create(to=number, from_="+18328955063",body="foo bar")

But instead of hitting Twilio's API multiple times, I'd rather send an array/list of phone numbers in bulk and just make one API call. So how do I do that? Is it possible at all?

I'm setting up a background async task to take care of outgoing text messages, and ideally wouldn't want my task to wait a long time while Twilio processes the messages one by one. I'd just want to send over a list of numbers as payload and be done with it.

2 Answers

You can also do it this way: https://www.twilio.com/docs/notify/api/notification-resource#code-send-a-notification-to-bindings-in-the-request-1

notification = client.notify.services("ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")\
    .notifications.create(
        to_binding=[
            "{\"binding_type\":\"sms\",\"address\":\"+15555555555\"}",
            "{\"binding_type\":\"facebook-messenger\",\"address\":\"123456789123\"}"
        ],
        body="Hello Bob")

no need to create binding, by the way, twilio can accept 10,000 numbers

The destination address specified as a JSON string. Multiple to_binding parameters can be included but the total size of the request entity should not exceed 1MB. This is typically sufficient for 10,000 phone numbers.

Related