Get Replies to Teams Thread in Power Automate with Graph API

Paul Nieuwelaar, 19 May 2021

The Microsoft Teams connector for Power Automate is a bit limited when it comes to messages and replies. With the built in Teams connector, we can only get ALL messages in a channel, using the “Get messages (preview)” action.

If we want to get just the replies to a specific Teams message, we can use the Microsoft Graph API List message replies endpoint with a Power Automate HTTP action, which allows us to fill that gap left by the current Teams Connector for Power Automate.

Creating the App Registration

To get started with the Graph API, we need to create an App Registration in Azure AD. Call this anything meaningful, I’ve called mine Teams Connector Test. For this example, I’m only using this internally, so I’m selecting Accounts in this org directory only (single tenant).

image

Once you’ve created the app registration, make a note of the Application (client) ID, and the Directory (tenant) ID, as these are both used with the HTTP request authentication later in Power Automate.

image

Next, go to Certificates & secrets, and create a new client secret. Once created, make a note of the value, as this is also needed in the HTTP request later. If you lose this value, you can create another secret at any time.

clip_image005

Finally, you need to grant permissions to the app registration to allow it to access the Graph API’s Teams endpoints. From API permissions, select Add a permission, and select Microsoft Graph.

clip_image007

When asked to add Delegated permissions or Application permissions, select Application permissions.

clip_image009

Now select the Team.ReadBasic.All, Channel.ReadBasic.All, and ChannelMessage.Read.All permissions. This will give our app access to the Replies endpoint, which we want to use.

clip_image011

Once the permissions have been added, they each require admin consent before they can be used. Click the Grant admin consent button to grant access to all three.

clip_image013

The app registration should now be set up correctly, and you should have the following 3 values needed for the authentication.

Tenant ID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Client ID: 1127d9ea-4c77-4974-8e85-58d5bb72b4f1

Secret: zF8ycuIBBX.Y6x-2A0dg3~q8Fbblx-cm~.

Asking Microsoft Nicely to Use Their APIs

This is where it gets a bit weird. Some of the Microsoft Graph APIs are protected APIs, and you have to request access from Microsoft before you can use them, even within your own tenant.

You can request access here: https://aka.ms/teamsgraph/requestaccess. It can take a couple of days for access to be granted, and you’ll be emailed once this happens. In the request, you’ll have to specify the App registration ID to grant access to, which will allow you to use that app registration only, once it’s been approved. If you’re only requesting access for internal use, you don’t have to provide as much info, otherwise you’ll need to provide a bunch of app details and website links as well.

clip_image015

Setting up the Power Automate Flow

Finally, you should have everything you need now to use the replies API. You can test this from Postman or anything else to test an HTTP GET request, but it’s quite easy to just test it in Power Automate.

In my Flow, I’m using the Microsoft Teams Trigger: For a selected message, which shows a button in the More Actions menu on a message in Teams to trigger the Flow.

Next, I’ve added an HTTP action. The Method is GET, and the URI is copied from the List message replies documentation, but I’ve dynamically plugged in the Team ID, Channel ID, and Message ID from the selected message step in my Flow.

GET /teams/{team-id}/channels/{channel-id}/messages/{message-id}/replies

The full URI looks like this, which can be copied directly into Flow to maintain the dynamic values:

https://graph.microsoft.com/v1.0/teams/@{triggerBody()?['entity']?['teamsFlowRunContext']?['channelData']?['team']?['aadGroupId']}/channels/@{triggerBody()?['entity']?['teamsFlowRunContext']?['channelData']?['channel']?['id']}/messages/@{triggerBody()?['entity']?['teamsFlowRunContext']?['messagePayload']?['id']}/replies

We can use Active Directory OAuth Authentication, and plug in all our values copied from the App registration we created.

· Authentication: Active Directory OAuth

· Authority: https://login.microsoftonline.com

· Tenant: [Copied from earlier]

· Audience: https://graph.microsoft.com

· Client ID: [Copied from earlier]

· Credential Type: Secret

· Secret: [Copied from earlier]

clip_image016

The response from this will be a bunch of JSON that Power Automate won’t understand automatically, so we need to add a Parse JSON action to tell it what to expect. The easiest way to do this is to simply run the Flow at this point and copy the JSON from the body of the HTTP OUTPUTS (Show raw outputs). Copy everything from the “body”, which is the raw response from the HTTP request.

clip_image018

From a Parse JSON action, we can now use the Generate from sample button and paste in the whole response JSON copied from the HTTP request earlier.

clip_image020

You should now have the correct schema defined. Make sure you’re also passing in the Body from the HTTP action, which is what will be parsed.

clip_image022

That’s pretty much it. You’ll now be able to use the “value” array, which contains each of the replies for the message you specified. In my example below, I’m simply looping through each of the replies within the value array, and I’m appending each reply to a string.

The values I’m getting for each reply includes the createdDateTime, displayName (of the user), and the content (the actual message HTML). There are other values that can be accessed as well if needed, which you should be able to see now within Power Automate.

clip_image024

When you run the flow, you can see the replies being looped through, and in my case, each are appended to my string.

clip_image026

Conclusion

Using the Graph API we can now easily get replies to a Teams message within Power Automate. This can be used to create simple integrations, or we can create more complex integrations by subscribing to changes.

The Microsoft Teams Connector built into Power Automate still has a lot of actions in preview, so maybe we’ll have better support for replies in the future, but for now, we can fill those gaps using the Graph API and HTTP actions.