Export chats from Microsoft Teams using Power Automate

During one of my demonstrations with a prospective client, who was using ‘Microsoft Teams‘, he asked if it was possible to automate the process of sending a Teams conversation history over e-mail using Power Automate.

The first thing that came to my mind was to use the ‘Microsoft Teams‘ connector and it’s  ‘Get messages‘ action,  but that wasn’t grabbing any of the replies. I was completely stumped as to where to turn next, but then I remembered that few months ago I was exploring ‘Graph API‘ and it had an endpoint to ‘Microsoft Teams‘. After a few quick tests using ‘Graph Explorer’, I found that it was exactly what I was looking for.

In this blog post we’ll use the ‘Graph API‘ with Power Automate, export chats from Microsoft Teams and then send it all as a PDF attachment to an e-mail.

Before we begin, please make sure the following prerequisites are in place:

  • Muhimbi PDF Converter Services Online full, free or trial subscription(Sign up).
  • Appropriate privileges to create Flows.
  • Appropriate permissions to access Graph APIs.
  • Working knowledge of Microsoft Power Automate and Graph API.

From a high-level, our Flow will look like the following:


To facilitate reproducing this method, we’ll separate this post into 2 parts:

  • Part 1: Understand ‘Graph API‘ using ‘Graph Explorer‘.
  • Part 2: Consume the Graph API in Power Automate and Export a chat from a Team and then send it as an e-mail attachment.


Part 1: Understand ‘Graph API’ using ‘Graph Explorer’.

Step 1:

  • Navigate to Graph Explorer in your browser.
  • Login with Office 365, credentials and provide the ‘Consent on behalf of your organization’ an click on ‘Accept‘.



Step 2:

  • Under ‘Sample Queries‘, click on ‘show more samples‘ and Turn ‘ON‘ the ‘Microsoft Team‘ API.



Step 3: 

  • Under the ‘Microsoft Teams(beta)’ click on ‘GET message in a channel‘.
  • Your API endpoint should look like this:
https://graph.microsoft.com/beta/teams/{group-id-for-teams}/channels/{channel-id}/messages/{message-id}

Note: This query requires a group id of the Team, channel id of the corresponding channel of that Team and message id of the message you want to retrieve. To find the group id, channel id and message-id you can run:

  1. GET https://graph.microsoft.com/beta/me/joinedTeams
  2. GET https://graph.microsoft.com/beta/groups/{group-id-for-teams}/channels
  3. GET https://graph.microsoft.com/beta/teams/{group-id-for-teams}/channels/{channel-id}/messages

OR

Open ‘Microsoft Teams‘  on your Desktop, open the channel and click on ‘Get link to channel‘.



The link to your channel should appear like the screenshot below:

https://teams.microsoft.com/l/channel/19%3a627dff9c30554a2b9xxxxxaebac3e744%40thread.tacv2/General?groupId=9feeea9c-55a6-xxxx-98c6-8055fe9efecf&tenantId=686c49db-c967-4b73-xxxxx-550dxxxxxa92b
  • Channel-id: 19:627dff9c30554a2b9xxxxxaebac3e744%40thread.tacv2
  • Group-id-for-teams: 9feeea9c-55a6-xxxx-98c6-8055fe9efecf

Your Final URL :

https://graph.microsoft.com/beta/teams/9feeea9c-55a6-xxxx-98c6-8055fe9efecf/channels/19:627dff9c30554a2b9xxxxxaebac3e744%40thread.tacv2/messages/


Step 4:

  • Copy and Paste above URL and then ‘Run Query‘ in the Graph Explorer.
  • If your endpoint(URL) is correct and if you have the permission to query the Team you should get a ‘Success – Status Code 200‘.


  • Now that we have an understanding of how to retrieve the messages using Graph-API, let’s create a Power Automate(Microsoft Flow).

Part 2: Consume the Graph API in Power Automate and Export chat from a Team and send it as an email.

Step 5:

  • Navigate to the Power Automate portal, click on ‘+ Create’ and select ‘Schedule flow‘.
  • Give you Flow a name, and schedule it for ‘Every day‘ at a specific time.

  • In your Flow designer the action will look like the screenshot below.


Step 6:


Step 7:

  • In the ‘Invoke an HTTP request’ action.
    • Method : GET
    • URL of the request: ‘The URL we created in Step 3’.
  • Save and RUN the ‘Flow‘.


  • Copy the ‘Body‘ to the Clipboard.


Step 8:

  • Add the Parse JSON Action to the Flow designer.
    • Content – Body.
    • Schema – Click on ‘Generate from sample‘, Paste the Content you copied on to your Clipboard(Step).



Step 9:

  • Initialize variables‘ with reference to screenshot below:


Step 10:

  • Add the ‘Set Variable’ action to the Flow canvas and configure it as shown in the screenshot below:
    • Name: HTML.
    • Value:
		  <tr>
		    <td>@{items('Apply_to_each')?['from']?['user']?['displayName']}</td>
		    <td>@{items('Apply_to_each')?['createdDateTime']}</td>
		    <td>@{items('Apply_to_each')?['body']?['content']}</td>
                  </tr>


Note:

The ‘Apply to each’ loop will automatically be added around the ‘Set variable’ action, so that section of your Flow should look similar to the image below:


Step 11:

  • In the ‘Apply to each’ loop add the ‘Set variable’ action.
    • Name: MergedChat.
    • Value:

@{variables(‘HTML’)} output of the ‘Set variable(HTML)’ action. @{variables(‘Temporaryvariable’)} output of the ‘Initialize variable(Temporary variable) action.




Step 12:

  • In the ‘Apply to each’ loop, add the ‘Set variable’ action.
    • Name: Temporaryvariable .
    • Value: @{variables(‘MergedChat’)} output of the ‘Set variable(Merged)‘ action.



Step 13:

  • Outside the ‘Apply to each’ loop, add the ‘Compose‘  and, add the ‘Temporary variable‘ in the HTML with reference to screenshot below:



	<!DOCTYPE html>
	<html>
	<head>
	<style>
	table {
	  font-family: arial, sans-serif;
	  border-collapse: collapse;
	  width: 100%;
	}
	td, th {
	  border: 1px solid #dddddd;
	  text-align: left;
	  padding: 8px;
	}
	tr:nth-child(even) {
	  background-color: #dddddd;
	}
	</style>
	</head>
	<body>
	<h2>Chat Backup</h2>
	<table>

	@{variables('Tempvariable')}

	</table>
	</body>
</html>


Step 14:

  • Add the Muhimbi ‘Convert HTML to PDF‘ action in Flow designer.
  • In the ‘Source URL or HTML’ section, add the ‘@{outputs(‘Compose’)}’ action.
  • In the ‘Media Type’ field, select the ‘Print’ option from the drop down menu. (This uses the Print specific CSS).



Step 15:

  • Add the ‘Send an email(v2)’ action and configure it with reference to instruction below.
    • To: Managers email address.
    • Subject: concat(‘Chat report’,utcNow()).pdf
    • Body: Email Body.
    • Attachments Name – 1: concat(‘Chat report’,utcNow()).pdf
    • Attachments Content -1: ‘Processed File Content’ the output of ‘Convert HTML to PDF‘ action.



Step 16:

  • That’s it- you’re done! Select the Test button from the top of your screen.
  • In the Test pane, select the options ‘I’ll perform the trigger action‘ for testing your flow:




  • After few seconds, you should receive an e-mail with the Chat report as a PDF attachment.



Subscribe to this blog for the latest updates about SharePoint Online, Microsoft Flow, Power Apps and document conversion and manipulation.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s