Graph API MS Teams Power Automate
Manish Solanki  

How to mention a teams or channel in channel message using Power Automate?

The introduction of “Send a Microsoft Graph HTTP request” action in Microsoft teams connector has added a new dimension to perform actions which would not have been possible using standard actions. All you need is premium ‘Http’ connector to call the graph api endpoints.

In one of my earlier post I had explained the method to leverage “Send a Microsoft Graph HTTP request‘ action to overcome 28 KB payload message limit: Send inline image in MS team message using power automate (manish-solanki.com)

In this article, I will cover the method to mention both a team and channel in channel message using power automate.

Scenario

The standard “Get an @mention token for a user” action for mentioning works only for a user but not for a team or channel. As an example, I have taken a MS teams name “Power Automate Team” with “General” channel where we will post a message by mentioning both team & channel.

Solution

1.Start with “Get a team” action to get the details of the team like ID, Display name etc. Select the team from the drop down as shown below:

2. Next, add action “List channels” to get the channel of a team. Dynamically, pass the Team ID generated by the previous action as shown below:

3. Add “Filter array” action to get a single channel “General” where we will be posting a message. In the “From” parameter, dynamically pass the output ‘Channel list’ from the previous action:

Click “Edit in advanced mode” link button on the bottom of the action and enter the filter query to get a channel based on its name:

@equals(item()?['displayName'], 'General')

4. Post getting the channel details, add “Apply to each” action and pass output body of filter array action as shown below:

Inside ‘Apply to each’ block, add “Send a Microsoft Graph HTTP request” action to post a message & mention channel in that message. We will be passing the teams & channel ID in the URI. In the body, we will be creating the message body along with mention array. The mention array object contains the channel ID & display name:

URI:
https://graph.microsoft.com/v1.0/teams/@{outputs('Get_a_team')?['body/id']}/channels/@{items('Apply_to_each')?['id']}/messages

Method: POST

Body:
{
  "body": {
    "contentType": "html",
    "content": "<div><div><at id=\"0\">@{items('Apply_to_each')?['displayName']}</at>&nbsp;Hello there!</div></div>"
  },
  "mentions": [
    {
      "id": 0,
      "mentionText": "@{items('Apply_to_each')?['displayName']}",
      "mentioned": {
        "conversation": {
          "id": "@{items('Apply_to_each')?['id']}",
          "displayName": "@{items('Apply_to_each')?['displayName']}",
          "conversationIdentityType": "channel"
        }
      }
    }
  ]
}

Please note that for mentioning channel, the property ‘conversationIdentityType’ should be set as ‘channel’.

5. Finally for mentioning a team in the channel message, add another “Apply to each” action to iterate the channel in the output of filter array action. Pass the output body of filter array action as input to “Apply to each” action:

Add “Send a Microsoft Graph HTTP request” action to post a message in channel and mentioning a team. We will be dynamically setting the teams & channel ID in the URI. The body object contains the teams ID & display name to mention in the message:

URI:
https://graph.microsoft.com/v1.0/teams/@{outputs('Get_a_team')?['body/id']}/channels/@{items('Apply_to_each_2')?['id']}/messages

Method: POST

Body:
{
    "body": {
        "contentType": "html",
        "content": "<div><at id=\"0\">@{outputs('Get_a_team')?['body/displayName']}</at>&nbsp;Hello team, Good Morning!</div></div>"
    },
    "mentions": [
        {
            "id": 0,
            "mentionText": "@{outputs('Get_a_team')?['body/displayName']}",
            "mentioned": {
                "conversation": {
                    "id": "@{outputs('Get_a_team')?['body/id']}",
                    "displayName": "@{outputs('Get_a_team')?['body/displayName']}",
                    "conversationIdentityType": "team"
                }
            }
        }
    ]
}

Please note that for mentioning channel, the property ‘conversationIdentityType’ should be set as ‘team’.

Output

Conclusion

I have demonstrated the technique to mention a team and channel in channel message which otherwise wouldn’t be possible using standard action “Get an @mention token for a user”.

2 thoughts on “How to mention a teams or channel in channel message using Power Automate?

  1. Joe

    I wonder if we can extend this functionality with the group chat too? (e.g. post a message to the group chat, instead of to the channel)
    Thanks

    1. Manish Solanki

      Hi Joe,

      Unfortunately, chat mentioning is only allowed for user, application, team, or channel but does not support for group chat.

      Here are the details: https://learn.microsoft.com/en-us/graph/api/resources/chatmessagemention?view=graph-rest-1.0

      Thanks

Leave A Comment