-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignalBot.pm
More file actions
332 lines (256 loc) · 9.08 KB
/
SignalBot.pm
File metadata and controls
332 lines (256 loc) · 9.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package SignalBot;
use strict;
use warnings;
use Data::Dumper;
use Mojo::Base 'SignalBotHelper';
use Time::Piece;
use POSIX;
has message => undef;
has groupID => undef;
has source => undef;
sub MessageReceived {
my $self= shift;
my $timestamp=shift;
$self->source(shift());
$self->groupID(shift());
$self->message(shift());
my $attachments=shift;
$self->check_moduls;
return;
}
sub check_moduls {
my $self = shift;
$self->modul_statistics;
$self->modul_commands;
}
sub modul_commands {
my $self = shift;
if ($self->message =~ m|^/bot (.*)$|) {
my $commands = [split(" ", $1)];
$self->command_send_pong($commands) if ($commands->[0] eq 'ping');
$self->command_send_statistic($commands) if ($commands->[0] eq 'statistik');
$self->command_set_event_time($commands) if ($commands->[0] eq 'event');
$self->command_help($commands) if ($commands->[0] eq 'help');
$self->command_humhub_post($commands) if ($commands->[0] eq 'post');
$self->command_get_group_id($commands) if ($commands->[0] eq 'groupId');
}
}
sub modul_statistics {
my $self = shift;
$self->dbh->do( "
INSERT
statistic
SET
groupe = ?,
user = ?;
", undef,
(
$self->signal_cli->getGroupName,
$self->source,
),
);
}
sub command_get_group_id {
my $self = shift;
my $options = shift;
$self->signal_cli->sendGroupMessage(join(",", @{$self->groupID}));
}
sub command_humhub_post {
my $self = shift;
my $options = shift;
# my $error = humhub_post("Message from Perl script. es funktioniert :) yeaaa");
# add_signal_message("Leider konnte der post nicht erstellt werden: $error", $message) unless $error;
}
sub command_help {
my $self = shift;
my $options = shift;
my $msg = '
Erstelle neuen Termin:
/bot event start 21.03.2019 13:33
Terminliste abruffen:
/bot event list
Prüffen ob bot lebt:
/bot ping
Statistik abruffen:
/bot statistik
Diese Hilfe abruffen:
/bot help
';
$self->signal_cli->sendGroupMessage($msg);
}
# move to modul/event.pm
sub command_set_event_time {
my $self = shift;
my $options = shift;
# /bot event
# @TODO: Nach umwandlung der struktur macht das keinen sinn... send message to single user :D
# Feature is only working in groups
unless (defined($self->groupID)) {
$self->signal_cli->sendGroupMessage("Events funktioniert leider nur im Gruppenchat...");
return;
}
if (scalar(@{$options}) == 2 && $options->[1] eq "list") {
my $events = $self->mysql_get_events_from_groupe;
my $events_msg = "Events der nächsten 14 Tage:\n";
foreach (values %{$events}) {
$events_msg .= localtime($_->{start_time})->strftime('%d.%m.%Y %H:%M')." - ".localtime($_->{end_time})->strftime('%d.%m.%Y %H:%M')." -> ".$_->{name}."\n";
}
$self->signal_cli->sendGroupMessage($events_msg);
return;
}
# /bot event start 21.03.2019 13:33
# temporar injection fix
unless (scalar(@{$options}) == 4 && $options->[1] =~ m/[a-z]{1,6}/ && $options->[2] =~ m/\d\d.\d\d.\d\d\d\d/ && $options->[3] =~ m/\d\d:\d\d/) {
$self->signal_cli->sendGroupMessage("Event validation error :( nutze dieses format: /bot event start 21.03.2019 13:33");
return;
}
# I don't know the timezone from the user... I need a timezone conzept...
# At the moment I use the system timezone
# Save it here as GTM and handle the timezone on the other places will be better in a international project
my $event_time = Time::Piece->strptime($options->[2]." ".$options->[3]." ".strftime("%z", localtime()), "%d.%m.%Y %H:%M %z");
$self->logEntry("create event: ".$event_time. " timestamp: ".$event_time->epoch);
$self->dbh->do("
INSERT
event
SET
groupe = ?,
start_time = FROM_UNIXTIME(?),
end_time = FROM_UNIXTIME(?),
name = ?;
", undef,
(
$self->signal_cli->getGroupName,
$event_time->epoch,
$event_time->epoch + 2*60*60,
$options->[1],
),
);
}
# move to modul/event.pm
sub mysql_get_events {
my $self = shift;
return $self->dbh->selectall_hashref( '
select id, groupe, UNIX_TIMESTAMP(start_time) as start_time, UNIX_TIMESTAMP(end_time) as end_time, name, status from event where ((start_time >= NOW() - INTERVAL 2 DAY AND start_time < NOW() + INTERVAL 14 DAY) OR start_time < NOW() AND end_time > NOW());
',
'id',
undef,
(
)
);
}
# move to modul/event.pm
sub mysql_get_events_from_groupe {
my $self = shift;
return $self->dbh->selectall_hashref( '
select id, UNIX_TIMESTAMP(start_time) as start_time, UNIX_TIMESTAMP(end_time) as end_time, name, status from event where groupe = ? AND ((start_time >= NOW() - INTERVAL 2 DAY AND start_time < NOW() + INTERVAL 14 DAY) OR start_time < NOW() AND end_time > NOW());
',
'id',
undef,
(
$self->signal_cli->getGroupName
)
);
}
# move to modul/event.pm and merge with mysql_get_events
sub mysql_get_all_upcomming_humhub_events {
my $self = shift;
return $self->dbh->selectall_hashref( '
select id, humhub_id, groupe, UNIX_TIMESTAMP(start_time) as start_time, UNIX_TIMESTAMP(end_time) as end_time, name, status from event where humhub_id > 0 AND ((start_time >= NOW() - INTERVAL 2 DAY) OR start_time < NOW() AND end_time > NOW());
',
'id',
undef,
(
)
);
}
sub mudul_humhub_event_import {
my $self = shift;
my $entrys = $self->mysql_humhub_calendar;
my $events = $self->mysql_get_all_upcomming_humhub_events;
my $existing_events = {};
foreach (values %{$events}){
$existing_events->{$_->{start_time}.$_->{end_time}.$_->{name}.$_->{humhub_id}} = 1;
}
foreach my $entry (values %{$entrys}) {
# I don't know the timezone from the user... I need a timezone conzept...
# At the moment I use the system timezone
# Save it here as GTM and handle the timezone on the other places will be better in a international project
my $event_time_start = Time::Piece->strptime($entry->{start_datetime}." ".strftime("%z", localtime()), "%Y-%m-%d %H:%M:%S %z");
my $event_time_end = Time::Piece->strptime($entry->{end_datetime}." ".strftime("%z", localtime()), "%Y-%m-%d %H:%M:%S %z");
# do not import old events
next if (localtime->epoch >= $event_time_start->epoch);
# Exist this event with exact the same data
next if (defined ($existing_events->{$event_time_start->epoch.$event_time_end->epoch.$entry->{title}.$entry->{id}}));
next unless ($self->resolve_humhub_space($entry->{space}));
# Better to change to a object, or oly use groupId to answer... now i have to fake a lot, when i will add a event not from the chat :(
$self->logEntry("set event time: ".$event_time_start. " timestamp: ".$event_time_start->epoch);
$self->dbh->do("
REPLACE INTO
event
SET
groupe = ?,
start_time = FROM_UNIXTIME(?),
end_time = FROM_UNIXTIME(?),
name = ?,
humhub_id = ?;
", undef,
(
$self->resolve_humhub_space($entry->{space}), # reolve humhub space to chat group
$event_time_start->epoch,
$event_time_end->epoch,
$entry->{title},
$entry->{id}
),
);
}
}
sub resolve_humhub_space {
my $self = shift;
my $space = shift;
unless (defined($self->config->humhub_data->{$space}->{group})) {
$self->logEntry("humhub Space konnte nicht aufgelöst werden: ".$space);
}
return $self->config->humhub_data->{$space}->{group};
}
sub mysql_humhub_calendar {
my $self = shift;
my $sql = 'SELECT ce.*, s.name as space FROM calendar_entry ce, content c, contentcontainer cc, space s WHERE c.object_id = ce.id and c.object_model = "humhub\\\\modules\\\\calendar\\\\models\\\\CalendarEntry" and c.contentcontainer_id = cc.id and cc.guid = s.guid AND ((start_datetime >= NOW() - INTERVAL 2 DAY) OR start_datetime < NOW() AND end_datetime > NOW())';
my $result = $self->dbh_humhub->selectall_hashref(
$sql
,
'id',
undef,
() );
return $result;
}
sub command_send_pong {
shift()->signal_cli->sendGroupMessage("pong");
}
sub command_send_statistic {
my $self = shift;
my $statistic =
$self->dbh->selectall_hashref( '
select user, count(user) as count from statistic where time>=? and time<=? and groupe = ? GROUP by user;
',
'user',
undef,
(
'2011-03-17 06:42:10',
'2020-03-17 07:42:50',
$self->signal_cli->getGroupName
)
);
my $send_message = "Geschriebene Nachrichten:\n";
foreach (sort {$statistic->{$b}->{count} cmp $statistic->{$a}->{count}}keys %{$statistic}) {
$send_message .= $self->resolve_number($_). ": ".$statistic->{$_}->{count}."\n";
}
$self->signal_cli->sendGroupMessage($send_message);
}
sub resolve_number {
my $self = shift;
my $user = shift;
my $resolve_user = $self->config->resolveUser;
$user = $resolve_user->{$user} if ($resolve_user->{$user});
return $user;
}
1;