[英]JSON Object Formatting using Powershell for http request
I am working on a project integrating with Slack and I am having some trouble when I am trying to send data to the API as it is expecting a Json object for the blocks that it is needing to format.我正在处理一个与 Slack 集成的项目,当我尝试将数据发送到 API 时遇到了一些问题,因为它需要一个 Json 对象作为它需要格式化的块。
Using the Slack Block Builder I know it is expecting:使用 Slack Block Builder 我知道它期待:
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "http://bar.com"
}
}
]
And this is how I am trying to construct the object and the parameters for the chat.postMessage
method to send to the API这就是我尝试构造对象和参数的方式,以便将
chat.postMessage
方法发送到 API
$messageText = 'http://bar.com'
$blocksDetailObj = [PSCustomObject]@{'type' = '"mrkdwn"'
'text' = $messageText}
$blocksObject = [PSCustomObject]@{ 'type' = '"section"'
'text' = $blocksDetailObj }
$blocksArray = @()
$blocksArray += $blocksObject
$queryStringParameters = @{ 'token' = $botToken
'channel' = $channelID
'text' = 'fallback text'
'blocks' = $blocksArray
}
Any help or documentation would be greatly appreciated.任何帮助或文档将不胜感激。
You may want to checkout PSSlack - a PS module to interact with the Slack API, or PoshBot , as suggested.您可能需要检查PSSlack - 一个与 Slack API 或PoshBot交互的 PS 模块,如建议的那样。 Here you can find a long blog post that gives context how to PSSlack can be used.
在这里你可以找到一篇很长的博客文章,其中给出了如何使用 PSSlack 的上下文。
If you do not want to use those modules directly you should take a look at the Send-SlackMessage
function :如果您不想直接使用这些模块,您应该查看
Send-SlackMessage
函数:
$body = @{ } switch ($psboundparameters.keys) { 'channel' {$body.channel = $channel } 'text' {$body.text = $text} 'username' {$body.username = $username} 'asuser' {$body.as_user = $AsUser} 'iconurl' {$body.icon_url = $iconurl} 'iconemoji' {$body.icon_emoji = $iconemoji} 'linknames' {$body.link_names = 1} 'parse' {$body.parse = $Parse} 'UnfurlLinks' {$body.unfurl_links = $UnfurlLinks} 'UnfurlMedia' {$body.unfurl_media = $UnfurlMedia} 'attachments' {$body.attachments = $Attachments} } $Messages += $Body } else { foreach($Message in $SlackMessage) { $Messages += $SlackMessage } } foreach($Message in $Messages) { if($Token -or ($Script:PSSlack.Token -and -not $Uri)) { if($Message.attachments) { $Message.attachments = ConvertTo-Json -InputObject @$Message.attachments) -Depth 6 -Compress } Write-Verbose "Send-SlackApi -Body $($Message | Format-List | Out-String)" $response = Send-SlackApi @ProxyParam -Method chat.postMessage -Body $Message -Token $Token -ForceVerbose:$ForceVerbose if ($response.ok) { $link = "$($Script:PSSlack.ArchiveUri)/$($response.channel)/p$($response.ts -replace '\\.')" $response | Add-Member -MemberType NoteProperty -Name link -Value $link } $response } elseif($Uri -or $Script:PSSlack.Uri) { if(-not $ForceVerbose) { $ProxyParam.Add('Verbose', $False) } if($ForceVerbose) { $ProxyParam.Add('Verbose', $true) } $json = ConvertTo-Json -Depth 6 -Compress -InputObject $Message Invoke-RestMethod @ProxyParam -Method Post -Body $json -Uri $Uri } else { Throw 'No Uri or Token specified. Specify a Uri or Token in the parameters or via Set-PSSlackConfig' } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.