0
votes

I am working on sending the message to 1:1 chat or to group chat in Microsoft Teams via Microsoft Graph API using C# code but unfortunately couldn't find the C# code/classes for the same.

Actually I am able to send the message to the team's channel using the below code successfully.

GraphServiceClient graphClient = new GraphServiceClient( authProvider );

var chatMessage = new ChatMessage
{
    Body = new ItemBody
    {
        Content = "Hello world"
    }
};

await graphClient.Teams["{id}"].Channels["{id}"].Messages
    .Request()
    .AddAsync(chatMessage);

The reference for the above code is from the below link:
https://docs.microsoft.com/en-us/graph/api/chat-post-messages?view=graph-rest-beta&tabs=csharp

Can anybody please suggest me the C# code/classes to send the message to 1:1 chat or to group chat in Microsoft Teams via Microsoft Graph API?

Please help.

Thanks in advance!

1
1:1 chat is supported. The code in your description is to send messages in Channel. The document reference link that you shared is for channel messages. - Abhijit
@Abhijit-MSFT Yes you are right, But I need C# code to send messages to a person in Teams. I couldn't find it in the Microsoft Graph documentation. - Pravin Durgam
Do you mean Channel? if Channel then you are using the correct code that you referred in your questions. Please explain me your scenario in case you are looking for something else. - Abhijit
No. I didn't mean channel. So basically my scenario is to post a direct message to 1:1 chat (directly to person) using Graph API in C# :) – - Pravin Durgam
Thanks you so much @Marc LaFleur as I found below of your answer very useful. This has saved my lot of time. stackoverflow.com/questions/58913955/… - Pravin Durgam

1 Answers

1
votes

Finally, I found the answer.

The Microsoft Graph API to send a message to 1:1 chat or to group chat is still in beta mode. And in order to call any beta version Microsoft Graph API's we need to include Microsoft.Graph.Beta NuGet package, not the Microsoft.Graph package. The later only contains APIs that have been released (i.e. /v1.0/...).

The working code to send the message to 1:1 chat or to group chat is below:

await graphClient.Users["{id}"]
                 .Chats["{id}"]
                 .Messages
                 .Request()
                 .AddAsync(chatMessage);

Actually, the code is not updated in the below documentation of Graph API for Sending the message to 1:1 chat under C# code snippet example section.

https://docs.microsoft.com/en-us/graph/api/chat-post-messages?view=graph-rest-beta&tabs=csharp#tabgroup_CeZOj-G++Q