-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.pl
More file actions
69 lines (63 loc) · 2.36 KB
/
server.pl
File metadata and controls
69 lines (63 loc) · 2.36 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
#!/usr/bin/env perl
use WWW::Telegram::BotAPI;
use Telegram::Bot::Message;
use Data::Dumper;
use Tie::File;
#### Config section ####
my $token = $ENV{DOORKEYBOT_TOKEN} || '441592632:AAEACcNg6CXM_As4-zg4m68WrChis547iuo';
my $api = WWW::Telegram::BotAPI->new (
token => $token
);
my $bot_name = $api->getMe->{result}{username};
my $filename = 'allowed_usernames.txt';
my $door_open_script = './root/fabkey/open.sh';
my $open_cmd = '/open';
my $group_chat_id = '-1001127744219';
########################
tie @array, 'Tie::File', $filename or die "Cant open $filename";
warn Dumper \@array;
while(1) {
my @updates = @{$api->getUpdates->{result}};
if (@updates) {
for my $u (@updates) {
warn Dumper $u;
my $username = $u->{message}{from}{username};
my $text = $u->{message}{text};
my $chat_id = $u->{message}{chat}{id};
if ( grep {$_ eq $username} @array ) {
warn "User allowed to open";
if ( ($text eq $open_cmd) || ($text eq $open_cmd.'@'.$bot_name ) ) {
if ($chat_id eq $group_chat_id ) {
`$door_open_script`;
warn "Door opened!";
$api->sendMessage ({
chat_id => $u->{message}{chat}{id},
text => 'Door opened. Welcome, @'.$username.'!',
reply_to_message_id => $u->{message}{message_id}
});
} else {
$api->sendMessage ({
chat_id => $u->{message}{chat}{id},
text => 'For now it s allowed to use bot only in group chat',
reply_to_message_id => $u->{message}{message_id}
});
}
} else {
warn "Wrong command";
# $api->sendMessage ({
# chat_id => $u->{message}{chat}{id},
# text => 'Wrong command',
# reply_to_message_id => $u->{message}{message_id}
# });
}
} else {
$api->sendMessage ({
chat_id => $u->{message}{chat}{id},
text => 'You are not allowed to open the door. Contact administrator to get permissions',
reply_to_message_id => $u->{message}{message_id}
});
}
$api->getUpdates({ offset => $u->{update_id} + 1.0 }); # clean buffer
}
}
};