I have a Slack slash command that I would like to implement, but I’m having difficulty responding correctly. No matter what I try, Slack is tacking the json and posting it as is, rather than interpreting its API is supposed to. Other methods of interacting with Slack work normally, so I think it’s something to do with how I’m using UD.
It works fine when I do an actual Invoke-RestMethod, but when I attempt to return json in the 200 response, which is what Slack wants you to do, it doesn’t work as I’m expecting it to.
Slack gives you 3 seconds to send a 200 response. After that you can do whatever, so you can just send a blank 200 and then do other stuff and respond later, but if you want to respond right away you can send JSON encoded stuff that follows what their APIs expect. This is what I’m struggling to work out.
I have confirmed this behaves the same on WIndows and Linux. Also, when I run Get-UDContentType, it returns application/x-www-form-urlencoded, even though I thought UD always sends application/json by default. When I explicitly set the contenttype to json, it does not change the behavior. Slack does both of these contentTypes, but in the past I’ve always used json. This is quite odd behavior.
Hmmm. You may be right. Slack can take both JSON and form data, but I reread some of their documentation (https://api.slack.com/interactivity/handling) and it may be that in the acknowledgement response it requires form data, not JSON.
In my previous attempts at a Slack/PowerShell app I never used the acknowledgement response to send anything other than the 200.
UD isn’t honoring the $Response.ContentType when you set it. What it is attempting to do is return the same content type as the request. If the request hasn’t sent a content-type, it will use “application/json; charset=utf8”. This is a valid content-type but slack is expecting exactly “application/json” without the charset. We can work around the UD issue by setting the request content type during the execution of the endpoint.