Open MrPeanut12 opened 2 years ago
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 SendMail
method (to use Slack instead of email), (b) the logic that deals with mail properties (SMTP server, credentials, etc), (c) possibly the FormatEmail
method (I suspect there is a way to pass HTML message to Slack, but I do not know). You will also need a couple of new properties for Slack settings and to indicate which notification provider to use. And the logic that validates all inputs before executing the main operation. Off the top of my head, I'd say it may be nice to have but given the amount of changes, that need to be implemented, I'd skip it.
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 FormatEmail
method.
#--------------------------------------------------------------------------
# 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 SendMail
right now to send the notification.
$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.
Result so far:
Sweet. Good luck with it. Btw, is there any security around who can post to your Slack URL?
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: