From 4bf814c473fbd98d6ff6b611e86a1b489e560b80 Mon Sep 17 00:00:00 2001 From: eleksir Date: Sun, 15 Nov 2020 20:44:35 +0300 Subject: [PATCH] =?UTF-8?q?Move=20chat=20commands=20to=20botlib.pm,=20impl?= =?UTF-8?q?ement=20!version,=20!ver,=20!=D0=B2=D0=B5=D1=80=D1=81=D0=B8?= =?UTF-8?q?=D1=8F=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cpanfile | 2 -- lib/botlib.pm | 38 +++++++++++++++++++- lib/karma.pm | 6 ++-- lib/lat.pm | 7 ++-- lib/telegrambot.pm | 86 ++++++++++++++++++++-------------------------- 5 files changed, 81 insertions(+), 58 deletions(-) diff --git a/cpanfile b/cpanfile index a6a82a8..9f29a7d 100644 --- a/cpanfile +++ b/cpanfile @@ -18,6 +18,4 @@ requires 'Log::Any'; requires 'Mojo::Base'; requires 'Mojo::IOLoop'; requires 'Mojo::UserAgent'; -#requires 'Telegram::Bot'; -#requires 'Test::Exception'; requires 'URI::URL'; diff --git a/lib/botlib.pm b/lib/botlib.pm index 0d32824..38d4eca 100644 --- a/lib/botlib.pm +++ b/lib/botlib.pm @@ -15,11 +15,13 @@ use Digest::MD5 qw(md5_base64); use DB_File; use conf qw(loadConf); +use lat qw(latAnswer); +use karma qw(karmaSet karmaGet); use vars qw/$VERSION/; use Exporter qw(import); -our @EXPORT_OK = qw(weather logger trim randomCommonPhrase); +our @EXPORT_OK = qw(weather logger trim randomCommonPhrase command); $VERSION = '1.0'; @@ -243,6 +245,40 @@ sub randomCommonPhrase { return $myphrase[rand ($#myphrase -1)]; } +sub command { + my $text = shift; + my $chatid = shift; + my $reply; + + if (substr ($text, 1) eq 'ping') { + $reply = 'Pong.'; + } elsif (substr ($text, 1) eq 'пинг') { + $reply = 'Понг.'; + } elsif (substr ($text, 1) eq 'pong') { + $reply = 'Wat?'; + } elsif (substr ($text, 1) eq 'понг') { + $reply = 'Шта?'; + } elsif (substr ($text, 1) eq 'ver' || substr ($text, 1) eq 'version' || substr ($text, 1) eq 'версия') { + $reply = 'Версия: Нуль.Чего-то_там.Чего-то_там'; + } elsif (substr ($text, 1, 2) eq 'w ' || substr ($text, 1, 2) eq 'п ') { + my $city = substr ($text, 3); + $reply = weather ($city); + } 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); + + if ($mytext ne '') { + chomp ($mytext); + $mytext = trim ($mytext); + } + + $reply = karmaGet ($chatid, $mytext); + } + + return $reply; +} + 1; # vim: set ft=perl noet ai ts=4 sw=4 sts=4: diff --git a/lib/karma.pm b/lib/karma.pm index e446679..37d3ebe 100644 --- a/lib/karma.pm +++ b/lib/karma.pm @@ -8,6 +8,7 @@ use open qw(:std :utf8); use English qw( -no_match_vars ); use Digest::SHA qw(sha1_base64); use DB_File; +use Carp qw(cluck); use vars qw/$VERSION/; use Exporter qw(import); @@ -16,7 +17,6 @@ our @EXPORT_OK = qw(karmaSet karmaGet); $VERSION = '1.0'; use conf qw(loadConf); -use botlib qw(logger); my $c = loadConf(); my $karmadir = $c->{karma}->{dir}; @@ -41,7 +41,7 @@ sub karmaSet (@) { # init hash, store phrase and score tie my %karma, 'DB_File', $karmafile || do { - logger "Something nasty happen when cachedata ties to its data: $OS_ERROR"; + cluck "Something nasty happen when cachedata ties to its data: $OS_ERROR"; return sprintf 'Карма %s составляет 0', $phrase; }; @@ -100,7 +100,7 @@ sub karmaGet (@) { # init hash, store phrase and score tie my %karma, 'DB_File', $karmafile || do { - logger "Something nasty happen when karma ties to its data: $OS_ERROR"; + cluck "Something nasty happen when karma ties to its data: $OS_ERROR"; return sprintf 'Карма %s составляет 0', $phrase; }; diff --git a/lib/lat.pm b/lib/lat.pm index 48cec25..8b95916 100644 --- a/lib/lat.pm +++ b/lib/lat.pm @@ -7,6 +7,7 @@ use warnings; use utf8; use open qw(:std :utf8); use English qw( -no_match_vars ); +use Carp qw(cluck); use vars qw/$VERSION/; use Exporter qw(import); @@ -29,7 +30,7 @@ sub train () { unless (-d $braindir) { mkpath ($braindir) or do { - logger "Unable to create $braindir: $OS_ERROR"; + cluck "Unable to create $braindir: $OS_ERROR"; exit 1; }; } @@ -52,12 +53,12 @@ sub latAnswer () { my $braindir = dirname ($brain); unless (-d $braindir) { - logger ("No lat module data: $braindir is absent! Train lat first."); + cluck "No lat module data: $braindir is absent! Train lat first."; return ''; } unless (-f $brain) { - logger ("No lat module data: $brain is absent! Train lat first."); + cluck "No lat module data: $brain is absent! Train lat first."; return ''; } diff --git a/lib/telegrambot.pm b/lib/telegrambot.pm index 61309a8..7ba25b9 100644 --- a/lib/telegrambot.pm +++ b/lib/telegrambot.pm @@ -8,15 +8,13 @@ use vars qw/$VERSION/; use utf8; use Data::Dumper; use open qw(:std :utf8); -use File::Path qw( mkpath ); +use File::Path qw(mkpath); use Hailo; use Mojo::Base 'Teapot::Bot::Brain'; use conf qw(loadConf); -use botlib qw(weather logger trim randomCommonPhrase); +use botlib qw(weather logger trim randomCommonPhrase command); use telegramlib qw(visavi); -use lat qw(latAnswer); -use karma qw(karmaSet karmaGet); use Exporter qw(import); our @EXPORT_OK = qw(run_telegrambot); @@ -220,19 +218,27 @@ sub __on_msg { my $csign = quotemeta ($c->{telegrambot}->{csign}); my $reply = 'Давайте ещё пообщаемся, а то я ещё не научилась от вас плохому.'; - # TODO: Process commands in separate sub, they are same for public and private chats. if (substr ($text, 0, 1) eq $c->{telegrambot}->{csign}) { - if (substr ($text, 1) eq 'ping') { - $reply = 'Pong.'; - } elsif (substr ($text, 1) eq 'пинг') { - $reply = 'Понг.'; - } elsif (substr ($text, 1, 2) eq 'w ' || substr ($text, 1, 2) eq 'п ') { - my $city = substr ($text, 2); - $reply = weather ($city); - } elsif ((length ($text) == 4) && (substr ($text, 1, 3) eq 'lat' || substr ($text, 1, 2) eq 'лат')) { - $reply = latAnswer(); - } else { - $reply = 'Чего?'; + if (substr ($text, 1) eq 'help') { + my $send_args; + $send_args->{text} = << 'MYHELP'; +``` +!help | !помощь - список команд +!w город | !п город - погода в указанном городе +!ping | !пинг - попинговать бота +!lat | !лат - сгенерировать фразу из крылатых латинских выражений +!karma | !карма фраза - посмотреть карму фразы +фраза-- | фраза++ - убавить или добавить карму фразе +``` +Но на самом деле я бот больше для общения, чем для исполнения команд. +Поговоришь со мной? +MYHELP + $send_args->{parse_mode} = 'Markdown'; + $send_args->{chat_id} = $userid; + Teapot::Bot::Brain::sendMessage ($self, $send_args); + return; + } esle { + $reply = command($text, $userid); } } else { my $str = $hailo->{$msg->chat->id}->learn_reply ($text); @@ -259,10 +265,12 @@ sub __on_msg { } } - $msg->typing(); - sleep 1; - logger sprintf ("Private chat bot reply to $vis_a_vi: %s", $reply); - $msg->reply ($reply); + if (defined $reply) { + $msg->typing(); + sleep 1; + logger sprintf ("Private chat bot reply to $vis_a_vi: %s", $reply); + $msg->reply ($reply); + } # group chat } elsif (($msg->chat->type eq 'supergroup') or ($msg->chat->type eq 'group')) { my $reply; @@ -320,23 +328,19 @@ sub __on_msg { # figure out reply :) $reply = $hailo->{$msg->chat->id}->learn_reply ($phrase); # simple commands - # TODO: Process commands in separate sub, they are same for public and private chats. # TODO: Log commands and answers } elsif (substr ($text, 0, 1) eq $c->{telegrambot}->{csign}) { if (substr ($text, 1) eq 'help') { - unless ($can_talk) { - return ; - } - my $send_args; $send_args->{text} = << 'MYHELP'; ``` -!help | !помощь - список команд -!w город | !п город - погода в указанном городе -!ping | !пинг - попинговать бота -!lat | !лат - сгенерировать фразу из крылатых латинских выражений -!karma | !карма фраза - посмотреть карму фразы -фраза-- | фраза++ - убавить или добавить карму фразе +!help | !помощь - список команд +!ver | !version | !версия - что-то про версию ПО +!w город | !п город - погода в указанном городе +!ping | !пинг - попинговать бота +!lat | !лат - сгенерировать фразу из крылатых латинских выражений +!karma | !карма фраза - посмотреть карму фразы +фраза-- | фраза++ - убавить или добавить карму фразе ``` Но на самом деле я бот больше для общения, чем для исполнения команд. Поговоришь со мной? @@ -345,24 +349,8 @@ MYHELP $send_args->{chat_id} = $chatid; Teapot::Bot::Brain::sendMessage ($self, $send_args); return; - } elsif (substr ($text, 1) eq 'ping') { - $reply = 'Pong.'; - } elsif (substr ($text, 1) eq 'пинг') { - $reply = 'Понг.'; - } elsif (substr ($text, 1) eq 'pong') { - $reply = 'Wat?'; - } elsif (substr ($text, 1) eq 'понг') { - $reply = 'Шта?'; - } elsif (substr ($text, 1, 2) eq 'w ' || substr ($text, 1, 2) eq 'п ') { - my $city = substr ($text, 3); - $reply = weather ($city); - } elsif (length ($text) == 4 && (substr ($text, 1, 3) eq 'lat' || substr ($text, 1, 3) eq 'лат')) { - $reply = latAnswer(); - } elsif ((length ($text) >= 7) && ((substr ($text, 1, 6) eq 'karma ') || (substr ($text, 1, 6) eq 'карма '))) { - my $mytext = substr ($text, 7); - chomp ($mytext); - $mytext = trim ($mytext); - $reply = karmaGet ($chatid, $mytext); + } else { + $reply = command ($text, $chatid); } } elsif ( ($text eq $qname) or