Skip to content

Idempotency Issue with SMS sending function: Add duplicate message detection #2386

Open
@NullPointer4096

Description

@NullPointer4096

Description:
The current implementation of the ProcessSmsQueueMessage function sends SMS messages without checking for duplicate messages. This can lead to the same message being sent multiple times if the function is triggered more than once with the same message content. To avoid this issue, it's important to add a mechanism for detecting duplicate messages before sending them.

Proposed Solution:
To address this issue, we can query the Twilio service for the messages sent to a specific number and then check if the new message is a duplicate of any of those messages. We can implement a IsDuplicateMessage function to perform this check and call it before sending the SMS message in ProcessSmsQueueMessage. This will prevent sending duplicate messages. An example implementation would be as follows:

public static bool IsDuplicateMessage(string body, string toPhoneNumber)
{
    var messages = MessageResource.Read(
        to: new Twilio.Types.PhoneNumber(toPhoneNumber),
        pageSize: 50
    );

    return messages.Any(message => message.Body == body);
}
public static void SendMessage(string toPhoneNumber, string fromPhoneNumber, string body)
{
    if (!IsDuplicateMessage(body, toPhoneNumber))
    {
        var message = MessageResource.Create(
            body: body,
            from: new Twilio.Types.PhoneNumber(fromPhoneNumber),
            to: new Twilio.Types.PhoneNumber(toPhoneNumber)
        );
        Console.WriteLine($"Sent message: {message.Sid}");
    }
    else
    {
        Console.WriteLine("Duplicate message not sent.");
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions