-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Post message with results to Slack #57
Comments
I do not have time to look into this now, so wouldn't promise anything, but if you are willing to make a change, you can obviously clone the code and do whatever you like, but this will involve a lot of changes: (a) the |
Thanks. I don't have much experience with PowerShell, but I'll see about using what already exists as a starting point. I have a good start by duplicating the #--------------------------------------------------------------------------
# SlackMessage
# Generates the body of the Slack message.
function SlackMessage {
[CmdletBinding()]
param (
)
WriteDebug "Entered SlackMessage."
[string]$backupMode = $null
[string]$dataDirPath = $null
if ($Script:Type) {
$backupMode = $Script:Mode + " -" + $Type
}
else {
$backupMode = $Script:Mode
}
if ($Script:Test) {
$backupMode += " (Test)"
}
$dataDirPath = $Script:BackupDir | ConvertTo-Json
$body = '*PlexBackup.ps1* completed the `' + $backupMode + '` operation on *PlexServer* (Plex version: ' + $Script:PlexVersion + '). Backup started at *' + $Script:StartTime + '* and ended at *' + $Script:EndTime + '* (total of *' + $Script:Duration + '*). Backup folder `' + $dataDirPath.Replace("`"","") + '` contains *' + $Script:ObjectCount + '* objects and *' + $Script:BackupSize + '* GB of data.'
WriteDebug "Exiting SlackMessage."
return $body
} I'm then hijacking $slackHeaderSuccess = "\u2705 Success"
$slackHeaderError = "\uD83D\uDC4E Failed"
if ($Script:ErrorResult) {
$slackHeader = $slackHeaderError
}
else {
$slackHeader = $slackHeaderSuccess
}
$messageSlack = SlackMessage
$bodySlack = @"
{
"text": "$slackHeader - $messageSlack",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "$slackHeader",
"emoji": true
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "$messageSlack"
}
}
]
}
"@
$uriSlack = "https://hooks.slack.com/services/<my-unique-url>"
Invoke-RestMethod -uri $uriSlack -Method Post -body $bodySlack -ContentType 'application/json' Obviously this needs a lot of work, but as an initial proof of concept, this appears to be a possibility. |
Sweet. Good luck with it. Btw, is there any security around who can post to your Slack URL? |
No, there is no security. You just have to know the unique URL. |
I am starting to move all of my notifications to Slack and was wondering if this script could post the results to Slack, similar to how it sends an email. Another example is how Tautulli sends notifications to Slack.
I have an Incoming Webhook set up (via Sending messages using Incoming Webhooks). I am able to post from PowerShell with something like the code below (modified from Sending messages to Slack using PowerShell). However, I'm not sure how to modify this script in order to send success/failure messages with the appropriate data.
Example PowerShell script:
Slack has a convenient Block Kit Builder to show some of the components available.
Example JSON to post to Slack:
The text was updated successfully, but these errors were encountered: