Try to move most of static commands to redis rails
This commit is contained in:
@@ -145,7 +145,9 @@ local $SIG{TERM} = \&__signal_handler;
|
|||||||
$PROGRAM_NAME = abs_path $PROGRAM_NAME; ## no critic (Variables::RequireLocalizedPunctuationVars)
|
$PROGRAM_NAME = abs_path $PROGRAM_NAME; ## no critic (Variables::RequireLocalizedPunctuationVars)
|
||||||
|
|
||||||
# make some global vars for telegram and redis context
|
# make some global vars for telegram and redis context
|
||||||
our ($TGM $REDIS);
|
our $TGM;
|
||||||
|
our $REDIS;
|
||||||
|
our $RCONN;
|
||||||
|
|
||||||
# run bot, at last
|
# run bot, at last
|
||||||
RunTelegramBot ();
|
RunTelegramBot ();
|
||||||
|
|||||||
@@ -67,6 +67,8 @@ sub Command {
|
|||||||
my ($userid, $username, $fullname, $highlight, $visavi) = Highlight ($msg);
|
my ($userid, $username, $fullname, $highlight, $visavi) = Highlight ($msg);
|
||||||
|
|
||||||
my $rmsg->{from} = 'telegram';
|
my $rmsg->{from} = 'telegram';
|
||||||
|
$rmsg->{misc}->{answer} = 1;
|
||||||
|
$rmsg->{misc}->{csign} = $csign;
|
||||||
$rmsg->{userid} = $userid;
|
$rmsg->{userid} = $userid;
|
||||||
$rmsg->{chatid} = $chatid;
|
$rmsg->{chatid} = $chatid;
|
||||||
$rmsg->{plugin} = 'telegram';
|
$rmsg->{plugin} = 'telegram';
|
||||||
@@ -77,13 +79,20 @@ sub Command {
|
|||||||
$rmsg->{mode} = 'public';
|
$rmsg->{mode} = 'public';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (substr ($text, 1) eq 'ping') {
|
if ((substr ($text, 1) eq 'ping') || (substr ($text, 1) eq 'пинг') || (substr ($text, 1) eq 'pong') ||
|
||||||
|
(substr ($text, 1) eq 'понг') || (substr ($text, 1) eq 'coin' || substr ($text, 1) eq 'монетка') ||
|
||||||
|
(substr ($text, 1) eq 'roll' || substr ($text, 1) eq 'dice' || substr ($text, 1) eq 'кости') ||
|
||||||
|
(substr ($text, 1) eq 'ver' || substr ($text, 1) eq 'version' || substr ($text, 1) eq 'версия') ||
|
||||||
|
(substr ($text, 1) eq 'хэлп' || substr ($text, 1) eq 'halp') ||
|
||||||
|
(substr ($text, 1) eq 'kde' || substr ($text, 1) eq 'кде') ) {
|
||||||
$rmsg->{message} = $text;
|
$rmsg->{message} = $text;
|
||||||
$self->log->debug ('[DEBUG] Sending message to redis ' . Dumper ($rmsg));
|
$self->log->debug ('[DEBUG] Sending message to redis ' . Dumper ($rmsg));
|
||||||
|
|
||||||
until (eval { Mojo::Redis::Connection->is_connected ($main::REDIS); }) {
|
{
|
||||||
$log->info ('[INFO] Not yet connected to redis');
|
do {
|
||||||
sleep 1;
|
my $ready = eval { $main::RCONN->is_connected; };
|
||||||
|
last if ($ready);
|
||||||
|
} while (sleep 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
my $pubsub = $main::REDIS->pubsub;
|
my $pubsub = $main::REDIS->pubsub;
|
||||||
@@ -91,34 +100,6 @@ sub Command {
|
|||||||
$pubsub->json ($c->{'redis_router_channel'})->notify (
|
$pubsub->json ($c->{'redis_router_channel'})->notify (
|
||||||
$c->{'redis_router_channel'} => $rmsg
|
$c->{'redis_router_channel'} => $rmsg
|
||||||
);
|
);
|
||||||
} elsif (substr ($text, 1) eq 'пинг') {
|
|
||||||
$reply = 'Понг.';
|
|
||||||
} elsif (substr ($text, 1) eq 'pong') {
|
|
||||||
$reply = 'Wat?';
|
|
||||||
} elsif (substr ($text, 1) eq 'понг') {
|
|
||||||
$reply = 'Шта?';
|
|
||||||
} elsif (substr ($text, 1) eq 'coin' || substr ($text, 1) eq 'монетка') {
|
|
||||||
if (rand (101) < 0.016) {
|
|
||||||
$reply = 'ребро';
|
|
||||||
} else {
|
|
||||||
if (irand (2) == 0) {
|
|
||||||
if (irand (2) == 0) {
|
|
||||||
$reply = 'орёл';
|
|
||||||
} else {
|
|
||||||
$reply = 'аверс';
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (irand (2) == 0) {
|
|
||||||
$reply = 'решка';
|
|
||||||
} else {
|
|
||||||
$reply = 'реверс';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} elsif (substr ($text, 1) eq 'roll' || substr ($text, 1) eq 'dice' || substr ($text, 1) eq 'кости') {
|
|
||||||
$reply = sprintf 'На первой кости выпало %d, а на второй — %d.', irand (6) + 1, irand (6) + 1;
|
|
||||||
} elsif (substr ($text, 1) eq 'ver' || substr ($text, 1) eq 'version' || substr ($text, 1) eq 'версия') {
|
|
||||||
$reply = 'Версия: Четыре.Технологическое_превью';
|
|
||||||
} elsif (substr ($text, 1) eq 'anek' || substr ($text, 1) eq 'анек' || substr ($text, 1) eq 'анекдот') {
|
} elsif (substr ($text, 1) eq 'anek' || substr ($text, 1) eq 'анек' || substr ($text, 1) eq 'анекдот') {
|
||||||
$msg->typing ();
|
$msg->typing ();
|
||||||
$reply = "```\n" . Anek () . "\n```";
|
$reply = "```\n" . Anek () . "\n```";
|
||||||
@@ -264,17 +245,6 @@ sub Command {
|
|||||||
sleep (irand (2) + 1);
|
sleep (irand (2) + 1);
|
||||||
$msg->replyMd ($reply);
|
$msg->replyMd ($reply);
|
||||||
return;
|
return;
|
||||||
} elsif (substr ($text, 1) eq 'хэлп' || substr ($text, 1) eq 'halp') {
|
|
||||||
$reply = 'HALP!!!11';
|
|
||||||
} elsif (substr ($text, 1) eq 'kde' || substr ($text, 1) eq 'кде') {
|
|
||||||
my @phrases = (
|
|
||||||
'Нет, я не буду поднимать вам плазму.',
|
|
||||||
'Повторяйте эту мантру по утрам не менее 5 раз: "Плазма не падает." И, возможно, она перестанет у вас падать.',
|
|
||||||
'Плазма не падает, она просто выходит отдохнуть.',
|
|
||||||
'Плазма это не агрегатное состояние, это суперпозиция. Во время работы она находится сразу в жидком, мягком и тёплом состояниях.',
|
|
||||||
);
|
|
||||||
|
|
||||||
$reply = $phrases[irand ($#phrases + 1)];
|
|
||||||
} elsif (substr ($text, 1) eq 'help' || substr ($text, 1) eq 'помощь') {
|
} elsif (substr ($text, 1) eq 'help' || substr ($text, 1) eq 'помощь') {
|
||||||
$reply = << "MYHELP";
|
$reply = << "MYHELP";
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -422,12 +422,14 @@ sub __redisListener {
|
|||||||
$r->on (
|
$r->on (
|
||||||
connection => sub {
|
connection => sub {
|
||||||
my ($redis, $connection) = @_;
|
my ($redis, $connection) = @_;
|
||||||
|
$main::RCONN = $connection;
|
||||||
|
|
||||||
# Log error
|
# Log error
|
||||||
$connection->on (
|
$connection->on (
|
||||||
error => sub {
|
error => sub {
|
||||||
my ($conn, $error) = @_;
|
my ($conn, $error) = @_;
|
||||||
$log->error ("[ERROR] Redis connection error: $error");
|
$log->error ("[ERROR] Redis connection error: $error");
|
||||||
|
$main::RCONN = undef;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@@ -439,13 +441,14 @@ sub __redisListener {
|
|||||||
# Subscribe to channels
|
# Subscribe to channels
|
||||||
my $pubsub = $r->pubsub;
|
my $pubsub = $r->pubsub;
|
||||||
my $sub;
|
my $sub;
|
||||||
|
my $rpm = \&redis_parse_message;
|
||||||
$log->notice ('[NOTICE] Subscribing to redis channels');
|
$log->notice ('[NOTICE] Subscribing to redis channels');
|
||||||
|
|
||||||
foreach my $channel (@{$c->{redis_channels}}) {
|
foreach my $channel (@{$c->{redis_channels}}) {
|
||||||
$log->info ("[INFO] Subscribing to $channel");
|
$log->info ("[INFO] Subscribing to $channel");
|
||||||
|
|
||||||
$sub->{$channel} = $pubsub->json ($channel)->listen (
|
$sub->{$channel} = $pubsub->json ($channel)->listen (
|
||||||
$channel => sub { \&redis_parse_message->(@_); }
|
$channel => sub { $rpm->(@_); }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user