Telegram.pm 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package Cpanel::iContact::Provider::Telegram;
  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::Telegram - Backend for the Telegram iContact module
  9. =head1 SYNOPSIS
  10. use Cpanel::iContact::Provider::Telegram;
  11. my $notifier = Cpanel::iContact::Provider::Telegram->new();
  12. $notifier->send();
  13. =head1 DESCRIPTION
  14. Provide backend accessor for the Telegram iContact module.
  15. =cut
  16. =head2 send
  17. Sends off the notification over to your Telegram channel/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 @missing = grep { !defined $self->{'contact'}{$_} } qw{TELEGRAMBOTTOKEN};
  34. die "Kit not complete! Missing: " . join( ", ", @missing ) if scalar( @missing );
  35. my @errs;
  36. require WWW::Telegram::BotAPI;
  37. my $api = WWW::Telegram::BotAPI->new(
  38. token => $self->{'contact'}{'TELEGRAMBOTTOKEN'},
  39. );
  40. $api->getMe(); # Should explode if bogus?
  41. my $subject = $args_hr->{'subject'};
  42. my $message = $args_hr->{'text_body'};
  43. # Send it
  44. foreach my $destination ( @{ $args_hr->{'to'} } ) {
  45. try {
  46. $api->sendMessage(
  47. 'chat_id' => $destination,
  48. 'text' => $message,
  49. );
  50. }
  51. catch {
  52. require Cpanel::Exception;
  53. push(
  54. @errs,
  55. Cpanel::Exception::create(
  56. 'ConnectionFailed',
  57. 'The system failed to send the message to “[_1]” due to an error: [_2]',
  58. [ $destination, $_ ]
  59. )
  60. );
  61. };
  62. }
  63. if (@errs) {
  64. # Module should already be loaded above
  65. die Cpanel::Exception::create( 'Collection', [ exceptions => \@errs ] );
  66. }
  67. return 1;
  68. }
  69. 1;