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

谁说胖子不能爱 提交于 2021-01-27 07:10:01

问题


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}"
        }
    }
}  

回答1:


You can find these channel data in OnMessageActivityAsync also. enter image description here




回答2:


Searched around a bit in the source code at https://github.com/microsoft/botbuilder-dotnet and found two things that indicate that there is no Channel in your turnContext.

Here's the code:

// Microsoft.Bot.Builder.Teams.TeamsActivityExtensions
public static TeamInfo TeamsGetTeamInfo(this IActivity activity)
{
    var channelData = activity.GetChannelData<TeamsChannelData>();
    return channelData?.Team;
}

// Microsoft.Bot.Schema.Activity
public TypeT GetChannelData<TypeT>()
{
    if (this.ChannelData == null)
    {
        return default(TypeT);
    }

    if (this.ChannelData.GetType() == typeof(TypeT))
    {
        return (TypeT)this.ChannelData;
    }

    return ((JObject)this.ChannelData).ToObject<TypeT>();
}





回答3:


I'm looking at an old conversationUpdate log I have, and there's definitely a TeamId in there - I can't imagine it's been removed. You can check if you're working locally, by looking at the ngrok logs (http://127.0.0.1:4040). If it IS there, it might just not be populating onto the TeamsChannelData for some reason, but you should still be able to access it fine, directly, by accessing activity.ChannelData itself.

[Update] This is a sample from a old payload I saved from a some time in 2020:

"channelData": {
        "team": {
            "id": "19:02...55@thread.skype",
            "name": "[Group Name]",
            "aadGroupId": "[Guid]"
        },
        "eventType": "teamMemberAdded",
        "tenant": {
            "id": "[Guid]"
        }
    }



回答4:


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.



来源:https://stackoverflow.com/questions/65669683/teamsbot-how-do-i-get-the-team-id-from-a-conversationupdate-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!