diff --git a/bin/aleesa-telegram-bot b/bin/aleesa-telegram-bot index 8d1f555..554c828 100755 --- a/bin/aleesa-telegram-bot +++ b/bin/aleesa-telegram-bot @@ -121,7 +121,7 @@ local $SIG{TERM} = \&__signal_handler; $PROGRAM_NAME = abs_path $PROGRAM_NAME; ## no critic (Variables::RequireLocalizedPunctuationVars) # run bot, at last -run_telegrambot (); +RunTelegramBot (); if (-f $pidfile) { unlink $pidfile; diff --git a/lib/BotLib.pm b/lib/BotLib.pm index f06ea96..f307141 100644 --- a/lib/BotLib.pm +++ b/lib/BotLib.pm @@ -11,8 +11,9 @@ use Carp qw (carp); use Math::Random::Secure qw (irand); use BotLib::Conf qw (LoadConf); -use BotLib::Admin qw (@forbiddenMessageTypes GetForbiddenTypes AddForbiddenType DelForbiddenType ListForbidden - FortuneToggle FortuneStatus PluginToggle PluginStatus PluginEnabled); +use BotLib::Admin qw (@ForbiddenMessageTypes GetForbiddenTypes AddForbiddenType + DelForbiddenType ListForbidden FortuneToggle FortuneStatus + PluginToggle PluginStatus PluginEnabled); use BotLib::Archeologist qw (Dig); use BotLib::Buni qw (Buni); use BotLib::Drink qw (Drink); @@ -138,7 +139,7 @@ sub Command { substr ($text, 1) eq 'сиси' || substr ($text, 1) eq 'сисечки' ) { - if (pluginEnabled $chatid, 'oboobs') { + if (PluginEnabled $chatid, 'oboobs') { $msg->typing (); $reply = Oboobs (); sleep (irand (2) + 1); @@ -153,7 +154,7 @@ sub Command { substr ($text, 1) eq 'попа' || substr ($text, 1) eq 'попка' ) { - if (pluginEnabled $chatid, 'obutts') { + if (PluginEnabled $chatid, 'obutts') { $msg->typing (); $reply = Obutts (); sleep (irand (2) + 1); diff --git a/lib/BotLib/Admin.pm b/lib/BotLib/Admin.pm index 271a756..c5ecf5b 100644 --- a/lib/BotLib/Admin.pm +++ b/lib/BotLib/Admin.pm @@ -37,17 +37,16 @@ sub GetForbiddenTypes { ); foreach (@ForbiddenMessageTypes) { - my $type = $c->get ($_); + my $type = $cache->get ($_); if ($type) { $messageTypes->{$_} = 1; } else { unless (defined $type) { - $c->set ($_, 1, 'never'); - $messageTypes->{$_} = 1; - } else { - $messageTypes->{$_} = 0; + $cache->set ($_, 0, 'never'); } + + $messageTypes->{$_} = 0; } } @@ -93,17 +92,16 @@ sub ListForbidden { ); foreach (@ForbiddenMessageTypes) { - my $type = $c->get ($_); + my $type = $cache->get ($_); if ($type) { push @list, sprintf 'Тип сообщения %s удаляется', $_; } else { unless (defined $type) { - push @list, sprintf 'Тип сообщения %s удаляется', $_; - $c->set ($_, 1, 'never'); - } else { - push @list, sprintf 'Тип сообщения %s не удаляется', $_; + $cache->set ($_, 0, 'never'); } + + push @list, sprintf 'Тип сообщения %s не удаляется', $_; } } @@ -191,9 +189,13 @@ sub PluginStatus (@) { my $state = $cache->get ($plugin); - if (defined $state && $state) { + if ($state) { $phrase .= 'включён.'; } else { + unless (defined $state) { + $cache->set ($plugin, 0, 'never'); + } + $phrase .= 'выключен.'; } diff --git a/lib/BotLib/Buni.pm b/lib/BotLib/Buni.pm index 1290571..f677f51 100644 --- a/lib/BotLib/Buni.pm +++ b/lib/BotLib/Buni.pm @@ -20,6 +20,7 @@ sub Buni { for (1..3) { my $ua = Mojo::UserAgent->new->connect_timeout (3); + $ua->max_redirects(3); $r = $ua->get ('http://www.bunicomic.com/?random&nocache=1')->result; if ($r->is_success) { diff --git a/lib/BotLib/Friday.pm b/lib/BotLib/Friday.pm index d06639a..468fe5b 100644 --- a/lib/BotLib/Friday.pm +++ b/lib/BotLib/Friday.pm @@ -15,7 +15,7 @@ use BotLib::Conf qw (LoadConf); use version; our $VERSION = qw (1.0); use Exporter qw (import); -our @EXPORT_OK = qw (seed friday); +our @EXPORT_OK = qw (Seed Friday); my $c = LoadConf (); my $dir = $c->{friday}->{dir}; diff --git a/lib/BotLib/Image.pm b/lib/BotLib/Image.pm index d333968..a436eda 100644 --- a/lib/BotLib/Image.pm +++ b/lib/BotLib/Image.pm @@ -9,7 +9,7 @@ use English qw ( -no_match_vars ); use Carp qw (cluck); use Math::Random::Secure qw (irand); use Mojo::UserAgent; -use BotLIb::Image::Flickr qw (FlickrByTags); +use BotLib::Image::Flickr qw (FlickrByTags); use BotLib::Image::Imgur qw (Imgur); use BotLib::Util qw (urlencode); @@ -17,7 +17,7 @@ use version; our $VERSION = qw (1.0); use Exporter qw (import); our @EXPORT_OK = qw (Kitty Fox Oboobs Obutts Rabbit Owl); -sub kitty { +sub Kitty { my $r; my $ret = 'Нету кошечек, все разбежались.'; @@ -52,7 +52,7 @@ sub kitty { return $ret; } -sub fox { +sub Fox { my $r; my $ret = 'Нету лисичек, все разбежались.'; @@ -87,7 +87,7 @@ sub fox { return $ret; } -sub oboobs { +sub Oboobs { my $r; my $ret = 'Нету cисичек, все разбежались.'; @@ -122,7 +122,7 @@ sub oboobs { return $ret; } -sub obutts { +sub Obutts { my $r; my $ret = 'Нету попок, все разбежались.'; @@ -179,7 +179,7 @@ sub rabbit_imgur { } } -sub rabbit { +sub Rabbit { # rabbit, but bunny my $url = FlickrByTags ('animal,bunny'); @@ -190,7 +190,7 @@ sub rabbit { } } -sub owl { +sub Owl { my $url = FlickrByTags ('bird,owl'); if (defined $url) { diff --git a/lib/BotLib/Image/Flickr.pm b/lib/BotLib/Image/Flickr.pm index 8b385ac..8b841c2 100644 --- a/lib/BotLib/Image/Flickr.pm +++ b/lib/BotLib/Image/Flickr.pm @@ -185,10 +185,10 @@ sub flickrAuthorization { my $ua = Mojo::UserAgent->new->connect_timeout (5); my $r = $ua->get ($url)->result; - if ($r->is_success) { - return $r->url; + if ($r->code == 302) { + return $r->content->headers->location; } else { - say "Something went wrong: $r->code $r->message"; ## no critic (InputOutput::RequireCheckedSyscalls) + printf 'Something went wrong: %s %s', $r->code, $r->message; return; } } @@ -471,7 +471,7 @@ sub FlickrInit { my $flickr_request_token_secret = $cache->get ('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) return 0; } diff --git a/lib/BotLib/Image/Imgur.pm b/lib/BotLib/Image/Imgur.pm index 9b317c4..81efd87 100644 --- a/lib/BotLib/Image/Imgur.pm +++ b/lib/BotLib/Image/Imgur.pm @@ -19,6 +19,7 @@ use Exporter qw (import); our @EXPORT_OK = qw (Imgur); my $c = LoadConf (); +my $cachedir = $c->{cachedir}; sub Imgur { my $tag = shift; @@ -27,20 +28,20 @@ sub Imgur { my $search_url = 'https://api.imgur.com/3/gallery/search/'; 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; }; 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; }; 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; }; 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 # some place where we can store new values @@ -81,7 +82,6 @@ sub Imgur { unless ($r->is_success) { cluck "Unable to refresh imgur token, please re-authorise app! or imgur experiencing api outage: $r->code $r->message"; - untie %secret; return undef; } @@ -89,12 +89,11 @@ sub Imgur { if (defined $refresh) { $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}; - $secret{imgur_access_token} = $refresh->{access_token}; + $cache->set ('imgur_access_token', $refresh->{access_token}, 'never'); } else { cluck "Unable to refresh imgur access_token, unable to decode json: $EVAL_ERROR"; - untie %secret; return undef; } diff --git a/lib/BotLib/Lat.pm b/lib/BotLib/Lat.pm index ab67d72..e1c2ce1 100644 --- a/lib/BotLib/Lat.pm +++ b/lib/BotLib/Lat.pm @@ -11,7 +11,7 @@ use Carp qw (cluck); use File::Basename qw (dirname); use File::Path qw (make_path); use Hailo; -use BotLib::Conf qw (loadConf); +use BotLib::Conf qw (LoadConf); use version; our $VERSION = qw (1.0); use Exporter qw (import); diff --git a/lib/BotLib/Weather.pm b/lib/BotLib/Weather.pm index 920a1bb..daadaf5 100644 --- a/lib/BotLib/Weather.pm +++ b/lib/BotLib/Weather.pm @@ -16,12 +16,12 @@ use BotLib::Util qw (trim urlencode); use version; our $VERSION = qw (1.0); use Exporter qw (import); -our @EXPORT_OK = qw (weather); +our @EXPORT_OK = qw (Weather); my $c = LoadConf (); my $cachedir = $c->{cachedir}; -sub weather { +sub Weather { my $city = shift; $city = trim $city; diff --git a/lib/TelegramBot.pm b/lib/TelegramBot.pm index 0c0c0ad..0fbbe87 100644 --- a/lib/TelegramBot.pm +++ b/lib/TelegramBot.pm @@ -13,7 +13,6 @@ use File::Path qw (make_path); use Hailo; use Math::Random::Secure qw (irand); use Mojo::Base 'Teapot::Bot::Brain'; - use BotLib::Admin qw (FortuneToggleList); use BotLib qw (RandomCommonPhrase Command Highlight BotSleep IsCensored); use BotLib::Conf qw (LoadConf); @@ -48,10 +47,10 @@ sub __cron { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime (time); if ($hour == 8 && ($min >= 0 && $min <= 14)) { - foreach my $enabledFortuneChat (FortuneToggleList ()) { + foreach my $enabledfortunechat (fortuneToggleList ()) { my $send_args; $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'; $self->sendMessage ($send_args); } @@ -66,7 +65,7 @@ sub __on_msg { my $chatid; my $chatname = 'Noname chat'; # 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}; unless ($myid) { @@ -150,11 +149,11 @@ sub __on_msg { # lazy init chat-bot brains 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 ( # we'll got file like this: data/telegrambot-brains/-1001332512695.brain.sqlite - brain => $brainName, + brain => $brainname, order => 3 ); @@ -168,7 +167,6 @@ sub __on_msg { # is this a 1-on-1 ? if ($msg->chat->type eq 'private') { unless (defined $msg->text) { - # TODO: it can be sticker, image, voice, video, etc. Maybe, i'll handle it once? :) return; } @@ -281,7 +279,7 @@ sub __on_msg { # bot mention by name } elsif ((lc ($text) =~ /.+ ${qname}[\,|\!|\?|\.| ]/) or (lc ($text) =~ / $qname$/)) { $phrase = $text; - $reply = $hailo->{$msg->chat->id}->reply ($phrase); + $reply = $hailo->{$msg->chat->id}->reply($phrase); # bot mention by telegram name } elsif ((lc ($text) =~ /.+ ${qtname}[\,|\!|\?|\.| ]/) or (lc ($text) =~ / $qtname$/)) { $phrase = $text; @@ -352,7 +350,7 @@ sub init { sub RunTelegramBot { while (sleep 3) { eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval) - telegrambot->new->think; + TelegramBot->new->think; } }