| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Bot::ChatBots::Telegram::Role::Source; | 
| 2 | 3 |  |  | 3 |  | 5971 | use strict; | 
|  | 3 |  |  |  |  | 16 |  | 
|  | 3 |  |  |  |  | 94 |  | 
| 3 | 3 |  |  | 3 |  | 31 | use warnings; | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 133 |  | 
| 4 |  |  |  |  |  |  | { our $VERSION = '0.014'; } | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 3 |  |  | 3 |  | 18 | use Ouch; | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 17 |  | 
| 7 | 3 |  |  | 3 |  | 219 | use Log::Any; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 16 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 3 |  |  | 3 |  | 206 | use Moo::Role; | 
|  | 3 |  |  |  |  | 36 |  | 
|  | 3 |  |  |  |  | 20 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | has token => ( | 
| 12 |  |  |  |  |  |  | is => 'ro', | 
| 13 |  |  |  |  |  |  | lazy => 1, | 
| 14 |  |  |  |  |  |  | predicate => 1, | 
| 15 |  |  |  |  |  |  | default => sub { ouch 500, 'token is not defined' } | 
| 16 |  |  |  |  |  |  | ); | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | has sender => ( | 
| 19 |  |  |  |  |  |  | is      => 'ro', | 
| 20 |  |  |  |  |  |  | lazy    => 1, | 
| 21 |  |  |  |  |  |  | default => sub {    # prefer has-a in this case | 
| 22 |  |  |  |  |  |  | my $self = shift; | 
| 23 |  |  |  |  |  |  | require Bot::ChatBots::Telegram::Sender; | 
| 24 |  |  |  |  |  |  | return Bot::ChatBots::Telegram::Sender->new(token => $self->token); | 
| 25 |  |  |  |  |  |  | }, | 
| 26 |  |  |  |  |  |  | ); | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | { | 
| 29 |  |  |  |  |  |  | my %data_type_for = ( | 
| 30 |  |  |  |  |  |  | message => 'Message', | 
| 31 |  |  |  |  |  |  | edited_message => 'Message', | 
| 32 |  |  |  |  |  |  | channel_post => 'Message', | 
| 33 |  |  |  |  |  |  | edited_channel_post => 'Message', | 
| 34 |  |  |  |  |  |  | inline_query => 'InlineQuery', | 
| 35 |  |  |  |  |  |  | chosen_inline_result => 'ChosenInlineResult', | 
| 36 |  |  |  |  |  |  | callback_query => 'CallbackQuery', | 
| 37 |  |  |  |  |  |  | shipping_query => 'ShippingQuery', | 
| 38 |  |  |  |  |  |  | pre_checkout_query => 'PreCheckoutQuery', | 
| 39 |  |  |  |  |  |  | ); | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | sub normalize_record { | 
| 42 | 3 |  |  | 3 | 1 | 3056 | my ($self, $record) = @_; | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 3 | 100 |  |  |  | 19 | my $update = $record->{update} or ouch 500, 'no update found!'; | 
| 45 | 2 |  |  |  |  | 8 | $record->{source}{technology} = 'telegram'; | 
| 46 | 2 |  | 66 |  |  | 15 | $record->{source}{token} //= $record->{source}{object_token}; | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 2 |  |  |  |  | 9 | my ($type) = grep { $_ ne 'update_id' } keys %$update; | 
|  | 4 |  |  |  |  | 15 |  | 
| 49 | 2 |  |  |  |  | 6 | $record->{type} = $type; | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 2 |  | 50 |  |  | 11 | $record->{data_type} = $data_type_for{$type} || 'unknown'; | 
| 52 |  |  |  |  |  |  |  | 
| 53 | 2 |  |  |  |  | 7 | my $payload = $record->{payload} = $update->{$type}; | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 2 |  |  |  |  | 5 | $record->{sender} = $payload->{from}; | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 2 |  |  |  |  | 9 | return $self->_normalize_record_chan($record); | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | sub _normalize_record_chan { | 
| 62 | 2 |  |  | 2 |  | 6 | my ($self, $record) = @_; | 
| 63 | 2 |  |  |  |  | 6 | my ($dtype, $payload) = @{$record}{qw< data_type payload >}; | 
|  | 2 |  |  |  |  | 7 |  | 
| 64 | 2 |  |  |  |  | 4 | my $chan; | 
| 65 | 2 | 50 |  |  |  | 8 | if ($dtype eq 'Message') { | 
|  |  | 0 |  |  |  |  |  | 
| 66 | 2 |  |  |  |  | 4 | $chan = {%{$payload->{chat}}}; | 
|  | 2 |  |  |  |  | 15 |  | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  | elsif ($dtype eq 'CallbackQuery') { | 
| 69 | 0 | 0 |  |  |  | 0 | if (exists $payload->{message}) { | 
| 70 | 0 |  |  |  |  | 0 | $chan = {%{$payload->{message}{chat}}}; | 
|  | 0 |  |  |  |  | 0 |  | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  | else { # FIXME guessing correctly here? | 
| 73 | 0 |  |  |  |  | 0 | $chan = {id => $payload->{chat_instance}}; | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  | } | 
| 76 | 2 | 50 |  |  |  | 8 | if ($chan) { | 
| 77 | 2 | 50 |  |  |  | 14 | $chan->{fqid} = "$chan->{type}/$chan->{id}" if exists $chan->{id}; | 
| 78 | 2 |  |  |  |  | 9 | $record->{channel} = $chan; | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 2 |  |  |  |  | 8 | return $record; | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | 1; |