Bot-ChatBots-Telegram

 view release on metacpan or  search on metacpan

lib/Bot/ChatBots/Telegram/LongPoll.pm  view on Meta::CPAN

package Bot::ChatBots::Telegram::LongPoll;
use strict;
use warnings;
{ our $VERSION = '0.014'; }

use Ouch;
use Try::Tiny;
use Log::Any qw< $log >;
use Mojo::IOLoop ();
use IO::Socket::SSL ();    # just to be sure to complain loudly in case
use List::Util qw< max >;
use Data::Dumper;

use Moo;
use namespace::clean;

with 'Bot::ChatBots::Telegram::Role::Source';    # normalize_record, token
with 'Bot::ChatBots::Role::Source';              # processor, typename

has connect_timeout => (
   is      => 'ro',
   default => sub { return 20 },
);

has interval => (
   is      => 'ro',
   default => sub { return 0.1 },
);

has max_redirects => (
   is => 'ro',
   default => sub { return 5 },
);

has _start => (
   is       => 'ro',
   default  => sub { return 1 },
   init_arg => 'start',
);

has update_timeout => (
   is      => 'ro',
   default => sub { return 300 },
);

sub BUILD {
   my $self = shift;
   $self->start if $self->_start;
}

sub class_custom_pairs {
   my $self = shift;
   return (token => $self->token);
}

sub parse_response {
   my ($self, $res, $threshold_id) = @_;
   my $data = $res->json // {};
   return grep { $_->{update_id} >= $threshold_id } @{$data->{result}//[]}
      if $data->{ok}; # boolean flag from Telegram API

   my $error = $data->{description} // 'unknown error';
   $log->error('getUpdates error: ' . $error);
   if ($log->is_trace) {
      local $Data::Dumper::Indent = 1;
      for ([json => $data], [res => $res]) {
         (my $d = Dumper $_->[1]) =~ s{\A.*?=}{$_->[0] =>}mxs;
         $log->trace($d);
      }
   }



( run in 0.445 second using v1.01-cache-2.11-cpan-39bf76dae61 )