Sending message with atmention onto a channel using GraphAPI .. and replying it

You can send a ”ordinary” message to Team channel using a Flow Connector. But what about if you want to atmention a channel in the same message? And reply to a message..

The documentation to Create Message can be found at Docs.Microsoft.Com

You can use Graph Explorer or other Graph API calls to resolve those ids. For testing this out, Graph Explorer is a great tool. Get your teams with a simple call and choose a team to get it’s id.

 https://graph.microsoft.com/v1.0/me/joinedTeams

Result contains blocks like this
{
"id": "Here is the GUID for Team ID. Copy this somewhere",
"displayName": "TechDays Dev&Test",
"description": "Description text that should be filled in..",
"isArchived": false
},

Do another call with Graph Explorer to get Channel is, replace {team-id} with your just copied team id.

 https://graph.microsoft.com/v1.0/teams/{team-id}/channels

Result contains channels with IDs.
Copy the Whole ChannelID with 19: and @.thread.skype. 
{
"id": "19:9216effx2516d4560x32c59d1b875d527@thread.skype",
"displayName": "General",
"description": "Description text that should be filled in.."
},

Create Message

The POST call to create the message is as follows:

  https://graph.microsoft.com/beta/teams/{TeamID}/channels/{ChannelID)/messages 

And the actual Request body contains the magic needed to create message and atmention the channel (or person or team). Don’t forget to set Content-Type to application/json. Some spaces should be taken off at at id part. Originally I also had some div-elements there but WordPress didn’t feel comfortable with showing these at.

{ 
"importance": "high",
"subject": "Welcome Teams Messaging!",
"body":
{
  "contentType": "html",
  "content": "
This is our team channel test message. \n < at id ="0">
  General channel mention
< / at>  "
} ,
"mentions": [ {
  "id":0,
  "mentionText": "General Channel Mention",
  "mentioned": {
    "application": null,
    "device": null,
    "conversation": null,
    "conversation": {
      "id": "{ChannelID}",
      "displayName": "General",
      "conversationIdentityType@odata.type": "#Microsoft.Teams.GraphSvc.conversationIdentityType",
      "conversationIdentityType": "channel"
     } 
 }}]
}

The trick is at the at tag and listing atmentioned targets. The actual display names will be replaced with real names – such as channel or team names.

Perhaps a reply is in order …

Looking at the POST response from Graph API we can see what is the new message’s ID.

This article describes how to reply: https://docs.microsoft.com/en-us/graph/api/channel-post-messagereply?view=graph-rest-beta

Let’s go ahead and try this at Graph Explorer

https://graph.microsoft.com/beta/teams/{TeamID}/channels/{ChannelID}/messages/{the message id from previous post}/replies
Keeping the Request Body simple now:
{
"body": {
"contentType": "html",
"content": "Just saying a hello with a reply!"
}
}

..and it was a success! I got the response preview with new ID and a reference to the original message too.

Of course the real beef is this:

Interesting: What is also possible, is to get the id the the reply and reply to that message! These message’s don’t show up in the UI but they can be retrieved using Graph API.

Permissions

Graph Explorer runs on different permissions than real applications, which makes it easy to test various API calls with it. However, it won’t mean that everything can run under Azure application permissions. Messaging is one of those APIs that require (at least during writing this blog) delegated permissions. This is something that needs to be considered when creating applications that utilize messaging.

The possibilities are endless

The Graph API allows lots of possibilities for extended interactions in Teams. While our sample was using the Graph Explorer you can embed these Graph API calls into various pieces of software like

  • Flow
  • Bots
  • Applications
  • Powershell Scripts
  • Azure Functions
  • Azure Logic Apps
  • … and .. and … an….

These could be as simple as welcome messages (hey you have a new team – welcome and read through these basics from here first) or they could be information sent to the team from some external system (servers, xRM or order systems, warehouses, shops, surveillance systems). The benefit over the ”sending email to a channel”-feature is the atmentions and possibilities to reply – as well as smarter formatting of messages to let the information stand out.

There is also a possibility to list channel messages, so a bot or application could actually be reading messages and acting upon keywords / replying to messages.

One simple application could be connected to a team lifecycle: when team is closer to deadline the whole team could be notified about this.

Adding: User mention

If you want to mention a specific user in the post you need a mention section with user specified.

{
"id": 1,
"mentionText": "User name",
"mentioned": {
"application": null,
"device": null,
"conversation": null,
"user": {
"id": "User GUID",
"displayName": "User (display) Name",
"userIdentityType": "aadUser"
}
}
}

6 vastausta artikkeliin “Sending message with atmention onto a channel using GraphAPI .. and replying it

    1. This needs to be in the body:
      Channel name

      And this mentions-block needs of course the channel id
      ”mentions”: [
      {
      ”id”:0,
      ”mentionText”: ”Channel name”,
      ”mentioned”: {
      ”application”: null,
      ”device”: null,
      ”conversation”: null,
      ”conversation”: {
      ”id”: ”{!GeneralChannelID!}”,
      ”displayName”: ”Channel Name”,
      ”conversationIdentityType@odata.type”: ”#Microsoft.Teams.GraphSvc.conversationIdentityType”,
      ”conversationIdentityType”: ”channel”
      }
      }}
      }
      ]

      Tykkää

  1. Hi,
    I got posting messages to channels working, but I don’t see any commands to do 1:1 or group chats with Graph API. do you know if this is possible at all?

    thanks,
    Stefan

    Tykkää

  2. thanks, I feared so…also getting messages from private chats also is not possible? are these even saved in some kind of channel/teams context or is this user based? it seems there really is no way to export non-channel chat history via API 😦

    Tykkää

Vastaa

Täytä tietosi alle tai klikkaa kuvaketta kirjautuaksesi sisään:

WordPress.com-logo

Olet kommentoimassa WordPress.com -tilin nimissä. Log Out /  Muuta )

Google photo

Olet kommentoimassa Google -tilin nimissä. Log Out /  Muuta )

Twitter-kuva

Olet kommentoimassa Twitter -tilin nimissä. Log Out /  Muuta )

Facebook-kuva

Olet kommentoimassa Facebook -tilin nimissä. Log Out /  Muuta )

Muodostetaan yhteyttä palveluun %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.