Get your Microsoft Teams One on One chats with Power Automate using the Graph API

You can get some details of One on One chats in Microsoft Teams using the Microsoft Teams connector in Power Automate, but getting the actual details is slightly harder.

Chats and the Teams Connector

Using the List chats action you can choose to get chats of the different types available. This way you can get the chats that are within Groups, Meetings or the One on One chats.

Get your Microsoft Teams One on One chats with Power Automate using the Graph API Microsoft Graph, Microsoft Power Apps, Microsoft Power Automate image 18

When we run this flow however, we will only see very little details of the flow. So how do we get to the message details?

Get your Microsoft Teams One on One chats with Power Automate using the Graph API Microsoft Graph, Microsoft Power Apps, Microsoft Power Automate image 19

We will need to use the Graph API here.

I’ve checked the various connectors that support Graph API calls, but none of them support the Chat/Messages endpoints. It is time to go back to the HTTP/Custom connector option.

Creating the HTTP request

For more details on how to setup the Graph API in an HTTP request action please have a read through my blog post series on the Graph API and the Power Platform.

To set up the app registration, please make sure that you have included one of the following API permissions, if you are creating a custom connector.:

Get your Microsoft Teams One on One chats with Power Automate using the Graph API Microsoft Graph, Microsoft Power Apps, Microsoft Power Automate image 20

And if you are using the HTTP request action then you will need the application permission instead of the delegated ones.

Get your Microsoft Teams One on One chats with Power Automate using the Graph API Microsoft Graph, Microsoft Power Apps, Microsoft Power Automate image 21

Now we can build an HTTP request returning all the chats.

Get your Microsoft Teams One on One chats with Power Automate using the Graph API Microsoft Graph, Microsoft Power Apps, Microsoft Power Automate image 22

Notice that the above request will collect all the chats and not just the one to one chats.

First thing to add is a filter. This filter will make sure that we only get the chats of the Chat type One On One..

Get your Microsoft Teams One on One chats with Power Automate using the Graph API Microsoft Graph, Microsoft Power Apps, Microsoft Power Automate image 23

So far I’ve just replicated the List Chat action. Potentially I could just have used the List Chats action. Although, in the above example we will have a bit more flexibility on collecting chats for a specific user Account. Also the data returned has some additional properties in it.

The data returned should look like this:

{
      "id": "19:092e5a70------------------------f94097890@unq.gbl.spaces",
      "topic": null,
      "createdDateTime": "2022-11-30T17:33:12.424Z",
      "lastUpdatedDateTime": "2022-11-30T17:33:16.25Z",
      "chatType": "oneOnOne",
      "webUrl": "https://teams.microsoft.com/l/chat/19%3A092e5a70-3549-40ff-8fb0-e3d84ac-ae0f94097890%40unq.gbl.spaces/0?tenantId=3985---------------------e47",
      "tenantId": "3985--------------------------e47",
      "viewpoint": null,
      "onlineMeetingInfo": null
    }

Now the next part of this solution is to get the message details.

Getting the Message Details on the One On One Chats

So the next step in the process is to get the messages of a chat. I can now build up my flow and collect the messages for my user account with the following HTTP request action.

But …

Get your Microsoft Teams One on One chats with Power Automate using the Graph API Microsoft Graph, Microsoft Power Apps, Microsoft Power Automate image 24

When we run this flow we will get the following message:

Invoked API requires Protected API access in application-only context when not using Resource Specific Consent. Visit https://docs.microsoft.com/en-us/graph/teams-protected-apis for more details.

Get your Microsoft Teams One on One chats with Power Automate using the Graph API Microsoft Graph, Microsoft Power Apps, Microsoft Power Automate image 25

So, this API is protected and we can’t get to it without specifically requesting access from Microsoft to use the requested API.

Me vs Users problem

If we ran the flow and used the https://graph.microsoft.com/v1.0/me/chats endpoint rather than the user equivalent, then we would run into issues resulting in /me request is only valid with delegated authentication flow.

So the only option is to create a custom connector.

Creating the Custom connector with Microsoft Graph

For more details on how to create a custom connector, please have a look at my Custom Connector with Microsoft Graph post.

When we import the endpoint that we want to use, we will see the following dialog. This is where all the magic will happen.

Get your Microsoft Teams One on One chats with Power Automate using the Graph API Microsoft Graph, Microsoft Power Apps, Microsoft Power Automate image 27

Now you could decide to make the Content Type and Accept header hidden and required as thee values will always be set to the same.

Get your Microsoft Teams One on One chats with Power Automate using the Graph API Microsoft Graph, Microsoft Power Apps, Microsoft Power Automate image 26

And then when we get to building our flow, we can collect the text of all the messages in a chat.

One On One Chats details in Power Automate

If you want to collect all the messages together into one array you could of course use Pieter’s method to do so. But this all depends on what you want to do with the messages found.

Continue Reading Pieter Veenstra’s Article on their blog

Get your Microsoft Teams One on One chats with Power Automate using the Graph API

You can get some details of One on One chats in Microsoft Teams using the Microsoft Teams connector in Power Automate, but getting the actual details is slightly harder. Using the List chats action you can choose to get chats of the different types available.

Blog Syndicated with Pieter Veenstra’s Permission

Author: Pieter Veenstra

Share This Post On
Share via
Copy link
Powered by Social Snap