diff --git a/lib/botlib.pm b/lib/botlib.pm index 483f2ba..6ac57a5 100644 --- a/lib/botlib.pm +++ b/lib/botlib.pm @@ -15,6 +15,7 @@ use admin qw (@forbiddenMessageTypes getForbiddenTypes addForbiddenType delForbi use archeologist qw (dig); use fisher qw (fish); use fortune qw (fortune); +use fox qw (fox); use friday qw (friday); use karma qw (karmaSet karmaGet); use kitty qw (kitty); @@ -88,6 +89,12 @@ sub command { } $reply = karmaGet ($chatid, $mytext); + } elsif (substr ($text, 1) eq 'fox' || substr ($text, 1) eq 'лис') { + $reply = fox (); + $msg->typing (); + sleep (irand (2) + 1); + $msg->replyMd ($reply); + return; } elsif (substr ($text, 1) eq 'friday' || substr ($text, 1) eq 'пятница') { $reply = friday (); } elsif (substr ($text, 1) eq 'fortune' || substr ($text, 1) eq 'фортунка' || substr ($text, 1) eq 'f' || substr ($text, 1) eq 'ф') { @@ -116,6 +123,7 @@ ${csign}fish | ${csign}рыба | ${csign}рыбка - порыбачить ${csign}fishing | ${csign}рыбалка - порыбачить ${csign}f | ${csign}ф - рандомная фраза из сборника цитат fortune_mod ${csign}fortune | ${csign}фортунка - рандомная фраза из сборника цитат fortune_mod +${csign}fox | ${csign}лис - лисичка ${csign}friday | ${csign}пятница - а не пятница ли сегодня? ${csign}lat | ${csign}лат - сгенерить фразу из крылатых латинских выражений ${csign}ping | ${csign}пинг - попинговать бота diff --git a/lib/fox.pm b/lib/fox.pm new file mode 100644 index 0000000..d722bb0 --- /dev/null +++ b/lib/fox.pm @@ -0,0 +1,55 @@ +package fox; + +use 5.018; +use strict; +use warnings; +use utf8; +use open qw (:std :utf8); +use English qw ( -no_match_vars ); +use Carp qw (cluck); +use JSON::XS; +use HTTP::Tiny; +use Math::Random::Secure qw (irand); + +use version; our $VERSION = qw (1.0); +use Exporter qw (import); +our @EXPORT_OK = qw (fox); + +sub fox { + my $r; + my $ret = 'Нету лисичек, все разбежались.'; + + for (1..3) { + my $http = HTTP::Tiny->new (timeout => 3); + $r = $http->get ('https://randomfox.ca/floof/'); + + if ($r->{success}) { + last; + } + + sleep 2; + } + + if ($r->{success}) { + my $jfox = eval { + decode_json ($r->{content}) + }; + + if ($EVAL_ERROR) { + cluck "[ERROR] Unable to decode JSON: $EVAL_ERROR"; + } else { + if ($jfox->{image}) { + $jfox->{image} =~ s/\\//xmsg; + $ret = sprintf '[-^^,--,~](%s)', $jfox->{image}; + } + } + } else { + cluck sprintf 'Server return status %s with message: %s', $r->{status}, $r->{reason}; + } + + return $ret; +} + +1; + +# vim: set ft=perl noet ai ts=4 sw=4 sts=4: