line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package LiBot::Provider::Lingr; |
2
|
1
|
|
|
1
|
|
1808
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
43
|
|
3
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
33
|
|
4
|
1
|
|
|
1
|
|
7
|
use utf8; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
24
|
use Mouse; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
9
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
has host => ( |
9
|
|
|
|
|
|
|
is => 'ro', |
10
|
|
|
|
|
|
|
required => 1, |
11
|
|
|
|
|
|
|
); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has port => ( |
14
|
|
|
|
|
|
|
is => 'ro', |
15
|
|
|
|
|
|
|
required => 1, |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
|
18
|
1
|
|
|
1
|
|
497
|
no Mouse; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
19
|
|
|
|
|
|
|
|
20
|
1
|
|
|
1
|
|
1131
|
use Plack::Request; |
|
1
|
|
|
|
|
114510
|
|
|
1
|
|
|
|
|
31
|
|
21
|
1
|
|
|
1
|
|
9
|
use JSON qw(decode_json); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
9
|
|
22
|
1
|
|
|
1
|
|
152
|
use Encode qw(encode_utf8 decode_utf8); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
52
|
|
23
|
1
|
|
|
1
|
|
786
|
use Twiggy::Server; |
|
1
|
|
|
|
|
20185
|
|
|
1
|
|
|
|
|
33
|
|
24
|
1
|
|
|
1
|
|
668
|
use Plack::Builder; |
|
1
|
|
|
|
|
6800
|
|
|
1
|
|
|
|
|
97
|
|
25
|
1
|
|
|
1
|
|
7
|
use Module::Runtime; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
10
|
|
26
|
|
|
|
|
|
|
|
27
|
1
|
|
|
1
|
|
33
|
use LiBot::Message; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
528
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub handle_request { |
30
|
0
|
|
|
0
|
0
|
|
my ($self, $bot, $json) = @_; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
return sub { |
33
|
0
|
|
|
0
|
|
|
my $respond = shift; |
34
|
|
|
|
|
|
|
my $callback = sub { |
35
|
0
|
|
|
|
|
|
my $ret = shift; |
36
|
0
|
|
|
|
|
|
$ret =~ s!\n+$!!; |
37
|
0
|
|
0
|
|
|
|
$respond->([200, ['Content-Type' => 'text/plain'], [encode_utf8($ret || '')]]); |
38
|
0
|
|
|
|
|
|
}; |
39
|
0
|
0
|
0
|
|
|
|
if ( $json && $json->{events} ) { |
40
|
0
|
|
|
|
|
|
for my $event ( @{ $json->{events} } ) { |
|
0
|
|
|
|
|
|
|
41
|
0
|
|
|
|
|
|
my $msg = LiBot::Message->new( |
42
|
|
|
|
|
|
|
text => $event->{message}->{text}, |
43
|
|
|
|
|
|
|
nickname => $event->{message}->{nickname}, |
44
|
|
|
|
|
|
|
); |
45
|
0
|
|
|
|
|
|
my $proceeded = eval { |
46
|
0
|
|
|
|
|
|
$bot->handle_message($callback, $msg) |
47
|
|
|
|
|
|
|
}; |
48
|
0
|
0
|
|
|
|
|
if ($@) { |
49
|
0
|
|
|
|
|
|
print STDERR $@; |
50
|
0
|
|
|
|
|
|
die $@; |
51
|
|
|
|
|
|
|
} else { |
52
|
0
|
0
|
|
|
|
|
if ($proceeded) { |
53
|
0
|
|
|
|
|
|
return; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# Not proceeeded. |
60
|
0
|
|
|
|
|
|
$respond->([200, ['Content-Type' => 'text/plain'], ['']]); |
61
|
0
|
|
|
|
|
|
}; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub to_app { |
65
|
0
|
|
|
0
|
0
|
|
my ($self, $bot) = @_; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub { |
68
|
0
|
|
|
0
|
|
|
my $req = Plack::Request->new(shift); |
69
|
|
|
|
|
|
|
|
70
|
0
|
0
|
|
|
|
|
if ($req->method eq 'POST') { |
71
|
0
|
|
|
|
|
|
my $json = decode_json($req->content); |
72
|
0
|
|
|
|
|
|
return $self->handle_request($bot, $json); |
73
|
|
|
|
|
|
|
} else { |
74
|
|
|
|
|
|
|
# lingr server always calls me by POST method. |
75
|
|
|
|
|
|
|
# This is human's health check page. |
76
|
0
|
|
|
|
|
|
return [200, ['Content-Type' => 'text/plain'], ["I'm lingr bot"]]; |
77
|
|
|
|
|
|
|
} |
78
|
0
|
|
|
|
|
|
}; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub run { |
82
|
0
|
|
|
0
|
0
|
|
my ($self, $bot) = @_; |
83
|
|
|
|
|
|
|
|
84
|
0
|
|
|
|
|
|
my $server = Twiggy::Server->new( |
85
|
|
|
|
|
|
|
host => $self->host, |
86
|
|
|
|
|
|
|
port => $self->port, |
87
|
|
|
|
|
|
|
); |
88
|
0
|
|
|
|
|
|
$bot->log->info("Lingr bot server: http://%s:%s/\n", $self->host, $self->port); |
89
|
|
|
|
|
|
|
$server->register_service(builder { |
90
|
0
|
|
|
0
|
|
|
enable 'AccessLog'; |
91
|
0
|
|
|
|
|
|
$self->to_app($bot); |
92
|
0
|
|
|
|
|
|
}); |
93
|
0
|
|
|
|
|
|
return $server; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
1; |
97
|
|
|
|
|
|
|
__END__ |