Slack.pm 2.1 KB

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