diff --git a/config.pl-dist b/config.pl-dist index 52ed1b8..1a546dd 100644 --- a/config.pl-dist +++ b/config.pl-dist @@ -268,6 +268,19 @@ $notify = { }, + #slack incoming web hook integration. + 'slack' => { + 'enabled' => 0, + 'push_recentlyadded' => 0, + 'push_watched' => 0, + 'push_watching' => 0, + 'push_paused' => 0, + 'push_resumed' => 0, + 'webhook_url' => 'https://hooks.slack.com/services/XXXXXXXXXXX/XXXXXXXXXX/XXXXXXXXXXXXXXXXX', #webhook url + 'channel' => '', # '@myuser', '#notifications', etc. '' uses default channel configured in the Slack webhook + 'message' => '{user}', + }, + }; $push_titles = { diff --git a/config.pl-dist-win32 b/config.pl-dist-win32 index e382d5e..a019f6d 100644 --- a/config.pl-dist-win32 +++ b/config.pl-dist-win32 @@ -232,6 +232,19 @@ $notify = { }, + #slack incoming web hook integration. + 'slack' => { + 'enabled' => 0, + 'push_recentlyadded' => 0, + 'push_watched' => 0, + 'push_watching' => 0, + 'push_paused' => 0, + 'push_resumed' => 0, + 'webhook_url' => 'https://hooks.slack.com/services/XXXXXXXXXXX/XXXXXXXXXX/XXXXXXXXXXXXXXXXX', #webhook url + 'channel' => '', # '@myuser', '#notifications', etc. '' uses default channel configured in the Slack webhook + 'message' => '{user}', + }, + }; diff --git a/plexWatch.pl b/plexWatch.pl index 8b2e9ee..dd5e349 100755 --- a/plexWatch.pl +++ b/plexWatch.pl @@ -169,6 +169,7 @@ 'backup', 'clean_extras', 'show_xml', + 'slack_channel:s', 'help|?' ) or pod2usage(2); pod2usage(-verbose => 2) if (exists($options{'help'})); @@ -1788,6 +1789,7 @@ () { 'name' => 'pushalot', 'definition' => 'INTEGER',}, { 'name' => 'boxcar', 'definition' => 'INTEGER',}, { 'name' => 'boxcar_v2', 'definition' => 'INTEGER',}, + { 'name' => 'slack', 'definition' => 'INTEGER',}, ); @@ -2326,6 +2328,71 @@ () return 0; # failed } +sub NotifySlack() { + my $provider = 'slack'; + + #my $alert = shift; + my $info = shift; + my ($alert) = &formatAlert($info,$provider); + + my $alert_options = shift; + + if ($provider_452->{$provider}) { + if ($options{'debug'}) { print uc($provider) . " 452: backing off\n"; } + return 0; + } + + my %sk = %{$notify->{slack}}; + my $ua = LWP::UserAgent->new( ssl_opts => { + verify_hostname => 0, + SSL_verify_mode => "SSL_VERIFY_NONE", + }); + $ua->timeout(20); + + ## allow formatting of appname + $sk{'message'} = '{user}' if $sk{'message'} eq $appname; ## force {user} if people still use $appname in config -- forcing update with the need to modify config. + my $format = $sk{'message'}; + + + if ($format =~ /\{.*\}/) { + my $regex = join "|", keys %{$alert_options}; + $regex = qr/$regex/; + $sk{'message'} =~ s/{($regex)}/$alert_options->{$1}/g; + $sk{'message'} =~ s/{\w+}//g; ## remove any {word} - templates that failed + $sk{'message'} = $appname if !$sk{'message'}; ## replace appname if empty + } + + + $sk{'message'} .= ': ' . $push_type_titles->{$alert_options->{'push_type'}} if $alert_options->{'push_type'}; + $sk{'message'} .= ' ' . ucfirst($alert_options->{'item_type'}) if $alert_options->{'item_type'}; + $sk{'message'} .= ' ' . $alert; + + my $channel = $sk{'channel'}; + if ($options{'slack_channel'}) { $channel = $options{'slack_channel'}; } + my %post = ('text' => $sk{'message'}, 'channel' => $channel); + my $json = encode_json \%post; + my $url = $sk{'webhook_url'}; + + my $response = $ua->post( $url, [ + "payload" => $json, + ]); + my $content = $response->decoded_content(); + + + if ($content !~ /ok/) { + print STDERR "Failed to post Slack notification -- $sk{'message'} result:$content\n"; + $provider_452->{$provider} = 1; + my $msg452 = uc($provider) . " failed: $alert - setting $provider to back off additional notifications\n"; + &ConsoleLog($msg452,,1); + + return 0; + } + + my $dmsg = uc($provider) . " Notification successfully posted.\n" if $debug; + &DebugLog($dmsg) if $dmsg && $debug; + return 1; ## success +} + sub NotifyPushOver() { my $provider = 'pushover'; @@ -3603,6 +3670,7 @@ () GNTP => \&NotifyGNTP, EMAIL => \&NotifyEMAIL, external => \&NotifyExternal, + slack => \&NotifySlack, ); my $error; ## this SHOULD never happen if the code is released -- this is just a reminder for whomever is adding a new provider in config.pl @@ -4324,6 +4392,8 @@ =head1 SYNOPSIS --exclude_library_id=... Full exclusion for a library section id. It will not log or notify. + --slack_channel Slack channel or user override to notify. '--slack_channel=#notifications', '--slack_channel=@myuser' + ############################################################################################# --format_options : list all available formats for notifications and cli output