BotKit/Polling.pm untested

filter: Statementstmt 0.0% 0 / 34 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 42 CC 2 SCAR 12.0
CC
2
Cov
0%
get_last_messages
6.0
BEGIN
2.0
BEGIN
2.0
CRAP 3.3
1 package Telegram::BotKit::Polling;
2 $Telegram::BotKit::Polling::VERSION = '0.03';
3 # ABSTRACT: Implementation of Telegram getUpdates method (long polling). For easy testing your bot code locally
4  
5  
6 use WWW::Telegram::BotAPI;
7 use List::MoreUtils qw/uniq/;
8 use common::sense;
9  
10 use Exporter qw(import);
11 our @EXPORT_OK = qw(get_last_messages);
12  
13  
14  
15 sub get_last_messages {
16 my $api = shift;
17 my @buffer = @{$api->getUpdates()->{result}};
18  
19 if (@buffer) {
20 my $h = {};
21 my $last_update = {};
22  
23 # 1. get unique chat ids
24 my @chat_ids;
25 for (@buffer) {
26 push @chat_ids, $_->{'message'}->{'chat'}->{'id'};
27 }
28 @chat_ids = uniq @chat_ids;
29  
30 # 2. get last message from each chat (assuming that it's sorted by Telegram)
31 for my $id (@chat_ids) {
32 my @i = grep { $_->{'message'}->{'chat'}->{'id'} eq $id } @buffer;
33 $h->{$id} = pop @i; # last message with higher message_id and update_id
34 }
35  
36 # 3. Clean buffer on server
37 my $last_update = pop @buffer;
38 $api->getUpdates({ offset => $last_update->{update_id} + 1.0 })->{result}; # clean buffer on server
39 return $h;
40 }
41
42 return undef;
43 };
44  
45 1;
46  
47 __END__