4

I read from here that getting a user's Teams chats that occurred outside the scope of a team or channel (1-to-1 chat conversation), you need to use this request

GET /users/id/messages

and Teams chat messages have "IM" as their subject.

My question now is whether it is possible to send a new message to this conversation via the Graph API and that the message will be displayed on the Teams application?

I tried to reply to this message via the graph API, but the reply message was sent to Outlook, not in the Microsoft Teams application.

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
Jeremy
  • 318
  • 4
  • 16
  • I do not have sufficiente reputation to comment on above answer, so here goes : If you are testing with the Graph Explorer, just remember to use POST: I was going crazy trying to send a message, just to discover I was always sending GET requests :-p – Philippe Morange Oct 22 '20 at 08:56

3 Answers3

6

It is now possible to send messages to personal chat(1:1) or to group chat in Microsoft Teams using Microsoft Graph API. And yes, the messages will be displayed in the Teams application using Microsoft Graph API.

Please refer the "1:1 and group chat messages" section from the below microsoft documentation link:

chatMessage resource type

Also, below is the graph API to send a message to any conversation you want using Post HTTP method :

https://graph.microsoft.com/beta/users/{user-id}/chats/{chat-id}/messages

To fetch {user-id} and {chat-id}, please follow the below steps using Get HTTP method:

  1. Fetch the user id of a logged-in user or user id of other user using below graph API's:

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

  2. Fetch the conversation/chat id of a user:

    https://graph.microsoft.com/beta/me/chats
    https://graph.microsoft.com/beta/users/{id}/chats

As of now, there is no graph API to reply to a personal chat but we can reply to any team's channel message using Microsoft Graph API.

Pravin Durgam
  • 127
  • 1
  • 13
  • You can reply to a 1:1 chat now. :) You just can't create new 1:1 threads yet. – ShaneOss Jun 10 '20 at 10:55
  • Could you please send the graph API or a link to reply to 1:1 chat? I need it. – Pravin Durgam Jun 11 '20 at 09:34
  • You already have the info above. :) You just need the thread id and then post to /me/chats/{thread id}/messages – ShaneOss Jun 12 '20 at 11:16
  • @PravinDurgam How do you figure out which chat is for a specific user? I'm able to find every 1:1 chat, but I don't know which chat is for a specific user. – Anthony Sep 11 '20 at 01:33
  • 2
    @Anthony, You have to first fetch user's id's via Microsoft Graph API's and then concatenate the user's ids to get the chat id like-> "19:" + User1.Id + "_" + User2.Id + "@unq.gbl.spaces" Please note that "19:" & "@unq.gbl.spaces" are the constants & if you construct the above string you will have chat id. Please try to do vice a versa of the user's Id's by keeping the constants same if you don't find the user's chat otherwise. This way you will be able to find which chat is for which specific user's. – Pravin Durgam Sep 11 '20 at 12:39
  • @PravinDurgam Thanks. That was a lot easier to understand than your answer for some reason. It worked. – Anthony Sep 12 '20 at 23:42
3

The answer, for now, is no. The docs have stated

"In both the v1 and beta endpoints, the response of GET /users/id/messages includes the user's Microsoft Teams chats that occurred outside the scope of a team or channel. These chat messages have "IM" as their subject."

You may submit/vote an feature request in the UserVoice or just wait the update from the Product Team.

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
Seiya Su
  • 1,836
  • 1
  • 7
  • 10
0

Here is a detailed explanation to answers that @parvin provided if you are trying to do this using Graph Explorer. There is one difference that in this method you don't need to have user-id anymore and chat-id would suffice. Simply Post HTTP method:

https://graph.microsoft.com/v1.0/chats/{chat-id}/messages

if you don't know how to get the chat-id, pleaser refer to answer provided by Parvin. Make sure your message is in the json format and you can put that in request body.

enter image description here

Then you can go to "Request headers" tab and add "Content-type" as Key and "application/json" as value.

enter image description here

Shahin Shirazi
  • 371
  • 3
  • 14
  • Hello, i am using this, but i notice that when i send the message i get no notification in teams that the message has arrived – rread Mar 18 '22 at 13:51
  • 1
    @rread: do you receive the message but not the notification or you dont event receive the message? If you dont receive the message, do you get any error? – Shahin Shirazi Mar 21 '22 at 13:45
  • I was able to figure it out, i did not get the alerts because i was sending the message with the same user as i had teams open. Once i started sending the message with another user it worked – rread Mar 23 '22 at 15:56