From 5fa559ca91965efdb71768273a4b8e41d91be3b7 Mon Sep 17 00:00:00 2001 From: zzanger Date: Thu, 3 Sep 2015 21:44:28 -0600 Subject: [PATCH 01/10] added functionality for slack --- config.pl-dist | 17 +++++++++++++++++ config.pl-dist-win32 | 17 +++++++++++++++++ plexWatch.pl | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) diff --git a/config.pl-dist b/config.pl-dist index 52ed1b8..ff2bd77 100644 --- a/config.pl-dist +++ b/config.pl-dist @@ -268,6 +268,23 @@ $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', #your webhook url + 'alert_format' => { + 'start' => '{title} [{streamtype}] [{year}] [{rating}] on {platform} [{progress} in]', + 'paused' => '{title} [{streamtype}] [{year}] [{rating}] on {platform} [{progress} in] [{percent_complete}%]', + 'resumed' => '{title} [{streamtype}] [{year}] [{rating}] on {platform} [{progress} in] [{percent_complete}%]', + 'stop' => '{title} [{streamtype}] [{year}] [{rating}] on {platform} for {duration} [{percent_complete}%]', + }, + }, + }; $push_titles = { diff --git a/config.pl-dist-win32 b/config.pl-dist-win32 index e382d5e..b507a6d 100644 --- a/config.pl-dist-win32 +++ b/config.pl-dist-win32 @@ -232,6 +232,23 @@ $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', #your webhook url + 'alert_format' => { + 'start' => '{title} [{streamtype}] [{year}] [{rating}] on {platform} [{progress} in]', + 'paused' => '{title} [{streamtype}] [{year}] [{rating}] on {platform} [{progress} in] [{percent_complete}%]', + 'resumed' => '{title} [{streamtype}] [{year}] [{rating}] on {platform} [{progress} in] [{percent_complete}%]', + 'stop' => '{title} [{streamtype}] [{year}] [{rating}] on {platform} for {duration} [{percent_complete}%]', + }, + }, + }; diff --git a/plexWatch.pl b/plexWatch.pl index 8b2e9ee..68a7947 100755 --- a/plexWatch.pl +++ b/plexWatch.pl @@ -2326,6 +2326,48 @@ () 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); + $sk{'message'} = $alert; + + my $response = $ua->post( sk{'webhook_url'}, [ + "payload" => '{"text": "' . $po{'message'} . '"}', + ]); + my $content = $response->decoded_content(); + + + if ($content !~ /\"status\":1/) { + 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 +3645,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 From c59ed1ecd125d29a56bc61056add0c7a7e83493f Mon Sep 17 00:00:00 2001 From: zzanger Date: Thu, 3 Sep 2015 21:45:51 -0600 Subject: [PATCH 02/10] adding database entry for slack --- plexWatch.pl | 1 + 1 file changed, 1 insertion(+) diff --git a/plexWatch.pl b/plexWatch.pl index 68a7947..32d81ce 100755 --- a/plexWatch.pl +++ b/plexWatch.pl @@ -1788,6 +1788,7 @@ () { 'name' => 'pushalot', 'definition' => 'INTEGER',}, { 'name' => 'boxcar', 'definition' => 'INTEGER',}, { 'name' => 'boxcar_v2', 'definition' => 'INTEGER',}, + { 'name' => 'slack', 'definition' => 'INTEGER',}, ); From b024b6ea822a7a06d273f37d8f4f551e3ecd2fad Mon Sep 17 00:00:00 2001 From: zzanger Date: Thu, 3 Sep 2015 21:55:20 -0600 Subject: [PATCH 03/10] use JSON library --- plexWatch.pl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/plexWatch.pl b/plexWatch.pl index 32d81ce..6d44c9c 100755 --- a/plexWatch.pl +++ b/plexWatch.pl @@ -2349,8 +2349,11 @@ () $ua->timeout(20); $sk{'message'} = $alert; + my %post = ('text' => $sk{'message'}); + my $json = encode_json \%post; + my $response = $ua->post( sk{'webhook_url'}, [ - "payload" => '{"text": "' . $po{'message'} . '"}', + "payload" => $json, ]); my $content = $response->decoded_content(); From f25c2f3cb47cf35a7c982e8e1489b17fc08bf75f Mon Sep 17 00:00:00 2001 From: zzanger Date: Thu, 3 Sep 2015 22:44:53 -0600 Subject: [PATCH 04/10] removed unused config and fixed missing $ --- config.pl-dist | 8 +------- plexWatch.pl | 5 +++-- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/config.pl-dist b/config.pl-dist index ff2bd77..c0e4ea4 100644 --- a/config.pl-dist +++ b/config.pl-dist @@ -276,13 +276,7 @@ $notify = { 'push_watching' => 0, 'push_paused' => 0, 'push_resumed' => 0, - 'webhook_url' => 'https://hooks.slack.com/services/XXXXXXXXXXX/XXXXXXXXXX/XXXXXXXXXXXXXXXXX', #your webhook url - 'alert_format' => { - 'start' => '{title} [{streamtype}] [{year}] [{rating}] on {platform} [{progress} in]', - 'paused' => '{title} [{streamtype}] [{year}] [{rating}] on {platform} [{progress} in] [{percent_complete}%]', - 'resumed' => '{title} [{streamtype}] [{year}] [{rating}] on {platform} [{progress} in] [{percent_complete}%]', - 'stop' => '{title} [{streamtype}] [{year}] [{rating}] on {platform} for {duration} [{percent_complete}%]', - }, + 'webhook_url' => 'https://hooks.slack.com/services/XXXXXXXXXXX/XXXXXXXXXX/XXXXXXXXXXXXXXXXX', #webhook url }, }; diff --git a/plexWatch.pl b/plexWatch.pl index 6d44c9c..00e0b22 100755 --- a/plexWatch.pl +++ b/plexWatch.pl @@ -2351,8 +2351,9 @@ () my %post = ('text' => $sk{'message'}); my $json = encode_json \%post; - - my $response = $ua->post( sk{'webhook_url'}, [ + my $url = $sk{'webhook_url'}; + + my $response = $ua->post( $url, [ "payload" => $json, ]); my $content = $response->decoded_content(); From 480ba333d1f0e7a668aa0d36d513775b7d1773dd Mon Sep 17 00:00:00 2001 From: zzanger Date: Thu, 3 Sep 2015 23:04:48 -0600 Subject: [PATCH 05/10] checking response for ok --- plexWatch.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plexWatch.pl b/plexWatch.pl index 00e0b22..25ff60f 100755 --- a/plexWatch.pl +++ b/plexWatch.pl @@ -2359,7 +2359,7 @@ () my $content = $response->decoded_content(); - if ($content !~ /\"status\":1/) { + 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"; From f96967e10fcca3c958446848c55340d79343a9a9 Mon Sep 17 00:00:00 2001 From: zzanger Date: Thu, 3 Sep 2015 23:16:33 -0600 Subject: [PATCH 06/10] formatted message to include action and item --- plexWatch.pl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/plexWatch.pl b/plexWatch.pl index 25ff60f..5332add 100755 --- a/plexWatch.pl +++ b/plexWatch.pl @@ -2347,7 +2347,11 @@ () SSL_verify_mode => "SSL_VERIFY_NONE", }); $ua->timeout(20); - $sk{'message'} = $alert; + $sk{'message'} = '*{user}'; + $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'} .= '*\n'; + $sk{'message'} .= $alert; my %post = ('text' => $sk{'message'}); my $json = encode_json \%post; From eac1d55bc8d80b31a0f88d00d3d98b4c7644e5a3 Mon Sep 17 00:00:00 2001 From: zzanger Date: Thu, 3 Sep 2015 23:37:13 -0600 Subject: [PATCH 07/10] added user and action formatted correclty now --- config.pl-dist | 1 + plexWatch.pl | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/config.pl-dist b/config.pl-dist index c0e4ea4..03b0657 100644 --- a/config.pl-dist +++ b/config.pl-dist @@ -277,6 +277,7 @@ $notify = { 'push_paused' => 0, 'push_resumed' => 0, 'webhook_url' => 'https://hooks.slack.com/services/XXXXXXXXXXX/XXXXXXXXXX/XXXXXXXXXXXXXXXXX', #webhook url + 'message' => '{user}', }, }; diff --git a/plexWatch.pl b/plexWatch.pl index 5332add..e820142 100755 --- a/plexWatch.pl +++ b/plexWatch.pl @@ -2347,11 +2347,24 @@ () SSL_verify_mode => "SSL_VERIFY_NONE", }); $ua->timeout(20); - $sk{'message'} = '*{user}'; + + ## 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'} .= '*\n'; - $sk{'message'} .= $alert; + $sk{'message'} .= ' ' . $alert; my %post = ('text' => $sk{'message'}); my $json = encode_json \%post; From ee587d7e2dca7373787e360d1f7b411c08251bc4 Mon Sep 17 00:00:00 2001 From: zzanger Date: Thu, 3 Sep 2015 23:39:30 -0600 Subject: [PATCH 08/10] oops forgot about the windows config --- config.pl-dist-win32 | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/config.pl-dist-win32 b/config.pl-dist-win32 index b507a6d..10e96aa 100644 --- a/config.pl-dist-win32 +++ b/config.pl-dist-win32 @@ -240,13 +240,8 @@ $notify = { 'push_watching' => 0, 'push_paused' => 0, 'push_resumed' => 0, - 'webhook_url' => 'https://hooks.slack.com/services/XXXXXXXXXXX/XXXXXXXXXX/XXXXXXXXXXXXXXXXX', #your webhook url - 'alert_format' => { - 'start' => '{title} [{streamtype}] [{year}] [{rating}] on {platform} [{progress} in]', - 'paused' => '{title} [{streamtype}] [{year}] [{rating}] on {platform} [{progress} in] [{percent_complete}%]', - 'resumed' => '{title} [{streamtype}] [{year}] [{rating}] on {platform} [{progress} in] [{percent_complete}%]', - 'stop' => '{title} [{streamtype}] [{year}] [{rating}] on {platform} for {duration} [{percent_complete}%]', - }, + 'webhook_url' => 'https://hooks.slack.com/services/XXXXXXXXXXX/XXXXXXXXXX/XXXXXXXXXXXXXXXXX', #webhook url + 'message' => '{user}', }, From 9e2b2ac0383e1d8269060543aa055c1c7fe60c9d Mon Sep 17 00:00:00 2001 From: Ben Loveridge Date: Wed, 20 Jul 2016 23:41:11 -0600 Subject: [PATCH 09/10] Support 'channel' in the slack configuration --- config.pl-dist | 1 + config.pl-dist-win32 | 1 + plexWatch.pl | 3 ++- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/config.pl-dist b/config.pl-dist index 03b0657..1a546dd 100644 --- a/config.pl-dist +++ b/config.pl-dist @@ -277,6 +277,7 @@ $notify = { '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/config.pl-dist-win32 b/config.pl-dist-win32 index 10e96aa..a019f6d 100644 --- a/config.pl-dist-win32 +++ b/config.pl-dist-win32 @@ -241,6 +241,7 @@ $notify = { '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 e820142..a64179b 100755 --- a/plexWatch.pl +++ b/plexWatch.pl @@ -2366,7 +2366,8 @@ () $sk{'message'} .= ' ' . ucfirst($alert_options->{'item_type'}) if $alert_options->{'item_type'}; $sk{'message'} .= ' ' . $alert; - my %post = ('text' => $sk{'message'}); + my $channel = $sk{'channel'}; + my %post = ('text' => $sk{'message'}, 'channel' => $channel); my $json = encode_json \%post; my $url = $sk{'webhook_url'}; From f3fc93694787727a1d51a8f219dd06973f047ae2 Mon Sep 17 00:00:00 2001 From: Ben Loveridge Date: Wed, 20 Jul 2016 23:41:51 -0600 Subject: [PATCH 10/10] Add `slack_channel` option for overriding the destination of slack messages --- plexWatch.pl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/plexWatch.pl b/plexWatch.pl index a64179b..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'})); @@ -2367,6 +2368,7 @@ () $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'}; @@ -4390,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