Forum Discussion

Maury33308's avatar
Maury33308
Copper Contributor
Sep 20, 2023

Teams sending multiple responses to interactive bot

Hello,

 

We have developed an interactive chat bot as a Teams plugin using the Spring Bot Integration framework provided by Microsoft.  Users can interact with the chat bot by clicking buttons in Adaptive Cards sent by our bot.

 

In general everything works just fine, but from time to time I receive the same message several times when a user clicks a button in a message that was sent earlier.  The contents of the message are exactly the same, except for the ID of the message. 

 

Based on what I understand, there are two reasons for this:

1. The user clicked on the button multiple times

2. Something in Teams is sending the message multiple times behind the scenes (ex. retry because it is unable to communicate with the backend)

 

I can't say that the first option is not the problem, but in certain cases, the times in the messages are several milliseconds apart. I'm not sure that a user is even capable of clicking a button twice in < 50 milliseconds, which leads me to believe that Teams is sending the message multiple times behind the scenes.  

 

Has anyone experienced this phenomenon?

 

Thanks,

Maurice

    • Maury33308's avatar
      Maury33308
      Copper Contributor

      Thank you for your response Prasad_Das-MSFT.

       

      The problem is happening when I am sending the message, but when the user clicks a button in Teams to send me their response.

      • Prasad_Das-MSFT's avatar
        Prasad_Das-MSFT
        Icon for Microsoft rankMicrosoft

        Maury33308 - To prevent multiple messages from being sent in quick succession, you can implement throttling in your code. Throttling limits the rate at which messages can be sent, ensuring that only one message is sent at a time. Rate limiting for bots - Teams | Microsoft Learn

        Also, you can check for event duplication: It's possible that the button click event is being triggered multiple times due to event duplication. This can happen if there are multiple event handlers registered for the same event. Make sure that you have only one event handler registered for the button click event to avoid duplicate messages.

        Here is an example of how to send a message in response to a button click event using the Teams JavaScript SDK:

        // Handle button click event
        async function handleButtonClick(event) {
          // Get the conversation ID and user ID
          const conversationId = event.conversation.id;
          const userId = event.user.id;
        
          try {
            // Create the conversation
            const conversation = await teamsClient.conversations.createConversation({
              bot: { id: '<your-bot-id>' },
              members: [{ id: userId }],
            });
        
            // Get the conversation ID
            const newConversationId = conversation.id;
        
            // Send the message
            await teamsClient.conversations.sendToConversation(newConversationId, {
              type: 'message',
              text: 'This is a response to the button click event.',
            });
          } catch (error) {
            console.error('Error sending message:', error);
          }
        }

         

        Thanks, 

        Prasad Das

        ------------------------------------------------------------------------------------------ 

        If the response is helpful, please click "**Mark as Best Response**" and like it. You can share your feedback via Microsoft Teams Developer Feedback link.

Resources