1

I'm trying to send a message to MS Teams using Graph API. I'm passing access token (AAD token) with it but still, it's giving me below error. I have given all the required permissions in Azure API permissions. error:

{
  "error": {
    "code": "UnknownError",
    "message": "",
    "innerError": {
      "request-id": "53a5aaff-3d39-42ce-bdc6-74d02a756be2",
      "date": "2019-12-23T06:42:27"
    }
  }
}

API: https://graph.microsoft.com/beta/teams/{group-id-for-Teams}/channels/{channel-id}/messages/{message-id}/replies

Alok C
  • 2,787
  • 3
  • 25
  • 44
Mayuresh Jaiswal
  • 297
  • 3
  • 21
  • 2
    What is the request header and body for both getting access token and POST https://graph.microsoft.com/beta/teams/{group-id-for-teams}/channels/{channel-id}/messages/{message-id}/replies? And what permission did you add in Azure AD app. Please share more details. It is best to provide a screenshot of the permissions that have been assigned. You can firstly test this API in Microsoft Graph Explorer to determine if the problem lies in the access token. – Allen Wu Dec 23 '19 at 09:08
  • I see you added the `botframework` tag. Any reason you're using the Graph API instead of the bot? If you're trying to send a message as the bot, you'll still need the bot installed to the channel you want to send to. – mdrichardson Dec 23 '19 at 17:55
  • Bot will be installed to the channel where i'll be sending the message. I have used graph api as my solution from where i'm sending message is not in teams environment rather its a stand alone azure function. – Mayuresh Jaiswal Dec 24 '19 at 09:11

2 Answers2

1

Oh, if this is from a bot (not clear from the original question, but clarified in your later comment) then you don't need to use the Graph API at all - there's another way to send the message using the Bot Framework tools instead. You can do this either from within your bot, or from a different application altogether. I've got a few bots where the user schedules something, like when they want a message sent, where the bot saves it to a database and I have another application (mostly I use Azure Functions right now) to send the item on that schedule.

There are some important pieces of information you need to store though, which you can get any time the users sends your bot a message - it's the information you need to store so that you know how to connect directly to that user and that conversation. It's called Pro-active Messaging, and to see how to do this, see the answer I posted at Programmtically sending a message to a bot in Microsoft Teams

If you DON'T have any conversation history with the user ever (as in they have never spoken with your bot before, and you're trying to send the first message) then it gets more complicated... Let me know if that's the case though.

Hilton Giesenow
  • 9,809
  • 2
  • 10
  • 24
  • This approach is clear to me but in my case i have to first read the replies in the message and if there are no replies then only i have to send the replies (Message) to the existing message. so for reading the message replies i would need graph api or this can be done using bot framework? – Mayuresh Jaiswal Dec 26 '19 at 07:18
  • So: (1) do you have a bot registered into the conversation already (e.g. is the bot added to the channel) and (2) Must the bot reply directly to the message at the same time, or only later e.g. once per day)? – Hilton Giesenow Dec 26 '19 at 10:33
  • 1. yes, bot will be registered into the conversation already 2. Reply to message will be send once per day (at mid-night) – Mayuresh Jaiswal Dec 31 '19 at 06:56
  • In that case, check out the link I put into the main answer above - that should be what you need. Let me know if you come right (or just "mark as answer") – Hilton Giesenow Dec 31 '19 at 08:10
  • I think you have overlooked my 1st comment, i need to first read replies to a message and then send reply to only those messages where no body has replied. So how do i read replies for a message. – Mayuresh Jaiswal Jan 02 '20 at 05:55
  • oh yes, I did miss that message but just saw it now. I'm looking again at your original post and it actually does look correct - I've called that endpoint successfully before I think. One possible issue it could be is the format for the TeamId/ChannelId - they need to include the correct start and end like this: "19:___@thread.skype". If you want to see a working example, I've got a beta of a PowerShell module that calls the Graph, and it includes a function hitting this endpoint. Check out https://www.powershellgallery.com/packages/PowerGraph365/ and specifically Get-MessageReplies – Hilton Giesenow Jan 02 '20 at 17:53
0

Sending message to a channel using graph api is a protected api and it needs access permission from Microsoft. Access can be requested from Microsoft access reuqest form. Once access is given from Microsoft add graph api in api permissions of your web app, and bingo you can get the response.

Mayuresh Jaiswal
  • 297
  • 3
  • 21
  • I don't think it's the reason. See this document: https://learn.microsoft.com/en-us/graph/teams-protected-apis. It says Send message is not a protected API. Only the GET APIs are protected API. And I didn't complete the request form, I can still post messages using Microsoft Graph API: https://learn.microsoft.com/en-us/graph/api/channel-post-messagereply?view=graph-rest-beta&tabs=http. – Allen Wu Dec 26 '19 at 03:48
  • @AllenWu you are correct, but in my requirement i first need to read a message thread and if there is no reply then i have to send the message, and if you see the documentation api is same to read and write message just method is different. – Mayuresh Jaiswal Jan 27 '20 at 11:44