Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
NullPointer4096 opened this issue Apr 25, 2023 · 0 comments

Comments

@NullPointer4096
Copy link

NullPointer4096 commented Apr 25, 2023

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.");
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant