Search code examples
c#botframeworkmicrosoft-teams

TeamsBot - How do I get the Team Id from a conversationupdate event?


I'm installing a Teams bot and adding it to a team. When I add it to the team, on the initial call I get the "conversationUpdate" event correctly, in OnTurnAsync. The issue I'm running into is that any call to get the team itself is failing because there is nothing there, that I can see, for getting the Id to call to get the team data.

I want to be able to add it to the team, get that event and then get the data related to the team it was added to.

Here is what I'm trying.

        private const string ActOnType = "conversationUpdate";

        public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default)
        {
            if (IsConversationUpdate(turnContext))
            {
                // This fails because TeamsGetInfo is returning null
                var teamDetails = await TeamsInfo.GetTeamDetailsAsync(turnContext,  turnContext.Activity.TeamsGetTeamInfo().Id, cancellationToken);
            }
        }

        private bool IsConversationUpdate(ITurnContext turnContext) => string.Equals(turnContext.Activity.Type,
            ActOnType, StringComparison.InvariantCultureIgnoreCase);

Other notes...
turnContext.Activity.TeamsGetChannelId() is null, as is ChannelData. Any further calls have the channel data, but the initial one where I add the bot to the team does not have any.

Adding my JSON from the call

{
    "membersAdded": [
        {
            "id": "29:1HqtPeQvYpNL682R_NeBMndg6kYbZbBHsZliZdAT2xWsJWzS0_b50S1ijo2hmPu5i0HTrvqPiOBxqpbtJjS7zyja",
            "aadObjectId": "{valid guid}"
        },
        {
            "id": "28:{valid guid}"
        }
    ],
    "type": "conversationUpdate",
    "timestamp": "2021-01-11T18:15:49.9118003Z",
    "id": "f:{valid guid}",
    "channelId": "msteams",
    "serviceUrl": "https://smba.trafficmanager.net/amer/",
    "from": {
        "id": "29:1HqtPeQvYpNL682R_NeBMndg6kXaZbBHsZliZdAT2xWsJWzS0_gOS1ijo2hmPu5i0HTrvqPiOBxqpbtJjS6zyjA",
        "aadObjectId": "{valid guid}"
    },
    "conversation": {
        "conversationType": "personal",
        "tenantId": "{valid guid}",
        "id": "a:1UgWdBcfpF4JUtOCCjQZsvhjl-QK9YBBnALG7ACgA0QdKx_xGICsQ3d6l94t_pPed7fvtnwSnRlYcWe7kXT7dStP-YCtuvliI8GPZj9Sd5P2wHsBAAn1ibwdad4Lq-O3B"
    },
    "recipient": {
        "id": "28:{valid guid}",
        "name": "LocalBot"
    },
    "channelData": {
        "tenant": {
            "id": "{valid guid}"
        }
    }
}  

Solution

  • I'll post the answer here in case anyone comes into it...

    You can only get this event once for when the bot was added to the team. If you miss it, you no longer get the data. Uninstalling and reinstalling the bot will not fire the event again. If you're still not getting it, the recommended action was to remove the app from Azure and re-do it with the same name.

    See more on the github discussion

    In our scenario, I decided that the limitations on this were not within the realm of what I considered a working solution and I decided to go down a different route for getting the team data. Since the data was coming through in events after this one, I grab it then and do what I need to do with it.