Telegram.pm 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. # Telegram max message length is 4096 chars.
  37. # As such , truncate at 4092, add ellipsis (3 chars).
  38. # Why not 4093? I want to avoid fencepost errors.
  39. # Also, mojibake worries... oof
  40. require Encode;
  41. my $subject = Encode::decode_utf8( $args_hr->{'subject'}, $Encode::FB_QUIET );
  42. my $body = Encode::decode_utf8( ${$args_hr->{'text_body'}}, $Encode::FB_QUIET );
  43. my $message = substr( "$subject\n$body", 0, 4092 );
  44. $message .= '...' if length $message == 4092;
  45. # Disgusting, but whatever. We are about to have some fun here boyos
  46. # First, gotta load our libs
  47. # Second, the mojo that comes with cP is titanic.
  48. # Mojo you install from cpan won't work with cP binaries
  49. # Disaster all around.
  50. # Get around it by forcing the module into LWP mode, lol
  51. push @INC, '/usr/local/share/perl5';
  52. require WWW::Telegram::BotAPI;
  53. my $api = WWW::Telegram::BotAPI->new(
  54. token => $self->{'contact'}{'TELEGRAMBOTTOKEN'},
  55. force_lwp => 1,
  56. );
  57. # Test the auth. Will die if it fails.
  58. $api->getMe();
  59. # Send it
  60. foreach my $destination ( @{ $args_hr->{'to'} } ) {
  61. try {
  62. $api->sendMessage({
  63. 'chat_id' => $destination,
  64. 'text' => $message,
  65. });
  66. }
  67. catch {
  68. require Cpanel::Exception;
  69. push(
  70. @errs,
  71. Cpanel::Exception::create(
  72. 'ConnectionFailed',
  73. 'The system failed to send the message to “[_1]” due to an error: [_2]',
  74. [ $destination, $_ ]
  75. )
  76. );
  77. };
  78. }
  79. if (@errs) {
  80. # Module should already be loaded above
  81. die Cpanel::Exception::create( 'Collection', [ exceptions => \@errs ] );
  82. }
  83. return 1;
  84. }
  85. 1;