| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Telegram::BotKit::Sessions; |
|
2
|
|
|
|
|
|
|
$Telegram::BotKit::Sessions::VERSION = '0.02'; |
|
3
|
|
|
|
|
|
|
# ABSTRACT: Implements memory of Telegram::BotKit::Wizard state automat. Almost all methods has at least one argument = chat_id |
|
4
|
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
9619
|
use common::sense; |
|
|
1
|
|
|
|
|
9
|
|
|
|
1
|
|
|
|
|
5
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub new { |
|
12
|
1
|
|
|
1
|
1
|
10
|
my $class = shift; |
|
13
|
1
|
|
|
|
|
2
|
my $sess_obj = {}; |
|
14
|
1
|
|
|
|
|
2
|
$sess_obj->{session} = {}; |
|
15
|
1
|
|
|
|
|
2
|
bless $sess_obj, $class; |
|
16
|
1
|
|
|
|
|
3
|
return $sess_obj; |
|
17
|
|
|
|
|
|
|
} |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub all { |
|
22
|
0
|
|
|
0
|
1
|
0
|
my ($self, $chat_id) = @_; |
|
23
|
0
|
0
|
|
|
|
0
|
if ($chat_id) { |
|
24
|
0
|
0
|
|
|
|
0
|
if (defined $self->{session}{$chat_id}) { |
|
25
|
0
|
|
|
|
|
0
|
return $self->{session}{$chat_id}; |
|
26
|
|
|
|
|
|
|
} else { |
|
27
|
0
|
|
|
|
|
0
|
return undef; |
|
28
|
|
|
|
|
|
|
} |
|
29
|
|
|
|
|
|
|
} else { |
|
30
|
0
|
|
|
|
|
0
|
return $self->{session}; |
|
31
|
|
|
|
|
|
|
} |
|
32
|
|
|
|
|
|
|
} |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub all_keys_arr { |
|
37
|
0
|
|
|
0
|
1
|
0
|
my ($self, $chat_id, $key) = @_; |
|
38
|
0
|
|
|
|
|
0
|
my @msgs; |
|
39
|
0
|
|
|
|
|
0
|
for (@{$self->{session}{$chat_id}}) { |
|
|
0
|
|
|
|
|
0
|
|
|
40
|
0
|
|
|
|
|
0
|
push @msgs, $_->{$key}; |
|
41
|
|
|
|
|
|
|
} |
|
42
|
0
|
|
|
|
|
0
|
return \@msgs; |
|
43
|
|
|
|
|
|
|
} |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub start { |
|
47
|
0
|
|
|
0
|
1
|
0
|
my ($self, $chat_id) = @_; |
|
48
|
0
|
|
|
|
|
0
|
$self->{session}{$chat_id} = []; |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub del { |
|
54
|
0
|
|
|
0
|
1
|
0
|
my ($self, $chat_id) = @_; |
|
55
|
0
|
|
|
|
|
0
|
delete $self->{session}{$chat_id}; |
|
56
|
|
|
|
|
|
|
} |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub set { |
|
61
|
1
|
|
|
1
|
1
|
11
|
my ($self, $chat_id, $array) = @_; |
|
62
|
1
|
|
|
|
|
17
|
$self->{session}{$chat_id} = $array; |
|
63
|
|
|
|
|
|
|
} |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub update { |
|
69
|
0
|
|
|
0
|
1
|
0
|
my ($self, $chat_id, $hash_with_data) = @_; |
|
70
|
0
|
|
|
|
|
0
|
my @active_sess_uids = keys %{$self->{session}}; |
|
|
0
|
|
|
|
|
0
|
|
|
71
|
|
|
|
|
|
|
|
|
72
|
0
|
0
|
|
|
|
0
|
if ((grep { $chat_id eq $_ } @active_sess_uids)) { |
|
|
0
|
|
|
|
|
0
|
|
|
73
|
0
|
|
|
|
|
0
|
push @{$self->{session}->{$chat_id}}, $hash_with_data; |
|
|
0
|
|
|
|
|
0
|
|
|
74
|
|
|
|
|
|
|
} |
|
75
|
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
0
|
return $self->{session}->{$chat_id}; |
|
77
|
|
|
|
|
|
|
} |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub last { |
|
82
|
0
|
|
|
0
|
1
|
0
|
my ($self, $chat_id) = @_; |
|
83
|
0
|
0
|
|
|
|
0
|
if ($self->{session}{$chat_id}) { |
|
84
|
0
|
|
|
|
|
0
|
my @sess_for_chat = @{$self->{session}{$chat_id}}; |
|
|
0
|
|
|
|
|
0
|
|
|
85
|
0
|
|
|
|
|
0
|
return $sess_for_chat[$#sess_for_chat]; # last elenemt |
|
86
|
|
|
|
|
|
|
} else { |
|
87
|
0
|
|
|
|
|
0
|
return undef; |
|
88
|
|
|
|
|
|
|
} |
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub combine_properties { |
|
93
|
1
|
|
|
1
|
1
|
206
|
my ($self, $chat_id, $params) = @_; |
|
94
|
1
|
|
|
|
|
2
|
my $kv = {}; |
|
95
|
1
|
|
|
|
|
2
|
for (@{$self->{session}{$chat_id}}) { |
|
|
1
|
|
|
|
|
5
|
|
|
96
|
2
|
|
|
|
|
8
|
$kv->{$_->{$params->{first_property}}} = $_->{$params->{second_property}}; |
|
97
|
|
|
|
|
|
|
} |
|
98
|
1
|
|
|
|
|
1
|
my $returned_hash; |
|
99
|
1
|
|
|
|
|
3
|
$returned_hash->{$params->{name_of_hash_key}} = $kv; |
|
100
|
1
|
|
|
|
|
7
|
return $returned_hash; |
|
101
|
|
|
|
|
|
|
} |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub combine_properties_retrospective { |
|
105
|
3
|
|
|
3
|
1
|
1240
|
my ($self, $chat_id, $params) = @_; |
|
106
|
3
|
|
|
|
|
5
|
my $kv = {}; |
|
107
|
3
|
|
|
|
|
4
|
my @sess = @{$self->{session}{$chat_id}}; |
|
|
3
|
|
|
|
|
9
|
|
|
108
|
|
|
|
|
|
|
|
|
109
|
3
|
|
|
|
|
9
|
for my $i (0 .. $#sess-1) { |
|
110
|
3
|
|
|
|
|
6
|
my $key = $sess[$i]->{$params->{first_property}}; |
|
111
|
3
|
|
|
|
|
6
|
my $value = $sess[$i+1]->{$params->{second_property}}; |
|
112
|
3
|
|
|
|
|
8
|
$kv->{$key} = $value; |
|
113
|
|
|
|
|
|
|
} |
|
114
|
|
|
|
|
|
|
|
|
115
|
3
|
|
|
|
|
3
|
my $returned_hash; |
|
116
|
3
|
|
|
|
|
5
|
$returned_hash->{$params->{name_of_hash_key}} = $kv; |
|
117
|
3
|
|
|
|
|
13
|
return $returned_hash; |
|
118
|
|
|
|
|
|
|
} |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub get_replies_hash { |
|
122
|
1
|
|
|
1
|
1
|
64
|
my ($self, $chat_id) = @_; |
|
123
|
1
|
|
|
|
|
3
|
my $params = { |
|
124
|
|
|
|
|
|
|
name_of_hash_key => 'replies', |
|
125
|
|
|
|
|
|
|
first_property => 'screen', |
|
126
|
|
|
|
|
|
|
second_property => 'callback_text' |
|
127
|
|
|
|
|
|
|
}; |
|
128
|
1
|
|
|
|
|
3
|
my $res = $self->combine_properties_retrospective($chat_id,$params); |
|
129
|
1
|
|
|
|
|
3
|
return $res; |
|
130
|
|
|
|
|
|
|
} |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
1; |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
__END__ |