0

I'm creating a chat bot using WhatsApp cloud api but for some reason if a send a message I'm receiving multiple notifications of the same message. I have re-configured the webhooks but its still spamming me. I have tested my code and its returning 200 which is the expected response but still I can't figure out what I'm doing wrong.

This is the code I'm using. I'm just sending a welcome message if the user sends "hi".

public async Task ProcessMessageAsync(dynamic receivedMessage)
    {
        string profileName = string.Empty;

        TextMessageReceived? textMessageReceived = JsonSerializer.Deserialize<TextMessageReceived>(receivedMessage);

        if (textMessageReceived != null)
        {
            try
            {
                TextMessageEntry textMessageEntry = textMessageReceived.Entry[0];

                TextMessageChange textMessageChange = textMessageEntry.Changes[0];

                TextMessageValue? textMessageValue = textMessageChange.Value;
                if (textMessageValue != null)
                {
                    if (textMessageValue.Messages != null)
                    {
                        foreach (TextMessageContact textMessageContact in textMessageValue.Contacts!)
                        {
                            profileName = textMessageContact.Profile!.Name!;
                        }
                        TextMessage textMessage = textMessageValue.Messages[0];
                        if (textMessage != null)
                        {
                            string from = textMessage.From;
                            string messageBody = textMessage.Text?.Body!;
                            
                           if (messageBody.Equals("hi") || messageBody.Equals("HI") || messageBody.Equals("hie"))
                                {
                                    // Send welcome message
                                    string welcome = $"Hi {profileName}. Thank you for getting in touch with Insure-Tech.";
                                    WhatsAppText whatsAppText = new(false, welcome);
                                    TextMessageRequest textMessageRequest = new(from, whatsAppText);
                                    var response = await _messagingService.SendTextMessageAsync(textMessageRequest);

                                    }
                            
                        }
                    }
                }
            }
            catch { }

        }

    }

Has anyone faced a similar issue and know how to resolve it?

Edgias
  • 77
  • 3
  • 10

1 Answers1

0

I also experienced this issue. I believe this is the same or similar to issue reported here. I managed to work around it by looking at the message's timestamp. Check my answer here for more details.

tariro
  • 51
  • 2
  • 4