From ac8c0f17dee634d86891d06f81a717c81329591e Mon Sep 17 00:00:00 2001 From: eleksir Date: Tue, 17 Nov 2020 01:51:10 +0300 Subject: [PATCH] =?UTF-8?q?Fix=20karma,=20fix=20private=20commands,=20actu?= =?UTF-8?q?ally=20add=20!=D0=BF=D0=BE=D0=BC=D0=BE=D1=89=D1=8C=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix zero length !karma parsing * In private chat commands not working except !help * Miss to add !помощь to parser, so it was noop --- lib/botlib.pm | 7 ++++-- lib/telegrambot.pm | 53 +++++++++++++++++++++++++++++----------------- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/lib/botlib.pm b/lib/botlib.pm index 38d4eca..da0648a 100644 --- a/lib/botlib.pm +++ b/lib/botlib.pm @@ -266,11 +266,14 @@ sub command { } elsif (substr ($text, 1) eq 'lat' || substr ($text, 1) eq 'лат') { $reply = latAnswer(); } elsif (substr ($text, 1, 6) eq 'karma ' || substr ($text, 1, 6) eq 'карма ' || substr($text, 1) eq 'karma' || substr($text, $1) eq 'карма') { - my $mytext = substr ($text, 7); + my $mytext = ''; - if ($mytext ne '') { + if (length($text) >= 7) { + $mytext = substr ($text, 7); chomp ($mytext); $mytext = trim ($mytext); + } else { + $mytext = ''; } $reply = karmaGet ($chatid, $mytext); diff --git a/lib/telegrambot.pm b/lib/telegrambot.pm index 7ba25b9..d2454ca 100644 --- a/lib/telegrambot.pm +++ b/lib/telegrambot.pm @@ -15,6 +15,7 @@ use Mojo::Base 'Teapot::Bot::Brain'; use conf qw(loadConf); use botlib qw(weather logger trim randomCommonPhrase command); use telegramlib qw(visavi); +use karma qw(karmaSet); use Exporter qw(import); our @EXPORT_OK = qw(run_telegrambot); @@ -219,7 +220,7 @@ sub __on_msg { my $reply = 'Давайте ещё пообщаемся, а то я ещё не научилась от вас плохому.'; if (substr ($text, 0, 1) eq $c->{telegrambot}->{csign}) { - if (substr ($text, 1) eq 'help') { + if (substr ($text, 1) eq 'help' || substr ($text, 1) eq 'помощь') { my $send_args; $send_args->{text} = << 'MYHELP'; ``` @@ -237,30 +238,44 @@ MYHELP $send_args->{chat_id} = $userid; Teapot::Bot::Brain::sendMessage ($self, $send_args); return; - } esle { + } else { $reply = command($text, $userid); } } else { - my $str = $hailo->{$msg->chat->id}->learn_reply ($text); + my $just_message_in_chat = 0; - if (defined ($str) && $str ne '') { - $reply = $str; - # TODO: move it to telegramlib.pm/botlib.pm! - $phrase = trim ($text); + if (substr ($text, -2) eq '++' || substr ($text, -2) eq '--') { + my @arr = split(/\n/, $text); - while ($phrase =~ /[\.|\,|\?|\!]$/) { - chop $phrase; + if ($#arr < 1) { + $reply = karmaSet ($chatid, substr ($text, 0, -2), substr ($text, -2)); + } else { + $just_message_in_chat = 1; } + } - $phrase = lc ($phrase); + if ($just_message_in_chat) { + my $str = $hailo->{$msg->chat->id}->learn_reply ($text); - if (lc ($reply) eq $phrase) { - $reply = randomCommonPhrase(); - } elsif (lc ($reply) eq substr ($phrase, 0, -1)) { - # in case of trailing dot - $reply = randomCommonPhrase(); - } elsif (substr (lc ($reply), 0, -1) eq $phrase) { - $reply = randomCommonPhrase(); + if (defined ($str) && $str ne '') { + $reply = $str; + # TODO: move it to telegramlib.pm/botlib.pm! + $phrase = trim ($text); + + while ($phrase =~ /[\.|\,|\?|\!]$/) { + chop $phrase; + } + + $phrase = lc ($phrase); + + if (lc ($reply) eq $phrase) { + $reply = randomCommonPhrase(); + } elsif (lc ($reply) eq substr ($phrase, 0, -1)) { + # in case of trailing dot + $reply = randomCommonPhrase(); + } elsif (substr (lc ($reply), 0, -1) eq $phrase) { + $reply = randomCommonPhrase(); + } } } } @@ -330,7 +345,7 @@ MYHELP # simple commands # TODO: Log commands and answers } elsif (substr ($text, 0, 1) eq $c->{telegrambot}->{csign}) { - if (substr ($text, 1) eq 'help') { + if (substr ($text, 1) eq 'help' || substr ($text, 1) eq 'помощь') { my $send_args; $send_args->{text} = << 'MYHELP'; ``` @@ -372,7 +387,7 @@ MYHELP } elsif ((lc ($text) =~ /.+ ${qtname}[\,|\!|\?|\.| ]/) or (lc ($text) =~ / $qtname$/)) { $phrase = $text; $reply = $hailo->{$msg->chat->id}->reply ($phrase); - } elsif ((substr ($text, -2) eq '++') || (substr ($text, -2) eq '--')) { + } elsif (substr ($text, -2) eq '++' || substr ($text, -2) eq '--') { my @arr = split(/\n/, $text); if ($#arr < 1) {