user3111472
user3111472

Reputation: 207

Sending Hundreds of SMS With Different Numbers with Twilio Caused Duplicate Messages to be Sent

I have a list of about 470 numbers with different messaging I would like to send SMS to. I'm currently using Firebase Cloud Functions as my backend. Everything seemed to work perfectly locally without sending duplicate SMS to about 5 different test numbers.

By using the code below with the 470 numbers, each number got the same SMS 3-4 times each. I don't know if the issue is with Firebase Cloud Functions or not.

The concept is to send a SMS with Twilio and if it's a success. I use Firebase Firestore to store the message in a database. I'm trying to prevent Twilio from sending duplicate SMS to the same number with the same message.

Sample Code from Client:

textMessages[
{phoneNumber: "1111111111", message: "Test message 1 ", campaign: "Test Campaign"},
{phoneNumber: "2222222222", message: "Test message 2", campaign: "Test Campaign"},
{phoneNumber: "3333333333", message: "Test message 3", campaign: "Test Campaign"}]

exports.bulkTextWeb = functions.https.onCall(async (data) => {

    const records = data.textMessages;

    for (let i = 0, j = records.length; i < j; i++) {

        const { phoneNumber, message, campaign } = records[i];

        var msgFrom = twilio_number;
        var msgTo = phoneNumber;
        var msgBody = message;

        await client.messages.create({
            to: '1' + phoneNumber,
            from: twilio_number,
            body: message
        })
            .then(async message => {

                if (message.status === 'queued' || message.status === 'sent' || message.status === 'delivered') {

                    // Check if campaign already exist
                    let docRef = await db.collection('campaigns')
                        .where('campaigns', 'array-contains', campaign).get()
                        .then(snapshot => {
                            // If there no campaign, add it to Firestore.
                            if (snapshot.empty) {
                                addCampaign(campaign);
                            }
                            // Add new message to Firestore
                            addNewMessage(msgFrom, msgTo, msgBody, campaign);
                        })
                }
            })

        if (i === (j - 1)) {

            // Return back to the client when all messages are sent
            return { messages: 'Sent' } 
        }
    }
})

Upvotes: 1

Views: 875

Answers (1)

Alex Baban
Alex Baban

Reputation: 11732

In my opinion the amount of code provided in the question is just fine, in fact it let me remember having the same problem some time ago with a Node.js application.

The problem is not at Twilio.

You send one message via Twilio with one call of client.messages.create(). Since the same message is sent multiple times it's clear that client.messages.create() is called multiple times and it's called multiple times because of async/await.

I solved the problem by getting rid of async/await and using recursion to send the messages one after another.

You can see some code using recursion in answers I gave

Upvotes: 2

Related Questions