1

1-st run fixes after refactor

This commit is contained in:
eleksir
2021-04-24 16:12:30 +03:00
parent a3f8b43f07
commit fbab0a4d88
11 changed files with 50 additions and 49 deletions

View File

@@ -121,7 +121,7 @@ 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)
# run bot, at last # run bot, at last
run_telegrambot (); RunTelegramBot ();
if (-f $pidfile) { if (-f $pidfile) {
unlink $pidfile; unlink $pidfile;

View File

@@ -11,8 +11,9 @@ use Carp qw (carp);
use Math::Random::Secure qw (irand); use Math::Random::Secure qw (irand);
use BotLib::Conf qw (LoadConf); use BotLib::Conf qw (LoadConf);
use BotLib::Admin qw (@forbiddenMessageTypes GetForbiddenTypes AddForbiddenType DelForbiddenType ListForbidden use BotLib::Admin qw (@ForbiddenMessageTypes GetForbiddenTypes AddForbiddenType
FortuneToggle FortuneStatus PluginToggle PluginStatus PluginEnabled); DelForbiddenType ListForbidden FortuneToggle FortuneStatus
PluginToggle PluginStatus PluginEnabled);
use BotLib::Archeologist qw (Dig); use BotLib::Archeologist qw (Dig);
use BotLib::Buni qw (Buni); use BotLib::Buni qw (Buni);
use BotLib::Drink qw (Drink); use BotLib::Drink qw (Drink);
@@ -138,7 +139,7 @@ sub Command {
substr ($text, 1) eq 'сиси' || substr ($text, 1) eq 'сиси' ||
substr ($text, 1) eq 'сисечки' substr ($text, 1) eq 'сисечки'
) { ) {
if (pluginEnabled $chatid, 'oboobs') { if (PluginEnabled $chatid, 'oboobs') {
$msg->typing (); $msg->typing ();
$reply = Oboobs (); $reply = Oboobs ();
sleep (irand (2) + 1); sleep (irand (2) + 1);
@@ -153,7 +154,7 @@ sub Command {
substr ($text, 1) eq 'попа' || substr ($text, 1) eq 'попа' ||
substr ($text, 1) eq 'попка' substr ($text, 1) eq 'попка'
) { ) {
if (pluginEnabled $chatid, 'obutts') { if (PluginEnabled $chatid, 'obutts') {
$msg->typing (); $msg->typing ();
$reply = Obutts (); $reply = Obutts ();
sleep (irand (2) + 1); sleep (irand (2) + 1);

View File

@@ -37,17 +37,16 @@ sub GetForbiddenTypes {
); );
foreach (@ForbiddenMessageTypes) { foreach (@ForbiddenMessageTypes) {
my $type = $c->get ($_); my $type = $cache->get ($_);
if ($type) { if ($type) {
$messageTypes->{$_} = 1; $messageTypes->{$_} = 1;
} else { } else {
unless (defined $type) { unless (defined $type) {
$c->set ($_, 1, 'never'); $cache->set ($_, 0, 'never');
$messageTypes->{$_} = 1;
} else {
$messageTypes->{$_} = 0;
} }
$messageTypes->{$_} = 0;
} }
} }
@@ -93,17 +92,16 @@ sub ListForbidden {
); );
foreach (@ForbiddenMessageTypes) { foreach (@ForbiddenMessageTypes) {
my $type = $c->get ($_); my $type = $cache->get ($_);
if ($type) { if ($type) {
push @list, sprintf 'Тип сообщения %s удаляется', $_; push @list, sprintf 'Тип сообщения %s удаляется', $_;
} else { } else {
unless (defined $type) { unless (defined $type) {
push @list, sprintf 'Тип сообщения %s удаляется', $_; $cache->set ($_, 0, 'never');
$c->set ($_, 1, 'never');
} else {
push @list, sprintf 'Тип сообщения %s не удаляется', $_;
} }
push @list, sprintf 'Тип сообщения %s не удаляется', $_;
} }
} }
@@ -191,9 +189,13 @@ sub PluginStatus (@) {
my $state = $cache->get ($plugin); my $state = $cache->get ($plugin);
if (defined $state && $state) { if ($state) {
$phrase .= 'включён.'; $phrase .= 'включён.';
} else { } else {
unless (defined $state) {
$cache->set ($plugin, 0, 'never');
}
$phrase .= 'выключен.'; $phrase .= 'выключен.';
} }

View File

@@ -20,6 +20,7 @@ sub Buni {
for (1..3) { for (1..3) {
my $ua = Mojo::UserAgent->new->connect_timeout (3); my $ua = Mojo::UserAgent->new->connect_timeout (3);
$ua->max_redirects(3);
$r = $ua->get ('http://www.bunicomic.com/?random&nocache=1')->result; $r = $ua->get ('http://www.bunicomic.com/?random&nocache=1')->result;
if ($r->is_success) { if ($r->is_success) {

View File

@@ -15,7 +15,7 @@ use BotLib::Conf qw (LoadConf);
use version; our $VERSION = qw (1.0); use version; our $VERSION = qw (1.0);
use Exporter qw (import); use Exporter qw (import);
our @EXPORT_OK = qw (seed friday); our @EXPORT_OK = qw (Seed Friday);
my $c = LoadConf (); my $c = LoadConf ();
my $dir = $c->{friday}->{dir}; my $dir = $c->{friday}->{dir};

View File

@@ -9,7 +9,7 @@ use English qw ( -no_match_vars );
use Carp qw (cluck); use Carp qw (cluck);
use Math::Random::Secure qw (irand); use Math::Random::Secure qw (irand);
use Mojo::UserAgent; use Mojo::UserAgent;
use BotLIb::Image::Flickr qw (FlickrByTags); use BotLib::Image::Flickr qw (FlickrByTags);
use BotLib::Image::Imgur qw (Imgur); use BotLib::Image::Imgur qw (Imgur);
use BotLib::Util qw (urlencode); use BotLib::Util qw (urlencode);
@@ -17,7 +17,7 @@ use version; our $VERSION = qw (1.0);
use Exporter qw (import); use Exporter qw (import);
our @EXPORT_OK = qw (Kitty Fox Oboobs Obutts Rabbit Owl); our @EXPORT_OK = qw (Kitty Fox Oboobs Obutts Rabbit Owl);
sub kitty { sub Kitty {
my $r; my $r;
my $ret = 'Нету кошечек, все разбежались.'; my $ret = 'Нету кошечек, все разбежались.';
@@ -52,7 +52,7 @@ sub kitty {
return $ret; return $ret;
} }
sub fox { sub Fox {
my $r; my $r;
my $ret = 'Нету лисичек, все разбежались.'; my $ret = 'Нету лисичек, все разбежались.';
@@ -87,7 +87,7 @@ sub fox {
return $ret; return $ret;
} }
sub oboobs { sub Oboobs {
my $r; my $r;
my $ret = 'Нету cисичек, все разбежались.'; my $ret = 'Нету cисичек, все разбежались.';
@@ -122,7 +122,7 @@ sub oboobs {
return $ret; return $ret;
} }
sub obutts { sub Obutts {
my $r; my $r;
my $ret = 'Нету попок, все разбежались.'; my $ret = 'Нету попок, все разбежались.';
@@ -179,7 +179,7 @@ sub rabbit_imgur {
} }
} }
sub rabbit { sub Rabbit {
# rabbit, but bunny # rabbit, but bunny
my $url = FlickrByTags ('animal,bunny'); my $url = FlickrByTags ('animal,bunny');
@@ -190,7 +190,7 @@ sub rabbit {
} }
} }
sub owl { sub Owl {
my $url = FlickrByTags ('bird,owl'); my $url = FlickrByTags ('bird,owl');
if (defined $url) { if (defined $url) {

View File

@@ -185,10 +185,10 @@ sub flickrAuthorization {
my $ua = Mojo::UserAgent->new->connect_timeout (5); my $ua = Mojo::UserAgent->new->connect_timeout (5);
my $r = $ua->get ($url)->result; my $r = $ua->get ($url)->result;
if ($r->is_success) { if ($r->code == 302) {
return $r->url; return $r->content->headers->location;
} else { } else {
say "Something went wrong: $r->code $r->message"; ## no critic (InputOutput::RequireCheckedSyscalls) printf 'Something went wrong: %s %s', $r->code, $r->message;
return; return;
} }
} }
@@ -471,7 +471,7 @@ sub FlickrInit {
my $flickr_request_token_secret = $cache->get ('flickr_request_token_secret'); my $flickr_request_token_secret = $cache->get ('flickr_request_token_secret');
unless (defined $flickr_request_token || defined $flickr_request_token_secret) { unless (defined $flickr_request_token || defined $flickr_request_token_secret) {
say "Looks like there is no request token or request token secret in config db."; ## no critic (InputOutput::RequireCheckedSyscalls) say 'Looks like there is no request token or request token secret in config db.'; ## no critic (InputOutput::RequireCheckedSyscalls)
say 'Please remove verifier settings from config.json and run this script again.'; ## no critic (InputOutput::RequireCheckedSyscalls) say 'Please remove verifier settings from config.json and run this script again.'; ## no critic (InputOutput::RequireCheckedSyscalls)
return 0; return 0;
} }

View File

@@ -19,6 +19,7 @@ use Exporter qw (import);
our @EXPORT_OK = qw (Imgur); our @EXPORT_OK = qw (Imgur);
my $c = LoadConf (); my $c = LoadConf ();
my $cachedir = $c->{cachedir};
sub Imgur { sub Imgur {
my $tag = shift; my $tag = shift;
@@ -27,20 +28,20 @@ sub Imgur {
my $search_url = 'https://api.imgur.com/3/gallery/search/'; my $search_url = 'https://api.imgur.com/3/gallery/search/';
my $imgur_client_id = $c->{image}->{imgur}->{client_id} // do { my $imgur_client_id = $c->{image}->{imgur}->{client_id} // do {
cluck "No client_id specified in config for imgur module"; cluck 'No client_id specified in config for imgur module';
return undef; return undef;
}; };
my $imgur_client_secret = $c->{image}->{imgur}->{client_secret} // do { my $imgur_client_secret = $c->{image}->{imgur}->{client_secret} // do {
cluck "No client_secret specified in config for imgur module"; cluck 'No client_secret specified in config for imgur module';
return undef; return undef;
}; };
my $imgur_access_token = $c->{image}->{imgur}->{access_token} // do { my $imgur_access_token = $c->{image}->{imgur}->{access_token} // do {
cluck "No access_token specified in config for imgur module"; cluck 'No access_token specified in config for imgur module';
return undef; return undef;
}; };
my $imgur_refresh_token = $c->{image}->{imgur}->{refresh_token} // do { my $imgur_refresh_token = $c->{image}->{imgur}->{refresh_token} // do {
cluck "No refresh_token specified in config for imgur module"; cluck 'No refresh_token specified in config for imgur module';
} };
# N.B. Both imgur_access_token and imgur_refresh_token time to time can change, so we need # N.B. Both imgur_access_token and imgur_refresh_token time to time can change, so we need
# some place where we can store new values # some place where we can store new values
@@ -81,7 +82,6 @@ sub Imgur {
unless ($r->is_success) { unless ($r->is_success) {
cluck "Unable to refresh imgur token, please re-authorise app! or imgur experiencing api outage: $r->code $r->message"; cluck "Unable to refresh imgur token, please re-authorise app! or imgur experiencing api outage: $r->code $r->message";
untie %secret;
return undef; return undef;
} }
@@ -89,12 +89,11 @@ sub Imgur {
if (defined $refresh) { if (defined $refresh) {
$imgur_refresh_token = $refresh->{refresh_token}; $imgur_refresh_token = $refresh->{refresh_token};
$secret{imgur_refresh_token} = $refresh->{refresh_token}; $cache->set ('refresh_token', $refresh->{refresh_token}, 'never');
$imgur_access_token = $refresh->{access_token}; $imgur_access_token = $refresh->{access_token};
$secret{imgur_access_token} = $refresh->{access_token}; $cache->set ('imgur_access_token', $refresh->{access_token}, 'never');
} else { } else {
cluck "Unable to refresh imgur access_token, unable to decode json: $EVAL_ERROR"; cluck "Unable to refresh imgur access_token, unable to decode json: $EVAL_ERROR";
untie %secret;
return undef; return undef;
} }

View File

@@ -11,7 +11,7 @@ use Carp qw (cluck);
use File::Basename qw (dirname); use File::Basename qw (dirname);
use File::Path qw (make_path); use File::Path qw (make_path);
use Hailo; use Hailo;
use BotLib::Conf qw (loadConf); use BotLib::Conf qw (LoadConf);
use version; our $VERSION = qw (1.0); use version; our $VERSION = qw (1.0);
use Exporter qw (import); use Exporter qw (import);

View File

@@ -16,12 +16,12 @@ use BotLib::Util qw (trim urlencode);
use version; our $VERSION = qw (1.0); use version; our $VERSION = qw (1.0);
use Exporter qw (import); use Exporter qw (import);
our @EXPORT_OK = qw (weather); our @EXPORT_OK = qw (Weather);
my $c = LoadConf (); my $c = LoadConf ();
my $cachedir = $c->{cachedir}; my $cachedir = $c->{cachedir};
sub weather { sub Weather {
my $city = shift; my $city = shift;
$city = trim $city; $city = trim $city;

View File

@@ -13,7 +13,6 @@ use File::Path qw (make_path);
use Hailo; use Hailo;
use Math::Random::Secure qw (irand); use Math::Random::Secure qw (irand);
use Mojo::Base 'Teapot::Bot::Brain'; use Mojo::Base 'Teapot::Bot::Brain';
use BotLib::Admin qw (FortuneToggleList); use BotLib::Admin qw (FortuneToggleList);
use BotLib qw (RandomCommonPhrase Command Highlight BotSleep IsCensored); use BotLib qw (RandomCommonPhrase Command Highlight BotSleep IsCensored);
use BotLib::Conf qw (LoadConf); use BotLib::Conf qw (LoadConf);
@@ -48,10 +47,10 @@ sub __cron {
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime (time); my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime (time);
if ($hour == 8 && ($min >= 0 && $min <= 14)) { if ($hour == 8 && ($min >= 0 && $min <= 14)) {
foreach my $enabledFortuneChat (FortuneToggleList ()) { foreach my $enabledfortunechat (fortuneToggleList ()) {
my $send_args; my $send_args;
$send_args->{text} = sprintf "%s\n```\n%s\n```\n", $intro[irand ($#intro + 1)], trim (Fortune ()); $send_args->{text} = sprintf "%s\n```\n%s\n```\n", $intro[irand ($#intro + 1)], trim (Fortune ());
$send_args->{chat_id} = $enabledFortuneChat; $send_args->{chat_id} = $enabledfortunechat;
$send_args->{parse_mode} = 'Markdown'; $send_args->{parse_mode} = 'Markdown';
$self->sendMessage ($send_args); $self->sendMessage ($send_args);
} }
@@ -66,7 +65,7 @@ sub __on_msg {
my $chatid; my $chatid;
my $chatname = 'Noname chat'; my $chatname = 'Noname chat';
# user sending message info # user sending message info
my ($userid, $username, $fullname, $highlight, $vis_a_vi) = Highlight $msg; my ($userid, $username, $fullname, $highlight, $vis_a_vi) = BotLib::Highlight ($msg);
my $csign = $c->{telegrambot}->{csign}; my $csign = $c->{telegrambot}->{csign};
unless ($myid) { unless ($myid) {
@@ -150,11 +149,11 @@ sub __on_msg {
# lazy init chat-bot brains # lazy init chat-bot brains
unless (defined $hailo->{$chatid}) { unless (defined $hailo->{$chatid}) {
my $brainName = sprintf '%s/%s.brain.sqlite', $c->{telegrambot}->{braindir}, $chatid; my $brainname = sprintf '%s/%s.brain.sqlite', $c->{telegrambot}->{braindir}, $chatid;
$hailo->{$chatid} = Hailo->new ( $hailo->{$chatid} = Hailo->new (
# we'll got file like this: data/telegrambot-brains/-1001332512695.brain.sqlite # we'll got file like this: data/telegrambot-brains/-1001332512695.brain.sqlite
brain => $brainName, brain => $brainname,
order => 3 order => 3
); );
@@ -168,7 +167,6 @@ sub __on_msg {
# is this a 1-on-1 ? # is this a 1-on-1 ?
if ($msg->chat->type eq 'private') { if ($msg->chat->type eq 'private') {
unless (defined $msg->text) { unless (defined $msg->text) {
# TODO: it can be sticker, image, voice, video, etc. Maybe, i'll handle it once? :)
return; return;
} }
@@ -281,7 +279,7 @@ sub __on_msg {
# bot mention by name # bot mention by name
} elsif ((lc ($text) =~ /.+ ${qname}[\,|\!|\?|\.| ]/) or (lc ($text) =~ / $qname$/)) { } elsif ((lc ($text) =~ /.+ ${qname}[\,|\!|\?|\.| ]/) or (lc ($text) =~ / $qname$/)) {
$phrase = $text; $phrase = $text;
$reply = $hailo->{$msg->chat->id}->reply ($phrase); $reply = $hailo->{$msg->chat->id}->reply($phrase);
# bot mention by telegram name # bot mention by telegram name
} elsif ((lc ($text) =~ /.+ ${qtname}[\,|\!|\?|\.| ]/) or (lc ($text) =~ / $qtname$/)) { } elsif ((lc ($text) =~ /.+ ${qtname}[\,|\!|\?|\.| ]/) or (lc ($text) =~ / $qtname$/)) {
$phrase = $text; $phrase = $text;
@@ -352,7 +350,7 @@ sub init {
sub RunTelegramBot { sub RunTelegramBot {
while (sleep 3) { while (sleep 3) {
eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval) eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
telegrambot->new->think; TelegramBot->new->think;
} }
} }