Browse Source

Issue #21 basically fixed, need to make video and update readme now

Andy Baugh 5 years ago
parent
commit
79bc61a9e8
2 changed files with 16 additions and 14 deletions
  1. 9 6
      lib/Cpanel/iContact/Provider/Telegram.pm
  2. 7 8
      t/Cpanel-iContact-Provider-Telegram.t

+ 9 - 6
lib/Cpanel/iContact/Provider/Telegram.pm

@@ -72,16 +72,18 @@ sub send {
     # Test the auth. Will die if it fails.
     $api->getMe();
 
-    my $subject = $args_hr->{'subject'};
-    my $message = $args_hr->{'text_body'};
-    print $message;
+    # Telegram max message length is 4096 chars.
+    # As such , truncate at 4092, add ellipsis (3 chars).
+    # Why not 4093? I want to avoid fencepost errors.
+    my $message = substr( $args_hr->{'subject'} . "\n" . ${$args_hr->{'text_body'}}, 0, 4092 );
+    $message .= '...' if length $message == 4092;
 
     # Send it
     foreach my $destination ( @{ $args_hr->{'to'} } ) {
         try {
             $api->sendMessage({
-                'chat_id' => $destination,
-                'text'    => $$message,
+                'chat_id'    => $destination,
+                'text'       => $message,
             });
         }
         catch {
@@ -98,7 +100,8 @@ sub send {
     }
 
     if (@errs) {
-
+        use Data::Dumper;
+        print Dumper(\@errs);
         # Module should already be loaded above
         die Cpanel::Exception::create( 'Collection', [ exceptions => \@errs ] );
     }

+ 7 - 8
t/Cpanel-iContact-Provider-Telegram.t

@@ -18,19 +18,18 @@ plan tests => 2;
 
 # First, let's mock out the parent, and other stuff we wouldn't wanna do in a unit test
 subtest "Provider bits work as expected ('unit' test)" => sub {
-    pass("WIP");
-    return;
     my $text_scalar = 'lol, jk';
     my $send_args   = { 'subject' => "[test.host.tld] YOUR COMIC BOOKS ARE DYING!!!1", 'text_body' => \$text_scalar, 'to' => [ 'SalinasPunishmentRoom', '@cPSaurus' ] };
-    my $contact_cfg = {};
-    my $ua_mocker = Test::MockModule->new("Cpanel::HTTP::Client");
-    $ua_mocker->mock( 'request' => sub { return bless {}, "Cpanel::HTTP::Client::Response"; } );
-    my $resp_mocker = Test::MockModule->new("Cpanel::HTTP::Client::Response");
-    $resp_mocker->mock( 'success' => sub { return 1; }, 'status' => sub { return 200; }, 'reason' => sub { return 'OK'; } );
+    my $contact_cfg = { 'TELEGRAMBOTTOKEN' => '420SWAGYOLO69696969' };
+    my $ua_mocker = Test::MockModule->new("WWW::Telegram::BotAPI");
+
+    # Mock has to be used instead of redefine due to AUTOLOAD
+    $ua_mocker->mock( 'getMe' => sub {}, 'sendMessage' => sub {} );
 
     isa_ok( my $spammer = Cpanel::iContact::Provider::Telegram->new(), "Cpanel::iContact::Provider::Telegram" );
+    $spammer->{'contact'} = $contact_cfg;
     is( exception { $spammer->send() }, undef, "send doesn't throw on GreatSuccess" );
-    $resp_mocker->mock( 'success' => sub { return 0; }, 'status' => sub { return 500; }, 'reason' => sub { return 'ENOHUGS'; } );
+    $ua_mocker->mock( 'getMe' => sub { die "401 Unauthorized" } );
     isnt( exception { $spammer->send() }, undef, "send throws whenever anything goes wrong" );
 };