소스 검색

Add Slack Provider

Andy Baugh 8 년 전
부모
커밋
cec78a8e48
4개의 변경된 파일232개의 추가작업 그리고 2개의 파일을 삭제
  1. 3 1
      README.md
  2. 4 1
      TODO
  3. 118 0
      lib/Cpanel/iContact/Provider/Schema/Slack.pm
  4. 107 0
      lib/Cpanel/iContact/Provider/Slack.pm

+ 3 - 1
README.md

@@ -1,7 +1,9 @@
 # iContact-cPanel-Plugins
 # iContact-cPanel-Plugins
 Extra "Contact Manager" Providers for cPanel and WHM
 Extra "Contact Manager" Providers for cPanel and WHM
 
 
-Still somewhat WIP, for now only has 'alpha' quality XMPP provider
+Current plugins:
+* XMPP  -- Still somewhat WIP, for now only has 'alpha' quality XMPP provider (as I've received no external bug reports)
+* Slack -- Works presuming you have an incoming WebHook URL, much like CpanelRicky's MatterMost plugin. Also alpha quality
 
 
 Plans for the future can be read in TODO
 Plans for the future can be read in TODO
 
 

+ 4 - 1
TODO

@@ -4,8 +4,11 @@ XMPP provider:
 * Send to rooms? I haven't tried that
 * Send to rooms? I haven't tried that
 * Register the user? Haven't tried that.
 * Register the user? Haven't tried that.
 
 
+Slack Provider:
+* Add tests, remove Cpanel namespace deps from driver lib
+* Slack external auth module integration?
+
 Other Providers:
 Other Providers:
 * IRC
 * IRC
 * Discord
 * Discord
-* Slack (if I can't figure out how to get that mainlined with a good ext auth integration... their oauth is a pain)
 * ... Whatever else is hanging out on the feature site that sounds interesting
 * ... Whatever else is hanging out on the feature site that sounds interesting

파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 118 - 0
lib/Cpanel/iContact/Provider/Schema/Slack.pm


+ 107 - 0
lib/Cpanel/iContact/Provider/Slack.pm

@@ -0,0 +1,107 @@
+package Cpanel::iContact::Provider::Slack;
+
+use strict;
+use warnings;
+
+use parent 'Cpanel::iContact::Provider';
+
+use Try::Tiny;
+use Cpanel::LoadModule ();
+
+=encoding utf-8
+
+=head1 NAME
+
+Cpanel::iContact::Provider::Slack - Backend for the Slack iContact module
+
+=head1 SYNOPSIS
+
+    use Cpanel::iContact::Provider::Slack;
+
+    my $notifier = Cpanel::iContact::Provider::Slack->new();
+    $notifier->send();
+
+
+=head1 DESCRIPTION
+
+Provide backend accessor for the Slack iContact module.
+
+=cut
+
+=head2 send
+
+Sends off the notification over to your hipchat room/user
+
+=over 2
+
+=item Input
+
+=over 3
+
+None
+
+=back
+
+=item Output
+
+=over 3
+
+Truthy value on success, exception on failure.
+
+=back
+
+=back
+
+=cut
+
+sub send {
+    my ($self) = @_;
+
+    my $args_hr    = $self->{'args'};
+    my $contact_hr = $self->{'contact'};
+
+    my @errs;
+
+    Cpanel::LoadModule::load_perl_module("Cpanel::HTTP::Client");
+    my $ua = Cpanel::HTTP::Client->new( 'default_headers' => { 'content-type' => 'application/json' } )->die_on_http_error();
+
+    my $subject = $args_hr->{'subject'};
+    my $message = ${ $args_hr->{'text_body'} };
+
+    Cpanel::LoadModule::load_perl_module("Cpanel::AdminBin::Serializer");
+    my $message_json = Cpanel::AdminBin::Serializer::Dump(
+        {
+            'text'        => $subject,
+            'attachments' => [ { "text" => $message } ],
+        }
+    );
+
+    # Send it
+    foreach my $destination ( @{ $args_hr->{'to'} } ) {
+        try {
+            my $res = $ua->request( 'POST', $destination, { 'content' => $message_json } );
+            die( sprintf "Error %d: %s", $res->status(), $res->reason() ) if !$res->success();
+        }
+        catch {
+            Cpanel::LoadModule::load_perl_module("Cpanel::Exception");
+            push(
+                @errs,
+                Cpanel::Exception::create(
+                    'ConnectionFailed',
+                    'The system failed to send the message to “[_1]” due to an error: [_2]',
+                    [ $destination, $_ ]
+                )
+            );
+        };
+    }
+
+    if (@errs) {
+
+        # Module should already be loaded above
+        die Cpanel::Exception::create( 'Collection', [ exceptions => \@errs ] );
+    }
+
+    return 1;
+}
+
+1;

이 변경점에서 너무 많은 파일들이 변경되어 몇몇 파일들은 표시되지 않았습니다.