Move chat commands to botlib.pm, implement !version, !ver, !версия command
This commit is contained in:
2
cpanfile
2
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';
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
@@ -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 '';
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user