line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojar::Message::Telegram; |
2
|
1
|
|
|
1
|
|
250477
|
use Mojo::Base -base; |
|
1
|
|
|
|
|
13
|
|
|
1
|
|
|
|
|
6
|
|
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
136
|
use Carp qw(croak); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
36
|
|
5
|
1
|
|
|
1
|
|
415
|
use Mojo::URL; |
|
1
|
|
|
|
|
6834
|
|
|
1
|
|
|
|
|
6
|
|
6
|
1
|
|
|
1
|
|
483
|
use Mojo::UserAgent; |
|
1
|
|
|
|
|
190517
|
|
|
1
|
|
|
|
|
9
|
|
7
|
1
|
|
|
1
|
|
46
|
use Mojo::Util qw(dumper); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
695
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = 0.001; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# Attributes |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has address => 'api.telegram.org'; |
14
|
|
|
|
|
|
|
has scheme => 'https'; |
15
|
|
|
|
|
|
|
has 'token'; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has in => sub { [] }; |
18
|
|
|
|
|
|
|
has out => sub { [] }; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Public methods |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub send { |
23
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
24
|
0
|
0
|
0
|
|
|
|
my $cb = @_ && ref $_[-1] eq 'CODE' ? pop : undef; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
my %args = @_ % 2 == 0 ? @_ |
27
|
0
|
0
|
|
|
|
|
: ref $_[0] eq 'HASH' ? %{$_[0]} |
|
0
|
0
|
|
|
|
|
|
28
|
|
|
|
|
|
|
: croak 'Bad args'; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
my @param = ( |
31
|
|
|
|
|
|
|
chat_id => $args{recipient}, |
32
|
|
|
|
|
|
|
text => $args{message}, |
33
|
0
|
|
|
|
|
|
); |
34
|
0
|
0
|
|
|
|
|
push @param, $cb if $cb; |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
return $self->submit(sendMessage => @param); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub submit { |
40
|
0
|
|
|
0
|
0
|
|
my ($self, $method) = (shift, shift); |
41
|
0
|
0
|
0
|
|
|
|
my $cb = @_ && ref $_[-1] eq 'CODE' ? pop : undef; |
42
|
0
|
|
|
|
|
|
my %payload = @_; |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
|
my $url = Mojo::URL->new->scheme($self->scheme) |
45
|
|
|
|
|
|
|
->host($self->address) |
46
|
|
|
|
|
|
|
->path(sprintf 'bot%s/%s', $self->token, $method); |
47
|
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
|
my $ua = Mojo::UserAgent->new; |
49
|
0
|
|
|
|
|
|
my $headers = {}; |
50
|
0
|
|
|
|
|
|
my $tx = $ua->build_tx('POST', $url, $headers, json => \%payload); |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# blocking |
53
|
0
|
0
|
|
|
|
|
unless ($cb) { |
54
|
0
|
|
|
|
|
|
$tx = $ua->start($tx); |
55
|
0
|
0
|
|
|
|
|
if (my $err = $tx->error) { |
56
|
0
|
|
|
|
|
|
return $err; |
57
|
|
|
|
|
|
|
} |
58
|
0
|
0
|
|
|
|
|
die "Failed to send message\n" unless $tx->res->json->{ok}; |
59
|
0
|
|
|
|
|
|
return $self; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# non-blocking |
63
|
|
|
|
|
|
|
$ua->start($tx, sub { |
64
|
0
|
|
|
0
|
|
|
my ($ua, $tx_) = @_; |
65
|
0
|
|
|
|
|
|
my ($err, $json); |
66
|
0
|
0
|
|
|
|
|
$json = $tx_->res->json unless $err = $tx_->error; |
67
|
|
|
|
|
|
|
($err //= {})->{message} ||= 'Failed to send message' |
68
|
0
|
0
|
0
|
|
|
|
unless $tx->res->json->{ok}; |
|
|
|
0
|
|
|
|
|
69
|
0
|
|
|
|
|
|
Mojo::IOLoop->next_tick(sub { $self->$cb($err, $json, $tx_) }); |
|
0
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
}); |
71
|
0
|
|
|
|
|
|
return $self; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
1; |
75
|
|
|
|
|
|
|
__END__ |