Guest Mike Francis Posted April 4, 2023 Posted April 4, 2023 You may have the need to archive chat messages or capture chat sessions for further processing. Activities like summarizing the conversation, extract action items, or suggesting meeting times can be done by analyzing the chat conversation. This starts with extracting the conversation using the Microsoft Graph APIs. In Microsoft Teams, a chat conversation is made up of messages and replies. A 'message' is the initiating text. Responses to the initial prompt by recipients of the message and the message initiator are 'replies'. For example: In this case the 'message' is 'Where did you want to meet for lunch?'. The remainder of the conversation are 'replies'. The Microsoft Teams Connector, provides methods we need to extract the messages. However, no method is provided for collecting the message replies. For this, we will call the Microsoft Graph using a custom connector. An exported solution with the following implemented can be downloaded here. Requirements Scope The solution must only capture messages for Teams that a specified user (Service Account) is a member. The solution must capture both messages and replies for that message. The solution must capture messages in both public and private channels. Tools The Postman Graph API collection is very useful for experimenting with the Graph APIs. See here: Use Postman with the Microsoft Graph API - Microsoft Graph | Microsoft Learn This useful for getting the Team, Channel and Message ids you'll need for testing the custom connector. Implementation In Azure Active Directory (AAD) - For the global admin - Create a Service account and app registration Create a Service Account (A user account not associated with an actual user) with the default 'User' role. Create an App registration with the following delegated permissions: These permissions require consent by the Global admin for the organization. In 'Authentication' Add the 'Web' platform and specify the following Redirect URI: https://global.consent.azure-apim.net/redirect. Create a secret. Note the secret, Client Id, and Tenant Id, of the app registration. You'll need these when creating the custom connector. Assign the Power Apps, and Microsoft 365 licenses as you normally do when onboarding users. Power Automate - For app makers - Create the Custom Connector for calling the MS Graph In this step we'll create a custom connector that will call the MS Graph API: GET /teams/{team-id}/channels/{channel-id}/messages/{message-id}/replies Connector Name: GetMessageReplies Key Fields: General: Host: graph.microsoft.com Security: Authentication Type: OAuth 2.0 Identity Provider: Azure Active Directory Client ID: <client id from AAD section above> Authorization URL: Sign in to your account Tenant ID: common Resource URL: Microsoft Graph Dev Center | APIs and app development Redirect URL: https://global.consent.azure-apim.net/redirect Definition: General: Summary: Get message replies Description: Get message replies Operation ID: Get-message-replies Visibility: Important Request: Import from sample: GET URL: https://graph.microsoft.com/beta/teams/{TeamId}/channels/{ChannelId}/messages/{MessageId}/replies // Response: // Import from sample: { "@odata.context": "https://graph.microsoft.com/beta/$metadata#teams('eb825a11-3b12-4c5f-8258-fd405b1395c8')/channels('19%3Aq0-BRQ6TDgcZczl9O7V2Ye078k1eOIzgghUkpRzTlW41%40thread.tacv2')/messages('1677699468267')/replies", "@odata.count": 0, "value": [] } Test: Click Update connector to save. Create a connection using the Service Account created in the AAD section above. Provide a TeamId, ChannelId, and Message Id, acquired from the PostMan Graph API helper. (See Tools section.) Click 'Test operation'. A successful test will return a 200 status code. See screenshot below. Power Automate - For app maker - Create Scheduled and Power App Flows In the following steps, we'll create a flow that will collect chat messages and replies for every Team the service account is a member. The idea here is that an admin would add this user account to each Team where they want to extract the chat messages. In this step we'll create a scheduled flow that calls a child flow that will capture the messages and replies and pass them back to the calling flow. This is required to have the flow run in the context of the 'run-only' user, which will be the Service Account created in the AAD section above. Create Child Flow Let create the child flow first. Create an Instant flow with a PowerApps V2 trigger. Call this flow GetChatMessages. Initialize an array variable called 'result'. Use the Microsoft Teams Connector to call the following. The channels, message, and replies should be in nested 'Apply to each' controls: List Teams - Specify the Service Account for the connection reference. List Channels Get Messages Get replies for message - here you will call the custom connector. Append the current team, channel, message and replies to the 'result' array. Covert the array to a string and return to calling flow. Save the Flow On the Flow's detail page, edit the 'Run only users' and set the 'Connections Used' for Microsoft Teams and GetAllTeams (Custom Connector) to be the Service Account. Share the flow with the Service Account. Optionally specify the Service Account as an owner of the flow. Create Main Flow Create a Scheduled flow. Name this GetTeamsChannelsMessages. By default, the flow will run every minute, you may want to change this to a longer interval (weekly) while you are developing the flow. You can always trigger this manually during development. Add the action to call a Child Flow. Specify GetChatMessages created above. Save the Flow Share the flow with the Service Account. Optionally specify the Service Account as an owner of the flow. When your testing is complete, update the schedule to the interval you need. You should run a few tests to determine the maximum runtime for the flow and adjust the schedule accordingly. Results Here is an example of the output of the chat in the first screenshot: { "Team": "Demo Team", "Channel": "Project Silver", "MessageId": "1677098428650", "MessageText": "Where did you want to meet for lunch?", "ReplyId": "1677098885814", "ReplyText": "I'd don't want to go all the way out to Dunmore." }, { "Team": "Demo Team", "Channel": "Project Silver", "MessageId": "1677098428650", "MessageText": "Where did you want to meet for lunch?", "ReplyId": "1677098838074", "ReplyText": "No seafood. How about Cugino's?" }, { "Team": "Demo Team", "Channel": "Project Silver", "MessageId": "1677098428650", "MessageText": "Where did you want to meet for lunch?", "ReplyId": "1677098447360", "ReplyText": "How about Cooper's?" } Continue reading... Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.