“It wasn’t me, boss!” You can now identify Microsoft Teams chat messages sent by bots on behalf of users

Tom Morgan
2 min readJan 10, 2022

A small but important change recently rolled out to Microsoft Graph. The chatMessage resource (which is the object type that contains information about chat messages sent and received in Microsoft Teams) now has a new property: onBehalfOf. This provides additional information about the user, when the message itself is actually sent by a bot. Note that right now this property is only available in /beta, it is not yet in the v1.0 endpoint.

You can use this information in a few different ways. First, just reading Graph — this now shows you messages which are being sent by bots but triggered by user actions or tied to specific users. This might be useful information for reporting but also for compliance scenarios.

Secondly, as a developer, you can include this onBehalfOf property when sending messages. For instance, if your bot code is posting a card to a channel as a result as action by a specific user you can now specify this user. In C#, you do this by adding a OnBehalfOf property to ChannelData in your Activity payload (as long as you are using v4.6 or later of the Bot Framework SDK):

OnBehalfOf = new []
{
new
{
ItemId = 0,
MentionType = "person",
Mri = turnContext.Activity.From.Id,
DisplayName = turnContext.Activity.From.Name
}
}

How does this look in Teams?

Where the onBehalfOf information is present, Teams will use it when building the title of the author of the post. Instead of the title just showing the name of the bot, the syntax {user} via {bot} is used. You can see the difference in the image below: the one on the right has the onBehalfOf information present:

ref: User attribution for bots messages

--

--

Tom Morgan

#MicrosoftTeams Platform Dev & related tech, #MVPBuzz, videos at http://thoughtstuff.co.uk/video, works @modalitysystems but views my own