1

Move chat commands to botlib.pm, implement !version, !ver, !версия command

This commit is contained in:
eleksir
2020-11-15 20:44:35 +03:00
parent ba56ea72a2
commit 4bf814c473
5 changed files with 81 additions and 58 deletions

View File

@@ -18,6 +18,4 @@ requires 'Log::Any';
requires 'Mojo::Base'; requires 'Mojo::Base';
requires 'Mojo::IOLoop'; requires 'Mojo::IOLoop';
requires 'Mojo::UserAgent'; requires 'Mojo::UserAgent';
#requires 'Telegram::Bot';
#requires 'Test::Exception';
requires 'URI::URL'; requires 'URI::URL';

View File

@@ -15,11 +15,13 @@ use Digest::MD5 qw(md5_base64);
use DB_File; use DB_File;
use conf qw(loadConf); use conf qw(loadConf);
use lat qw(latAnswer);
use karma qw(karmaSet karmaGet);
use vars qw/$VERSION/; use vars qw/$VERSION/;
use Exporter qw(import); use Exporter qw(import);
our @EXPORT_OK = qw(weather logger trim randomCommonPhrase); our @EXPORT_OK = qw(weather logger trim randomCommonPhrase command);
$VERSION = '1.0'; $VERSION = '1.0';
@@ -243,6 +245,40 @@ sub randomCommonPhrase {
return $myphrase[rand ($#myphrase -1)]; 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; 1;
# vim: set ft=perl noet ai ts=4 sw=4 sts=4: # vim: set ft=perl noet ai ts=4 sw=4 sts=4:

View File

@@ -8,6 +8,7 @@ use open qw(:std :utf8);
use English qw( -no_match_vars ); use English qw( -no_match_vars );
use Digest::SHA qw(sha1_base64); use Digest::SHA qw(sha1_base64);
use DB_File; use DB_File;
use Carp qw(cluck);
use vars qw/$VERSION/; use vars qw/$VERSION/;
use Exporter qw(import); use Exporter qw(import);
@@ -16,7 +17,6 @@ our @EXPORT_OK = qw(karmaSet karmaGet);
$VERSION = '1.0'; $VERSION = '1.0';
use conf qw(loadConf); use conf qw(loadConf);
use botlib qw(logger);
my $c = loadConf(); my $c = loadConf();
my $karmadir = $c->{karma}->{dir}; my $karmadir = $c->{karma}->{dir};
@@ -41,7 +41,7 @@ sub karmaSet (@) {
# init hash, store phrase and score # init hash, store phrase and score
tie my %karma, 'DB_File', $karmafile || do { 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; return sprintf 'Карма %s составляет 0', $phrase;
}; };
@@ -100,7 +100,7 @@ sub karmaGet (@) {
# init hash, store phrase and score # init hash, store phrase and score
tie my %karma, 'DB_File', $karmafile || do { 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; return sprintf 'Карма %s составляет 0', $phrase;
}; };

View File

@@ -7,6 +7,7 @@ use warnings;
use utf8; use utf8;
use open qw(:std :utf8); use open qw(:std :utf8);
use English qw( -no_match_vars ); use English qw( -no_match_vars );
use Carp qw(cluck);
use vars qw/$VERSION/; use vars qw/$VERSION/;
use Exporter qw(import); use Exporter qw(import);
@@ -29,7 +30,7 @@ sub train () {
unless (-d $braindir) { unless (-d $braindir) {
mkpath ($braindir) or do { mkpath ($braindir) or do {
logger "Unable to create $braindir: $OS_ERROR"; cluck "Unable to create $braindir: $OS_ERROR";
exit 1; exit 1;
}; };
} }
@@ -52,12 +53,12 @@ sub latAnswer () {
my $braindir = dirname ($brain); my $braindir = dirname ($brain);
unless (-d $braindir) { 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 ''; return '';
} }
unless (-f $brain) { 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 ''; return '';
} }

View File

@@ -8,15 +8,13 @@ use vars qw/$VERSION/;
use utf8; use utf8;
use Data::Dumper; use Data::Dumper;
use open qw(:std :utf8); use open qw(:std :utf8);
use File::Path qw( mkpath ); use File::Path qw(mkpath);
use Hailo; use Hailo;
use Mojo::Base 'Teapot::Bot::Brain'; use Mojo::Base 'Teapot::Bot::Brain';
use conf qw(loadConf); use conf qw(loadConf);
use botlib qw(weather logger trim randomCommonPhrase); use botlib qw(weather logger trim randomCommonPhrase command);
use telegramlib qw(visavi); use telegramlib qw(visavi);
use lat qw(latAnswer);
use karma qw(karmaSet karmaGet);
use Exporter qw(import); use Exporter qw(import);
our @EXPORT_OK = qw(run_telegrambot); our @EXPORT_OK = qw(run_telegrambot);
@@ -220,19 +218,27 @@ sub __on_msg {
my $csign = quotemeta ($c->{telegrambot}->{csign}); my $csign = quotemeta ($c->{telegrambot}->{csign});
my $reply = 'Давайте ещё пообщаемся, а то я ещё не научилась от вас плохому.'; 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, 0, 1) eq $c->{telegrambot}->{csign}) {
if (substr ($text, 1) eq 'ping') { if (substr ($text, 1) eq 'help') {
$reply = 'Pong.'; my $send_args;
} elsif (substr ($text, 1) eq 'пинг') { $send_args->{text} = << 'MYHELP';
$reply = 'Понг.'; ```
} elsif (substr ($text, 1, 2) eq 'w ' || substr ($text, 1, 2) eq 'п ') { !help | !помощь - список команд
my $city = substr ($text, 2); !w город | !п город - погода в указанном городе
$reply = weather ($city); !ping | !пинг - попинговать бота
} elsif ((length ($text) == 4) && (substr ($text, 1, 3) eq 'lat' || substr ($text, 1, 2) eq 'лат')) { !lat | !лат - сгенерировать фразу из крылатых латинских выражений
$reply = latAnswer(); !karma | !карма фраза - посмотреть карму фразы
} else { фраза-- | фраза++ - убавить или добавить карму фразе
$reply = 'Чего?'; ```
Но на самом деле я бот больше для общения, чем для исполнения команд.
Поговоришь со мной?
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 { } else {
my $str = $hailo->{$msg->chat->id}->learn_reply ($text); my $str = $hailo->{$msg->chat->id}->learn_reply ($text);
@@ -259,10 +265,12 @@ sub __on_msg {
} }
} }
$msg->typing(); if (defined $reply) {
sleep 1; $msg->typing();
logger sprintf ("Private chat bot reply to $vis_a_vi: %s", $reply); sleep 1;
$msg->reply ($reply); logger sprintf ("Private chat bot reply to $vis_a_vi: %s", $reply);
$msg->reply ($reply);
}
# group chat # group chat
} elsif (($msg->chat->type eq 'supergroup') or ($msg->chat->type eq 'group')) { } elsif (($msg->chat->type eq 'supergroup') or ($msg->chat->type eq 'group')) {
my $reply; my $reply;
@@ -320,23 +328,19 @@ sub __on_msg {
# figure out reply :) # figure out reply :)
$reply = $hailo->{$msg->chat->id}->learn_reply ($phrase); $reply = $hailo->{$msg->chat->id}->learn_reply ($phrase);
# simple commands # simple commands
# TODO: Process commands in separate sub, they are same for public and private chats.
# TODO: Log commands and answers # TODO: Log commands and answers
} elsif (substr ($text, 0, 1) eq $c->{telegrambot}->{csign}) { } elsif (substr ($text, 0, 1) eq $c->{telegrambot}->{csign}) {
if (substr ($text, 1) eq 'help') { if (substr ($text, 1) eq 'help') {
unless ($can_talk) {
return ;
}
my $send_args; my $send_args;
$send_args->{text} = << 'MYHELP'; $send_args->{text} = << 'MYHELP';
``` ```
!help | !помощь - список команд !help | !помощь - список команд
!w город | !п город - погода в указанном городе !ver | !version | !версия - что-то про версию ПО
!ping | !пинг - попинговать бота !w город | !п город - погода в указанном городе
!lat | !лат - сгенерировать фразу из крылатых латинских выражений !ping | !пинг - попинговать бота
!karma | !карма фраза - посмотреть карму фразы !lat | !лат - сгенерировать фразу из крылатых латинских выражений
фраза-- | фраза++ - убавить или добавить карму фразе !karma | !карма фраза - посмотреть карму фразы
фраза-- | фраза++ - убавить или добавить карму фразе
``` ```
Но на самом деле я бот больше для общения, чем для исполнения команд. Но на самом деле я бот больше для общения, чем для исполнения команд.
Поговоришь со мной? Поговоришь со мной?
@@ -345,24 +349,8 @@ MYHELP
$send_args->{chat_id} = $chatid; $send_args->{chat_id} = $chatid;
Teapot::Bot::Brain::sendMessage ($self, $send_args); Teapot::Bot::Brain::sendMessage ($self, $send_args);
return; return;
} elsif (substr ($text, 1) eq 'ping') { } else {
$reply = 'Pong.'; $reply = command ($text, $chatid);
} 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);
} }
} elsif ( } elsif (
($text eq $qname) or ($text eq $qname) or