From da7d61248d6f9863164b977696ffad81961dfabe Mon Sep 17 00:00:00 2001 From: eleksir Date: Mon, 8 Mar 2021 17:25:21 +0300 Subject: [PATCH] =?UTF-8?q?Implement=20!fox=20!=D0=BB=D0=B8=D1=81=20comman?= =?UTF-8?q?ds?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/botlib.pm | 4 ++++ lib/fox.pm | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 lib/fox.pm diff --git a/lib/botlib.pm b/lib/botlib.pm index 3276e23..5017392 100644 --- a/lib/botlib.pm +++ b/lib/botlib.pm @@ -12,6 +12,7 @@ use archeologist qw (dig); use conf qw (loadConf); use fisher qw (fish); use fortune qw (fortune); +use fox qw (fox); use friday qw (friday); use karma qw (karmaGet); use kitty qw (kitty); @@ -185,6 +186,7 @@ ${csign}fish | ${csign}fisher - порыбачить ${csign}рыба | ${csign}рыбка | ${csign}рыбалка - заняться археологией ${csign}f | ${csign}ф - рандомная фраза из сборника цитат fortune_mod ${csign}fortune | ${csign}фортунка - рандомная фраза из сборника цитат fortune_mod +${csign}fox | ${csign}лис - лисичка ${csign}friday | ${csign}пятница - а не пятница ли сегодня? ${csign}karma фраза - посмотреть карму фразы ${csign}карма фраза - посмотреть карму фразы @@ -201,6 +203,8 @@ EOL $reply = latAnswer (); } elsif (substr ($text, 1) eq 'cat' || substr ($text, 1) eq 'кис') { $reply = kitty (); + } elsif (substr ($text, 1) eq 'fox' || substr ($text, 1) eq 'лис') { + $reply = fox (); } elsif (substr ($text, 1) eq 'dig' || substr ($text, 1) eq 'копать') { $reply = dig ($chattername); } elsif (substr ($text, 1) eq 'fish' || substr ($text, 1) eq 'fisher' || substr ($text, 1) eq 'рыба' || substr ($text, 1) eq 'рыбка' || substr ($text, 1) eq 'рыбалка') { diff --git a/lib/fox.pm b/lib/fox.pm new file mode 100644 index 0000000..1aff987 --- /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 (carp); +use JSON::XS; +use HTTP::Tiny; + +use vars qw/$VERSION/; +use Exporter qw (import); +our @EXPORT_OK = qw (fox); +$VERSION = '1.0'; + +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}) + }; + + unless (defined $jfox) { + cluck "[ERROR] Unable to decode JSON: $EVAL_ERROR"; + } else { + if ($jfox->{image}) { + $jfox->{image} =~ s/\\//xmsg; + $ret = $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: