How to post message in MS Teams using Graph API

16,675

Solution 1

Update as of August 20201

The Teams API allows creating new chats, as well as sending messages to existing ones.

Create a new chat object

Send messages to existing chats

List chats to retrieve the ID of an existing chat

Other options

If you're agnostic to how you send messages to users, you may be interested in sending proactive messages and proactive messaging for bots.

Solution 2

Today the APIs for Microsoft Teams in the Graph only support sending messages to channels, not to users (private chat). As you can see here, only channels operations are listed out.

Share:
16,675
Prakash
Author by

Prakash

Updated on August 07, 2022

Comments

  • Prakash
    Prakash over 1 year

    I am trying to send messages to MS Teams using Graph API using the following code.

     List<Recipient> recipients = new List<Recipient>();
            recipients.Add(new Recipient
            {
                EmailAddress = new EmailAddress
                {
                    Address = "[email protected]"
                }
            });
    
            // Create the message.
            Message msg = new Message
            {
                Body = new ItemBody
                {
                    Content = "Test message3",
                    ContentType = BodyType.Text,
                },
                ToRecipients = recipients
            };
    
            _graphServiceClient.Users["fe0bb333-3334c49-a3eb-25af61fed1db"].SendMail(msg, true).Request().PostAsync().Wait();
    

    This code does not send message in MS Team but instead send that message in email.

    I am following the documentation https://docs.microsoft.com/en-us/graph/api/message-send?view=graph-rest-1.0 and was tyring Graph Explorer to send message but not working.

    Post: https://graph.microsoft.com/v1.0/users/fe0bb333-3335-4c49-a3eb-25af61fed1db/messages/22229b36-a7cb-4a33-a9f9-dd75742bf067/send

    Request Body

    { "Body": "Hello World" }

    But, I get following error from Graph Explorer:

    {
        "error": {
            "code": "ErrorInvalidIdMalformed",
            "message": "Id is malformed.",
            "innerError": {
                "request-id": "9cddabed-f886-4c89-be8b-7b5735ad957f",
                "date": "2019-04-21T05:37:11"
            }
        }
    }
    
  • Prakash
    Prakash about 5 years
    thanks @baywet for the update. Yeah. I also tested in Graph Explorer tool, it can send messages in channel. Any workaround or tweak or any other API like EWS API would be helpful for sending message in private message (user to user)?
  • baywet
    baywet about 5 years
    There's no other public API for Microsoft teams. I know teams relies on the Skype for business infrastructure for a few things, not sure if private messages is one of them. But you could look at the ucwa see if that surfaces back in teams at some point.
  • baywet
    baywet about 5 years
    Another approach could be to leverage a bot to handle user communications instead, not sure it fits your scénario though
  • Luv2Learn
    Luv2Learn over 2 years
    According to documentation, users can now create chats
  • Raza
    Raza over 2 years
    Thank you @Luv2Learn, I've updated the answer to reflect the new method.