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