Pages Menu

Posted by on Jan 9, 2022 in Development, Microsoft Teams

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

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

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 []
        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

Written by Tom Morgan

Tom is a Microsoft Teams Platform developer and Microsoft MVP who has been blogging for over a decade. Find out more.
Buy the book: Building and Developing Apps & Bots for Microsoft Teams. Now available to purchase online with free updates.

Post a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.