From 20bbfab333ad95525364e6f9166983d14f0a4202 Mon Sep 17 00:00:00 2001 From: eleksir Date: Sun, 6 Sep 2020 20:44:54 +0300 Subject: [PATCH] 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 --- bin/aleesa-telegram-bot | 30 ++++++++++++++++++++---------- data/sample_config.json | 3 ++- lib/botlib.pm | 17 +++++++++++++++-- lib/telegrambot.pm | 23 ++++++++++++++++++++++- 4 files changed, 59 insertions(+), 14 deletions(-) diff --git a/bin/aleesa-telegram-bot b/bin/aleesa-telegram-bot index a6f1de6..4609184 100755 --- a/bin/aleesa-telegram-bot +++ b/bin/aleesa-telegram-bot @@ -18,17 +18,17 @@ BEGIN { use lib ("$workdir/lib", "$workdir/vendor_perl", "$workdir/vendor_perl/lib/perl5"); use POSIX qw(setgid setuid setsid); -use Fcntl qw(O_WRONLY O_TRUNC O_CREAT); use File::Path qw(make_path); -use File::Basename qw(basename); -use telegrambot; -use conf; +use File::Basename qw(basename dirname); +use conf qw(loadConf); +use botlib qw(logger); +use telegrambot qw(run_telegrambot); sub daemonize (); sub __fork (); -sub __mkpidfile (); $| = 1; +my $runAsUid = $<; my $piddir = "/var/run/" . basename (abs_path ($0)); my $C = loadConf (); @@ -48,6 +48,17 @@ if ($< == 0) { ); } + if ($C->{'debug_log'}) { + unless (-d dirname($C->{'debug_log'})) { + make_path ( + dirname($C->{'debug_log'}), { + uid => $uid, + group => $gid + } + ); + } + } + # script intended to run as "unit" user, so drop privs to "unit" user setgid ($gid) or die "Unable to switch to group of $user($!)\n"; setuid ($uid) or die "Unable to switch to account $user($!)\n"; @@ -59,11 +70,12 @@ daemonize (); # write pidfile my $pidfile; -if ($< == 0) { +if ($runAsUid == 0) { # TODO: handle case where we run with unpriv. user $pidfile = sprintf ("%s/%s.pid", $piddir, basename (abs_path ($0))); - sysopen (PID, $pidfile, O_WRONLY|O_TRUNC|O_CREAT) or die "Error opening $pidfile: $!\n"; - syswrite PID, $$ or die "Error writing to $pidfile: $!\n"; + logger ("Creating pidfile at $pidfile"); + open (PID, ">", $pidfile) or die "Error opening $pidfile: $!\n"; + print PID $$ or die "Error writing to $pidfile: $!\n"; close PID; } @@ -72,7 +84,6 @@ $0 = abs_path ($0); # run bot, at last run_telegrambot (); - unlink ($pidfile) if (-f $pidfile); exit 0; @@ -92,6 +103,5 @@ sub __fork () { exit if ($pid != 0); } - __END__ # vim: set ft=perl noet ai ts=4 sw=4 sts=4: diff --git a/data/sample_config.json b/data/sample_config.json index e3e5347..ba19637 100644 --- a/data/sample_config.json +++ b/data/sample_config.json @@ -11,5 +11,6 @@ "cachetime": "data/cache/openweathermap.Id-Timestamp.db", "cachedata": "data/cache/openweathermap.Id-Data.db", "appid": "get_it_on_openweathermap.org" - } + }, + "debug_log" : "/var/log/aleesa-telegram-bot/debug.log" } diff --git a/lib/botlib.pm b/lib/botlib.pm index c8ca327..93398e3 100644 --- a/lib/botlib.pm +++ b/lib/botlib.pm @@ -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; diff --git a/lib/telegrambot.pm b/lib/telegrambot.pm index 26ee442..e692e4a 100644 --- a/lib/telegrambot.pm +++ b/lib/telegrambot.pm @@ -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 {