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