Copilot for Microsoft 365 Tech Accelerator
Feb 28 2024 07:00 AM - Feb 29 2024 10:30 AM (PST)
Microsoft Tech Community
Microsoft Teams: Capture Chat Messages and Replies
Published Apr 04 2023 09:53 AM 6,229 Views
Microsoft

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:

GraphChatMessagesandReplies.gif

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

  1. The solution must only capture messages for Teams that a specified user (Service Account) is a member.
  2. The solution must capture both messages and replies for that message.
  3. 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

  1. Create a Service Account (A user account not associated with an actual user) with the default 'User' role.
  2. Create an App registration with the following delegated permissions:

permissions.png

  1. These permissions require consent by the Global admin for the organization.
  2. In 'Authentication' Add the 'Web' platform and specify the following Redirect URI: https://global.consent.azure-apim.net/redirect.
  3. Create a secret. Note the secret, Client Id, and Tenant Id, of the app registration. You'll need these when creating the custom connector.
  4. 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: https://login.microsoftonline.com
Tenant ID: common
Resource URL: https://graph.microsoft.com
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:

  1. Click Update connector to save.
  2. Create a connection using the Service Account created in the AAD section above.
  3. Provide a TeamId, ChannelId, and Message Id, acquired from the PostMan Graph API helper. (See Tools section.)
  4. Click 'Test operation'. A successful test will return a 200 status code. See screenshot below.

customconnector.png

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

  1. Let create the child flow first. Create an Instant flow with a PowerApps V2 trigger. Call this flow GetChatMessages.
  2. Initialize an array variable called 'result'.
  3. Use the Microsoft Teams Connector to call the following. The channels, message, and replies should be in nested 'Apply to each' controls:
    1. List Teams - Specify the Service Account for the connection reference.
    2. List Channels
    3. Get Messages
    4. Get replies for message - here you will call the custom connector.

flow.png

  1. Append the current team, channel, message and replies to the 'result' array.

flow2.png

  1. Covert the array to a string and return to calling flow.

flow3.png

  1. Save the Flow

  2. 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.

connections.png

  1. Share the flow with the Service Account.
  2. Optionally specify the Service Account as an owner of the flow.

Create Main Flow

  1. 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.
  2. Add the action to call a Child Flow. Specify GetChatMessages created above.
  3. Save the Flow
  4. Share the flow with the Service Account.
  5. Optionally specify the Service Account as an owner of the flow.
  6. 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?"
}

 

1 Comment
Co-Authors
Version history
Last update:
‎May 19 2023 02:03 PM
Updated by: