我正在处理一个与Slack集成的项目,当我试图将数据发送到API时遇到了一些问题,因为它希望为需要格式化的块提供Json对象。
使用Slack Block Builder,我知道它正在期待:
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "http://bar.com"
}
}
]
这就是我试图构造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
}
如有任何帮助或文件,我们将不胜感激。
您可能需要签出PSSlick-一个PS模块,用于与Slack API或PoshBot交互,如建议所示。在这里,您可以找到一篇很长的博客文章,其中给出了如何使用PSSlack的上下文。
如果你不想直接使用这些模块,你应该看看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' } }