| 1 | package Plagger::Plugin::Notify::Slack; | ||||||||||
| 2 | 1 | ▶ | use 5.008001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use base qw( Plagger::Plugin ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Encode; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use JSON qw(encode_json); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | our $VERSION = "0.03"; | |||||||||
| 12 | |||||||||||
| 13 | 0 | ▶ | sub register { | ||||||||
|
Subroutine
register: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 14 | 0 | my ( $self, $context ) = @_; | |||||||||
| 15 | 0 | $context->register_hook( | |||||||||
| 16 | $self, | ||||||||||
| 17 | 'publish.entry' => $self->can('publish'), | ||||||||||
| 18 | 'plugin.init' => $self->can('initialize'), | ||||||||||
| 19 | ); | ||||||||||
| 20 | } | ||||||||||
| 21 | |||||||||||
| 22 | 0 | ▶ | sub initialize { | ||||||||
|
Subroutine
initialize: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 23 | 0 | my ( $self, $context, $args ) = @_; | |||||||||
| 24 | |||||||||||
| 25 | 0 | ▶ | $self->{remote} = $self->conf->{webhook_url} or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | 0 | ▶ | sub publish { | ||||||||
|
Subroutine
publish: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||
| 29 | 0 | my ( $self, $context, $args ) = @_; | |||||||||
| 30 | |||||||||||
| 31 | 0 | $context->log( info => "Notifying " . $args->{entry}->title . " to Slack" ); | |||||||||
| 32 | |||||||||||
| 33 | 0 | my $text = $self->templatize( 'notify.tt', $args ); | |||||||||
| 34 | 0 | ▶ | Encode::_utf8_off($text) if Encode::is_utf8($text); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | |||||||||||
| 36 | 0 | my $payload = +{ text => $text }; | |||||||||
| 37 | 0 | ▶ | $payload->{username} = $self->conf->{username} if exists $self->conf->{username}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 0 | ▶ | $payload->{icon_url} = $self->conf->{icon_url} if exists $self->conf->{icon_url}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 0 | ▶ | $payload->{icon_emoji} = $self->conf->{icon_emoji} if exists $self->conf->{icon_emoji}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 0 | ▶ | $payload->{channel} = $self->conf->{channel} if exists $self->conf->{channel}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | |||||||||||
| 42 | 0 | my $ua = LWP::UserAgent->new; | |||||||||
| 43 | 0 | my $res = $ua->post( $self->{remote}, [ payload => encode_json($payload) ] ); | |||||||||
| 44 | |||||||||||
| 45 | 0 | ▶ | unless ( $res->is_success ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 0 | $context->log( error => "Notiying to Slack failed: " . $res->status_line ); | |||||||||
| 47 | } | ||||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 1 | 1; | |||||||||
| 51 | __END__ | ||||||||||