Pantelisy
Pantelisy

Reputation: 93

Mention channel using ms graph api

I am working with ms graph and i need to integrate with ms teams. In one case i have to send a message to a channel which will contain a channel mention (@). Currently i have found how to tag a specific user using java but i cant figure out how to mention the whole channel. It is posible to do so in ms graph and if yes how?

To be more specific here is an example. Assume that i have a team named TestTeam and this team has a channel named testChannel. i need to send a message where will contain @testChannel and will send a notification to everyone in this channel.

Also in slack api i am able to do this action by using "<!channel>" in my message.

This is the code i am using in order to tag a user:

static void send_message(String text, String mentionName, String channelName, String teamName, String accessToken) {
  ensureGraphClient(accessToken);

  Team team = getTeam(accessToken, teamName);
  Channel channel = getChannel(accessToken, channelName, team);
  User user = getUser(accessToken, mentionName);
  if (userInChannel(user, team, channel)) {
     ChatMessage chatMessage = new ChatMessage();
     ItemBody body = new ItemBody();
     body.contentType = BodyType.HTML;
     body.content = String.format("%s.  <at id=\"1\">%s</at> ", text, mentionName);
     chatMessage.body = body;
     ChatMessageMention m = new ChatMessageMention();
     m.id = 1;
     IdentitySet st = new IdentitySet();
     Identity ide = new Identity();
     ide.id = user.id;
     ide.displayName = user.displayName;
     st.user = ide;
     m.mentioned = st;
     m.mentionText = mentionName;
     List<ChatMessageMention> cmmt = new LinkedList<>();
     cmmt.add(m);
     chatMessage.mentions = cmmt;
     graphClient.teams(team.id).channels(channel.id).messages()
           .buildRequest()
           .post(chatMessage);
  }

}

I also tried something similar in order to tag channels but it did not worked:

     body.contentType = BodyType.HTML;
     body.content = String.format("%s. <at id=\"1\">%s</at> ", text, "myChannel");
     chatMessage.body = body;
     ChatMessageMention m = new ChatMessageMention();
     m.id = 1;
     IdentitySet st = new IdentitySet();
     Identity ide = new Identity();
     ide.id = "19%xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%40thread.tacv2";
     ide.displayName = "myChannel";
     st.user = ide;
     m.mentioned = st;
     m.mentionText = "myChannel";
     List<ChatMessageMention> cmmt = new LinkedList<>();
     cmmt.add(m);
     chatMessage.mentions = cmmt;
     graphClient.teams(team.id).channels(channel.id).messages()
           .buildRequest()
           .post(chatMessage);

Upvotes: 1

Views: 2659

Answers (1)

Maytham Fahmi
Maytham Fahmi

Reputation: 33437

Introduction:

After playing around with it, here is the final result. And this is not final answer, but hope to lead for solution.

Lets start some introduction.

First lets define our post url:

POST https://graph.microsoft.com/beta/teams/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/channels/19:[email protected]/messages

Now lets mentioning a user in graph api:

{
    "body": {
        "contentType": "html",
        "content": "Hello World <at id=\"0\">Maytham Fahmi</at>"
    },
    "mentions": [
        {
            "id": 0, <- the index here should be the same as in the content message
            "mentionText": "Maytham Fahmi",
            "mentioned": {
                "user": {
                    "displayName": "Maytham Fahmi",
                    "id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" <- this should be user user object id
                }
            }
        }
    ]
}

This is tested and working via Graph Api.

Now I come to the interested part. Sending to channel with channel mentioning looks like this:

{
    "body": {
        "contentType": "html",
        "content": "Hello World <at id=\"0\">General</at>"
    },
    "mentions": [
        {
            "id": 0, <- the index here should be the same as in the content message
            "mentionText": "General",
            "mentioned":  {
                        "conversation": {
                            "id": "19:[email protected]",
                            "displayName": "General",
                            "[email protected]": "#Microsoft.Teams.GraphSvc.conversationIdentityType",
                            "conversationIdentityType": "channel"
                        }
        }
    ]
}

enter image description here

The results in teams, that is sent from channel mentioning enter image description here

This is also working from Graph api explorer.

The answer:

So as we can see for user mentioning we use User object under IdentitySet instance which works fine for user. I have testing it C# using Microsoft.Graph.

Here is comes the problem Conversation object does not exist under IdentitySet in C# using Microsoft.Graph. I am pretty sure this is the case for Java as well. If you have that object in Java then you should be able to make it. It is perhaps not in C#.

I have red different places, and sadly channel mentioning objects is not implemented in code yet, looks like a bug or a feature they will fix over time.

A work around solution is, you have graph api json payloads, you can make a Java client to post Jsons to the url with and your job is done.

Note: I have look at our solution, it does not support mentioning as well.

Inspiration links:

Note in case you need to send notification in general, you could use webhooks.

Upvotes: 3

Related Questions