From c3d2d76ad4408546fca035d9ad57c179822e2c63 Mon Sep 17 00:00:00 2001 From: eleksir Date: Wed, 9 Sep 2020 14:54:38 +0300 Subject: [PATCH] Add trim() and random pre-generated phrase answer + Add php-inspired trim() function + Add randomCommonPhrase() in order not to repeat given sentense if unable to produce relevant phrase from vocabulary generator --- lib/botlib.pm | 28 +++++++++++++++++++++++++++- lib/telegrambot.pm | 9 +++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/botlib.pm b/lib/botlib.pm index 93398e3..9690868 100644 --- a/lib/botlib.pm +++ b/lib/botlib.pm @@ -16,7 +16,7 @@ use conf qw(loadConf); use vars qw/$VERSION/; use Exporter qw(import); -our @EXPORT = qw(weather logger); +our @EXPORT = qw(weather logger trim randomCommonPhrase); $VERSION = "1.0"; @@ -202,6 +202,32 @@ sub logger { } } +sub trim($) { + my $str = shift; + + while (substr ($str, 0, 1) =~ /^\s$/) { + $str = substr($str, 1); + } + + while (substr ($str, -1, 1) =~ /^\s$/) { + chop ($str); + } + + return $str; +} + +sub randomCommonPhrase() { + my @myphrase = ( + "Так, блядь...", + "*Закатывает рукава* И ради этого ты меня позвал?", + "Ну чего ты начинаешь, нормально же общались", + "Повтори свой вопрос, не поняла", + "Выйди и зайди нормально" + ); + + return ($myphrase[rand($#myphrase -1)]); +} + 1; # vim: set ft=perl noet ai ts=4 sw=4 sts=4: diff --git a/lib/telegrambot.pm b/lib/telegrambot.pm index e692e4a..e682ff5 100644 --- a/lib/telegrambot.pm +++ b/lib/telegrambot.pm @@ -137,6 +137,15 @@ sub __on_msg { } if (defined($reply) && $reply ne '') { + if (lc($reply) eq lc(trim($text))) { + $reply = randomCommonPhrase(); + } + + # in case of trailing dot + if (lc($reply) eq substr(lc(trim($text)), 0, -1)) { + $reply = randomCommonPhrase(); + } + logger ("reply with: $reply"); $msg->reply($reply); } else {