Slack.pm 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package Cpanel::iContact::Provider::Slack;
  2. use strict;
  3. use warnings;
  4. use parent 'Cpanel::iContact::Provider';
  5. use Try::Tiny;
  6. use Cpanel::LoadModule ();
  7. =encoding utf-8
  8. =head1 NAME
  9. Cpanel::iContact::Provider::Slack - Backend for the Slack iContact module
  10. =head1 SYNOPSIS
  11. use Cpanel::iContact::Provider::Slack;
  12. my $notifier = Cpanel::iContact::Provider::Slack->new();
  13. $notifier->send();
  14. =head1 DESCRIPTION
  15. Provide backend accessor for the Slack iContact module.
  16. =cut
  17. =head2 send
  18. Sends off the notification over to your hipchat room/user
  19. =over 2
  20. =item Input
  21. =over 3
  22. None
  23. =back
  24. =item Output
  25. =over 3
  26. Truthy value on success, exception on failure.
  27. =back
  28. =back
  29. =cut
  30. sub send {
  31. my ($self) = @_;
  32. my $args_hr = $self->{'args'};
  33. my $contact_hr = $self->{'contact'};
  34. my @errs;
  35. Cpanel::LoadModule::load_perl_module("Cpanel::HTTP::Client");
  36. my $ua = Cpanel::HTTP::Client->new( 'default_headers' => { 'content-type' => 'application/json' } )->die_on_http_error();
  37. my $subject = $args_hr->{'subject'};
  38. my $message = ${ $args_hr->{'text_body'} };
  39. Cpanel::LoadModule::load_perl_module("Cpanel::AdminBin::Serializer");
  40. my $message_json = Cpanel::AdminBin::Serializer::Dump(
  41. {
  42. 'text' => $subject,
  43. 'attachments' => [ { "text" => $message } ],
  44. }
  45. );
  46. # Send it
  47. foreach my $destination ( @{ $args_hr->{'to'} } ) {
  48. try {
  49. my $res = $ua->request( 'POST', $destination, { 'content' => $message_json } );
  50. die( sprintf "Error %d: %s", $res->status(), $res->reason() ) if !$res->success();
  51. }
  52. catch {
  53. Cpanel::LoadModule::load_perl_module("Cpanel::Exception");
  54. push(
  55. @errs,
  56. Cpanel::Exception::create(
  57. 'ConnectionFailed',
  58. 'The system failed to send the message to “[_1]” due to an error: [_2]',
  59. [ $destination, $_ ]
  60. )
  61. );
  62. };
  63. }
  64. if (@errs) {
  65. # Module should already be loaded above
  66. die Cpanel::Exception::create( 'Collection', [ exceptions => \@errs ] );
  67. }
  68. return 1;
  69. }
  70. 1;