Share via

Issue sending message to an MS Teams channel via Graph API

Anonymous
2021-12-10T11:12:27.357+00:00

Hi, we're trying to send a message to an MS Teams channel or chat with that would contain a mention to a channel / user / tag.

We've experimented with sending messages as adaptive cards via a channel webhook but weren't able to include a working mention - it either didn't show up as a mention at all or didn't produce a notification.

Now we're giving a try to Graph API - it seems it does support mentions in chatMessage. We've followed the instructions provided in this Azure sample:

https://github.com/Azure-Samples/ms-identity-python-daemon/tree/master/1-Call-MsGraph-WithSecret

Specifically we've set up a client application via Azure AD, created a secret and added required permissions.

Sending a message to a chat seems to be unsupported for application scope, so we're now trying to send only to a channel.

Basically we're sending a POST request to the following endpoint:

https://graph.microsoft.com/v1.0/teams/<team_id>/channels/<channel_id>/messages

Specifying these headers:

'Content-type': 'application/json',
'Authorization': 'Bearer <token>'

And body:

{
  "body": {
    "content": "Hello World"
  }
}

We're getting the following response:

{
  "error": {
    "code": "Unauthorized",
    "message": "Unauthorized",
    "innerError": {
      "date": "2021-12-10T10:55:12",
      "request-id": "4c29336b-b4a4-4c93-896e-25b55bc4a95e",
      "client-request-id": "4c29336b-b4a4-4c93-896e-25b55bc4a95e"
    }
  }
}

We're pretty sure that the token we're using is correct - we're able to make GET requests with it.

Also we've checked the team and channel IDs via Teams UI, Graph Explorer and by making request to Graph API.

The permissions we currently have set on the application are the following: ChannelMessage.Read.All, ChannelMessage.Send, Group.Read.All, Sites.Manage.All, Teamwork.Migrate.All, User.Read, User.Read.All.

We would appreciate any suggestions on how to fix this / any other workarounds, thank you in advance.

Microsoft Security | Microsoft Graph

2 answers

Sort by: Most helpful
  1. Ramjot Singh 861 Reputation points Microsoft Employee
    2021-12-20T21:12:32.457+00:00

    Application permissions are ONLY supported when team is under migration mode as the documentation lists here https://learn.microsoft.com/en-us/graph/api/chatmessage-post?view=graph-rest-1.0&tabs=http

    Your request above failed because it using application-only permissions and not delegated permissions.

    You need to use delegated permissions.

    You can automate with delegated permissions as well. One such way is using Power Automate. Otherwise if you want to build an app yourself you need to use offline_access (https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-permissions-and-consent#offline_access) scope in addition to other required scopes. That way when the user sign ins and the token is acquired you will get additional refresh tokens which are long lived.

    For atmentioning users - https://learn.microsoft.com/en-us/graph/api/chatmessage-post?view=graph-rest-1.0&tabs=http#example-2--a-user-in-a-channel-message
    atmentioning channel - https://learn.microsoft.com/en-us/graph/api/chatmessage-post?view=graph-rest-1.0&tabs=http#example-7---a-channel-in-a-channel-message
    atmentioning tag - https://learn.microsoft.com/en-us/graph/api/chatmessage-post?view=graph-rest-beta&tabs=http#example-9---a-tag-in-a-channel-message (this is in beta only at the moment)

    @Siarhei Sintsou The same answer applies for your case as well as mentioned in the other question. You need a user to signin into your app and then make the calls on their behalf.

    0 comments No comments

  2. Siarhei Sintsou 1 Reputation point
    2021-12-20T10:17:24.64+00:00

    Facing the same issue. How we can send messages into MSTeams channels in an automatic way (for example implementing monitoring checks) with the possibility to mention people by name/channel tags?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.