SOLVED

Post messages with @mentions in teams channels using Graph API

Occasional Contributor

I think it's recommended that admins use Microsoft Graph for administrative functions that have to do with channels, tabs, bots and apps. I've been having trouble with posting posting messages, especially with @mentions to Teams channels using Powershell. Would it be possible to help me with finding clear documentation on using the Graph API to send messages in channels using PowerShell.

5 Replies

Hi there @pammnd - Here's a document which covers how you'd post to a channel via Teams Graph API: https://docs.microsoft.com/en-us/graph/api/channel-post-messages?view=graph-rest-1.0&tabs=http. Under the "mentions" option, you can select someone here to @ mention which should cover what you need here, example from the following response: https://docs.microsoft.com/en-us/graph/api/channel-post-messages?view=graph-rest-1.0&tabs=http

I'd recommend trying out the Graph Explorer module, which you can then further use to run queries through other areas like PS, third party tools like PostMan, etc. Hope this helps!

Sam, Teams Engineering

Thanks, @Sam Cosby ! It works fine when I test in Graph Explorer but not in Powershell...

 

Here's the error message I get:

Invoke-RestMethod : {
"error": {
"code": "UnknownError",
"message": "",
"innerError": {
"date": "2020-06-14T10:48:15",
"request-id": "b78a3535-34ef-48f0-bcdf-d102c5a49a3b"
}
}
}
At C:\Chatting to Webex Bot Testing\5.2 - Test Chatting in Channel.ps1:11 char:1
+ Invoke-RestMethod -Method POST 'https://graph.microsoft.com/v1.0/team ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

 

 

For reference, this is the basic script I'm using to send a test message that receives the unknownerror... $test = [ordered]@{ body = { content = "test message" } } | ConvertTo-Json Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/teams/team_id/channels/channel_id/messages" -Method Post -Headers $token -Body $test -ContentType application/json
best response confirmed by pammnd (Occasional Contributor)
Solution

Got it, good to know that's at least there for ya :) @pammnd. Do you know if other commands fail in this method? It could be possibly how Graph API is being leveraged when you invoke the rest method... As an example, if you use Fiddler to capture your error, you may see hitting a 401/403 vs. a 500 error to validate what might be failing for you.

Best,

Sam, Teams Engineering

Thank you! I'll check Fiddler