1

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' } 
        }
    }
})
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
user3111472
  • 207
  • 2
  • 12
  • Your question contains a lot of your own code ... likely a lot more than what is actually needed to help resolve the issue. What debugging have you performed yourself to show where the issue might lie? – Kolban Dec 02 '19 at 22:43
  • 1
    I wanted to provide as much code as I could to help with solving my problem. I've used console.logs for debugging and ran several test locally and using Firebase hosting. It might be a lagging issue between Twilio and Cloud Functions. – user3111472 Dec 02 '19 at 22:51
  • The gods of Stackoverflow like code to be as short as possible illustrating the issue. There are too many times users get stuck and just dump everything they have to the question with the expectation that someone else will solve the puzzle :-) .... Things that might help this puzzle would be to log that you are about to make an SMS text (but don't) and show where in the code that is made. Also some data design diagrams might help ... what is the high level "design" of your logic rather than just the code. Posting code is fine as long as it is pertinent. – Kolban Dec 02 '19 at 23:11
  • Although I don't have the answer, since this also happened to me, I can offer some tips. Make a temp db table. All you really need is one column, but an index column helps. With every Twilio send request, record the number. After the bulk send, examine the tmp table for duplicates. YES? Then, your code, not Twilio. – TARKUS Feb 14 '23 at 18:51

1 Answers1

2

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

Alex Baban
  • 11,312
  • 4
  • 30
  • 44