Add initial redis support

This commit is contained in:
eleksir
2021-12-11 13:57:23 +03:00
parent 71a2471725
commit ab89105168
4 changed files with 61 additions and 1 deletions

28
lib/RedisLib.pm Normal file
View File

@@ -0,0 +1,28 @@
package RedisLib;
use 5.018;
use strict;
use warnings;
use utf8;
use open qw (:std :utf8);
use Log::Any qw ($log);
use Data::Dumper;
use version; our $VERSION = qw (1.0);
use Exporter qw (import);
our @EXPORT_OK = qw (redisListener);
sub parse_message {
my $self = shift;
my $m = shift;
my $answer = $m;
my $send_to = $answer->{from};
$log->error (Dumper $self);
return;
};
1;
# vim: set ft=perl noet ai ts=4 sw=4 sts=4:

View File

@@ -19,6 +19,7 @@ use BotLib::Conf qw (LoadConf);
use BotLib::Fortune qw (Fortune);
use BotLib::Karma qw (KarmaSet);
use BotLib::Util qw (trim fmatch);
use RedisLib qw (redisListener);
use version; our $VERSION = qw (1.0);
use Exporter qw (import);
@@ -31,6 +32,7 @@ my $myusername;
my $myfirst_name;
my $mylast_name;
my $myfullname;
my $redis_mutex;
has token => $c->{telegrambot}->{token};
@@ -356,6 +358,28 @@ sub __on_msg {
return;
}
sub __redisListener {
return if ($redis_mutex);
my $redis = Mojo::Redis->new (
sprintf 'redis://%s:%s/1', $c->{redis_server}, $c->{redis_port}
);
my $pubsub = $redis->pubsub;
my $sub;
foreach my $channel (@{$c->{redis_channels}}) {
$log->info ("Subscribing to $channel");
$sub->{$channel} = $pubsub->json ($channel)->listen (
$channel => sub { \&redis_parse_message->(@_); }
);
}
$redis_mutex = 1;
return;
}
# setup our bot
sub init {
my $self = shift;
@@ -369,6 +393,7 @@ sub init {
}
$self->add_listener (\&__on_msg);
$self->add_listener (\&__redisListener);
$self->add_repeating_task (900, \&__cron);
return;
}