Add help, debug log, fix pid file creation

+ Add help in group chat
+ Add debug log ability
* A bit of cleanup
* Check against start uid rather than effective uid when creating pid-file
This commit is contained in:
eleksir
2020-09-06 20:44:54 +03:00
parent f7ecdb870f
commit 20bbfab333
4 changed files with 59 additions and 14 deletions

View File

@@ -11,12 +11,12 @@ use JSON::XS qw(decode_json encode_json);
use Digest::MD5 qw(md5_base64);
use DB_File;
use conf;
use conf qw(loadConf);
use vars qw/$VERSION/;
use Exporter qw(import);
our @EXPORT = qw(weather);
our @EXPORT = qw(weather logger);
$VERSION = "1.0";
@@ -188,6 +188,19 @@ sub __urlencode($) {
return $str;
}
sub logger {
my $msg = shift;
if ($c->{debug_log}) {
my $mode = '>';
$mode = '>>' if (-f $c->{debug_log});
if (open (LOG, $mode, $c->{debug_log})) {
print LOG $msg . "\n";
close LOG;
}
}
}
1;

View File

@@ -5,6 +5,7 @@ use warnings "all";
use vars qw/$VERSION/;
use v5.10.0;
use utf8;
use Data::Dumper;
use open qw(:std :utf8);
use File::Path qw( mkpath );
use Hailo;
@@ -32,6 +33,13 @@ sub __cron {
sub __on_msg {
my ($self, $msg) = @_;
if ($msg->{'new_chat_members'}) {
# we have newcommers here
$msg->reply("Дратути. Представьтес, пожалуйста, и расскажите, что вас сюда привело.");
return;
}
# lazy init chat-bot brains
unless (defined($hailo->{$msg->chat->id})) {
$hailo->{$msg->chat->id} = Hailo->new(
@@ -65,6 +73,7 @@ sub __on_msg {
if (defined($reply) && $reply ne '') {
$msg->reply($reply);
logger ("private chat reply: $reply");
} else {
# if we have no answer, say something default in private chat
$msg->reply("Давайте ещё пообщаемся, а то я ещё не научилась от вас плохому.");
@@ -83,7 +92,16 @@ sub __on_msg {
# simple commands
if (substr($text, 0, 1) eq $c->{telegrambot}->{csign}) {
if (substr($text, 1) eq "ping") {
if (substr($text, 1) eq "help") {
$reply = '```
!help | !помощь - список команд
!w город | !п город - погода в указанном городе
!ping | !пинг - попинговать бота
```
Но на самом деле я бот больше для общения, чем для исполнения команд.
Поговоришь со мной?
';
} elsif (substr($text, 1) eq "ping") {
$reply = "Pong.";
} elsif (substr($text, 1) eq "пинг") {
$reply = "Понг.";
@@ -119,7 +137,10 @@ sub __on_msg {
}
if (defined($reply) && $reply ne '') {
logger ("reply with: $reply");
$msg->reply($reply);
} else {
logger ("no reply");
}
# should be channel, so we can't talk
} else {