line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Telegram::BotKit::Keyboards; |
2
|
|
|
|
|
|
|
$Telegram::BotKit::Keyboards::VERSION = '0.03'; |
3
|
|
|
|
|
|
|
# ABSTRACT: Easy creation of keyboards for Telegram bots |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
788
|
use common::sense; |
|
1
|
|
|
|
|
9
|
|
|
1
|
|
|
|
|
5
|
|
7
|
1
|
|
|
1
|
|
512
|
use JSON::MaybeXS; |
|
1
|
|
|
|
|
8683
|
|
|
1
|
|
|
|
|
57
|
|
8
|
1
|
|
|
1
|
|
692
|
use Encode qw(decode); |
|
1
|
|
|
|
|
9601
|
|
|
1
|
|
|
|
|
107
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
8
|
use Exporter qw(import); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
775
|
|
11
|
|
|
|
|
|
|
our @EXPORT_OK = qw(create_one_time_keyboard create_inline_keyboard parse_reply_markup available_keys); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my $is_inline_flag = 0; # 1 = inline / 0 = one item at column |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub create_one_time_keyboard { |
19
|
0
|
|
|
0
|
1
|
0
|
my ($keys, $k_per_row) = @_; |
20
|
0
|
0
|
|
|
|
0
|
if (!(defined $k_per_row)) { |
21
|
0
|
0
|
|
|
|
0
|
if ($is_inline_flag) { $k_per_row = scalar @$keys } else { $k_per_row = 1 }; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
0
|
|
|
|
|
0
|
my @keyboard; |
25
|
|
|
|
|
|
|
my @row; |
26
|
0
|
|
|
|
|
0
|
for my $i (1 .. scalar @$keys) { |
27
|
0
|
|
|
|
|
0
|
my $el = $keys->[$i-1]; |
28
|
0
|
|
|
|
|
0
|
push @row, $el; |
29
|
0
|
0
|
0
|
|
|
0
|
if ((($i % $k_per_row) == 0) || ($i == scalar @$keys)) { |
30
|
0
|
|
|
|
|
0
|
push (@keyboard, [ @row ]); |
31
|
0
|
|
|
|
|
0
|
@row=(); |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
0
|
my %rpl_markup = ( |
36
|
|
|
|
|
|
|
keyboard => \@keyboard, |
37
|
|
|
|
|
|
|
one_time_keyboard => JSON::MaybeXS::JSON->true |
38
|
|
|
|
|
|
|
); |
39
|
0
|
|
|
|
|
0
|
my $json = JSON::MaybeXS->new(utf8 => 1); |
40
|
0
|
|
|
|
|
0
|
return decode('UTF-8', $json->encode(\%rpl_markup)); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub create_inline_keyboard { |
45
|
1
|
|
|
1
|
1
|
12
|
my ($keys, $k_per_row) = @_; |
46
|
1
|
50
|
|
|
|
2
|
if (!(defined $k_per_row)) { |
47
|
0
|
0
|
|
|
|
0
|
if ($is_inline_flag) { $k_per_row = scalar @$keys } else { $k_per_row = 1 }; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
48
|
|
|
|
|
|
|
} |
49
|
1
|
|
|
|
|
2
|
my @keyboard; |
50
|
|
|
|
|
|
|
my @row; |
51
|
1
|
|
|
|
|
3
|
for my $i (1 .. scalar @$keys) { |
52
|
5
|
|
|
|
|
7
|
my $el = $keys->[$i-1]; |
53
|
5
|
|
|
|
|
8
|
push @row, { "text" => $el, "callback_data" => $el }; |
54
|
5
|
100
|
100
|
|
|
18
|
if ((($i % $k_per_row) == 0) || ($i == scalar @$keys)) { |
55
|
2
|
|
|
|
|
3
|
push (@keyboard, [ @row ]); |
56
|
2
|
|
|
|
|
3
|
@row=(); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
} |
59
|
1
|
|
|
|
|
3
|
my %rpl_markup = ( |
60
|
|
|
|
|
|
|
inline_keyboard => \@keyboard |
61
|
|
|
|
|
|
|
); |
62
|
1
|
|
|
|
|
9
|
my $json = JSON::MaybeXS->new(utf8 => 1); |
63
|
1
|
|
|
|
|
67
|
return decode('UTF-8', $json->encode(\%rpl_markup)); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub available_keys { |
69
|
0
|
|
|
0
|
1
|
0
|
my $arr = shift; |
70
|
0
|
|
|
|
|
0
|
my $text = '[ '; |
71
|
0
|
|
|
|
|
0
|
$text.= join(' | ',@$arr); |
72
|
0
|
|
|
|
|
0
|
$text.= ' ]'; |
73
|
0
|
|
|
|
|
0
|
return $text; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub parse_reply_markup { |
78
|
2
|
|
|
2
|
1
|
1883
|
my $reply_markup = shift; |
79
|
2
|
|
|
|
|
13
|
my $data_structure = decode_json($reply_markup); |
80
|
2
|
|
|
|
|
3
|
my @res; |
81
|
|
|
|
|
|
|
my @keyboard; |
82
|
2
|
|
|
|
|
2
|
my $is_inline_flag = 0; |
83
|
|
|
|
|
|
|
|
84
|
2
|
50
|
|
|
|
6
|
if (defined $data_structure->{inline_keyboard}) { |
|
|
50
|
|
|
|
|
|
85
|
0
|
|
|
|
|
0
|
@keyboard = {$data_structure->{inline_keyboard}}; |
86
|
0
|
|
|
|
|
0
|
$is_inline_flag = 1; |
87
|
|
|
|
|
|
|
} elsif (defined $data_structure->{keyboard}) { |
88
|
2
|
|
|
|
|
1
|
@keyboard = @{$data_structure->{keyboard}}; |
|
2
|
|
|
|
|
5
|
|
89
|
|
|
|
|
|
|
} else { |
90
|
0
|
|
|
|
|
0
|
warn "reply_markup structure isn't recognized"; |
91
|
0
|
|
|
|
|
0
|
return undef; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
2
|
|
|
|
|
4
|
for my $i (@keyboard) { |
95
|
4
|
|
|
|
|
5
|
for (@$i) { |
96
|
6
|
50
|
|
|
|
8
|
if ($is_inline_flag) { |
97
|
0
|
|
|
|
|
0
|
push @res, $_->{text}; |
98
|
|
|
|
|
|
|
} else { |
99
|
6
|
|
|
|
|
7
|
push @res, $_; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
2
|
|
|
|
|
13
|
return \@res; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
1; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
__END__ |