Teams Bot Issue - Multiple (Repeated) ConversationUpdate/MembersAdded Messages

Posted on February 13th, 2020

I'm working on test notes for submission of the ChitChattr apps to the Teams store, and part of that is testing the adding of the apps for new users. Doing so, my bots were sending two "welcome" messages every time a user added them. I checked across environments, and of course debugged locally using an NGrok session, and it turned out that the bots, when added as apps (i.e. 1-1 with a new user) were receiving multiple converationUpdate/membersAdded messages. Now, I'm pretty sure this was working fine before, but it's been a while since I added the bot to a completely new user, so maybe I'm wrong.

Looking into it, over in this doc, it says that:

The conversationUpdate event is sent to your bot when it receives information on membership updates for teams where it has been added. It also receives (emphasis added) an update when it has been added for the first time specifically for personal conversations.

So now I was confused. Does "also receives" means it's another case when it will receive a message, or that it will two messages? Truth be told, either would have been fine, because my OnMembersAdded checks to see if it's the Bot that's being added, something like:

foreach (var member in membersAdded)
    {
        if (member.Id == turnContext.Activity.Recipient.Id) 
        {
           await SendWelcomeMessage(turnContext);
           break;
        }
   }

But, the problem was still occurring, even in this case. When I looked into the messages being received, the problem became clear. I'm getting, from Teams, something like:

...
    "membersAdded": [
        {
            "id": "29:...",
            "aadObjectId": "[guid]"
        },
        {
            "id": "28:..."
        }
    ],
    "type": "conversationUpdate",
    "timestamp": "...",
    "id": "f:guid",
...

As you can see from the payload, I'm getting BOTH the user ("29:...") AND the Bot's ID ("28:..."). Now, this is a bit wonky. I should be receiving EITHER two messages, one for each "person" joining the 1-1 conversation (i.e. one for the user and one for the Bot) OR one message, with both IDs, but surely not TWO messages with TWO IDs??

Things got even stranger when I headed over here, where it says:

For personal scoped bots, your bot will only ever receive the conversationUpdate event a single time, even if the bot is removed and re-added.

What made this strange was two separate things:

  1. I'm receiving TWO, not "a single" event
  2. When I remove and re-add the bot, it actually DOES send another new "conversationUpdate" event

So by now I'm pretty sure I'm either (a) going crazy (not impossible) or (b) misunderstanding the docs. I thought (b) was (hopefully) more likely, so I even tried to implement bot state management to try and make sure I was sending only a single welcome message, and there's a workaround I'm working on, but in the meantime I posted it on Stack Overflow, hoping someone would spot something I'd missed.

In the end, it turns out, as per the Stack Overflow comments, that it's actually a bug in Teams! That means I'm neither crazy, nor misunderstanding things - all good news. In fact, as I look at things now, it's very likely that items (2) and (1) above are related - I suspect someone was trying to "fix" the fact that Teams doesn't send a new "conversationUpdate" when a bot is removed and re-added, and ended up introducting a regression. Either way, the Teams team is clearly aware of it and I'm sure working hard on a fix.

In the meantime, hopefully this information will be of use to anyone who runs into this and, as I mentioned, I'm working on a workaround that I'll try post if I get it working, in case it helps someone else.

-- Hilton