New logging, handle bot quote in groups, newcommer greets reworked

+ More sane and verbose logging
* Newcommer greet now should now mention newcommer
+ If someone in group quote bot's phrase, bot should answer
* Move some subs related to framework to telegramlib.pm
* Handle (log only for now) sent media - documents, stickers, photos in public chats (voice, audio, video still unhandled)
This commit is contained in:
eleksir
2020-09-26 21:22:39 +03:00
parent 376cbdfbe5
commit 1567fbe0db
3 changed files with 320 additions and 167 deletions

View File

@@ -23,15 +23,15 @@ $VERSION = "1.0";
my $c = loadConf();
sub weather($) {
sub weather ($) {
my $city = shift;
$city = trim($city);
$city = trim ($city);
return "Мне нужно ИМЯ города." if ($city eq '');
return "Длинновато для названия города." if (length($city) > 80);
$city = ucfirst($city);
my $w = __weather($city);
$city = ucfirst ($city);
my $w = __weather ($city);
my $reply;
if ($w) {
@@ -39,7 +39,7 @@ sub weather($) {
$reply = sprintf ("Погода в городе %s, %s:\n%s, ветер %s %s м/c, температура %s°C, ощущается как %s°C, относительная влажность %s%%, давление %s мм.рт.ст",
$w->{name},
$w->{country},
ucfirst($w->{description}),
ucfirst ($w->{description}),
$w->{wind_direction},
$w->{wind_speed},
$w->{temperature_min},
@@ -51,7 +51,7 @@ sub weather($) {
$reply = sprintf ("Погода в городе %s, %s:\n%s, ветер %s %s м/c, температура %s-%s°C, ощущается как %s°C, относительная влажность %s%%, давление %s мм.рт.ст",
$w->{name},
$w->{country},
ucfirst($w->{description}),
ucfirst ($w->{description}),
$w->{wind_direction},
$w->{wind_speed},
$w->{temperature_min},
@@ -68,16 +68,16 @@ sub weather($) {
return $reply;
}
sub __weather($) {
sub __weather ($) {
my $city = shift;
$city = __urlencode($city);
my $id = md5_base64($city);
$city = __urlencode ($city);
my $id = md5_base64 ($city);
my $appid = $c->{openweathermap}->{appid};
my $now = time();
my $now = time ();
my $fc;
my $w;
# attach to cache data
# attach to cache data
tie my %cachetime, 'DB_File', $c->{openweathermap}->{cachetime} or do {
warn "Something nasty happen when cachetime ties to its data: $!";
return undef;
@@ -88,23 +88,23 @@ sub __weather($) {
return undef;
};
# lookup cache
# lookup cache
if (defined ($cachetime{$id}) && ($now - $cachetime{$id}) < 10800) {
$fc = decode_json($cachedata{$id});
$fc = decode_json ($cachedata{$id});
} else {
my $r;
# try 3 times and giveup
# try 3 times and giveup
for (my $counter = 0; $counter < 3; $counter++) {
next if ($r->{success});
my $http = HTTP::Tiny->new(timeout => 3);
$r = $http->get(sprintf("http://api.openweathermap.org/data/2.5/weather?q=%s&lang=ru&APPID=%s", $city, $appid));
my $http = HTTP::Tiny->new (timeout => 3);
$r = $http->get (sprintf ("http://api.openweathermap.org/data/2.5/weather?q=%s&lang=ru&APPID=%s", $city, $appid));
sleep 2;
}
# all 3 times can give error, so check it here
# all 3 times can give error, so check it here
if ($r->{success}) {
$fc = eval { decode_json($r->{content}) };
$fc = eval { decode_json ($r->{content}) };
if ($@) {
warn "[WARN] openweathermap returns corrupted json: $@";
@@ -114,7 +114,7 @@ sub __weather($) {
};
$cachetime{$id} = $now;
$cachedata{$id} = encode_json($fc);
$cachedata{$id} = encode_json ($fc);
} else {
untie %cachetime;
untie %cachedata;
@@ -125,21 +125,21 @@ sub __weather($) {
untie %cachetime;
untie %cachedata;
# TODO: check all of this for existance
# TODO: check all of this for existance
$w->{'name'} = $fc->{name};
$w->{'state'} = $fc->{state};
$w->{'country'} = $fc->{sys}->{country};
$w->{'longitude'} = $fc->{coord}->{lon};
$w->{'latitude'} = $fc->{coord}->{lat};
$w->{'temperature_min'} = int($fc->{main}->{temp_min} - 273.15);
$w->{'temperature_max'} = int($fc->{main}->{temp_max} - 273.15);
$w->{'temperature_feelslike'} = int($fc->{main}->{feels_like} - 273.15);
$w->{'temperature_min'} = int ($fc->{main}->{temp_min} - 273.15);
$w->{'temperature_max'} = int ($fc->{main}->{temp_max} - 273.15);
$w->{'temperature_feelslike'} = int ($fc->{main}->{feels_like} - 273.15);
$w->{'humidity'} = $fc->{main}->{humidity};
$w->{'pressure'} = int($fc->{main}->{pressure} * 0.75006375541921);
$w->{'pressure'} = int ($fc->{main}->{pressure} * 0.75006375541921);
$w->{'description'} = $fc->{weather}->[0]->{description};
$w->{'wind_speed'} = $fc->{wind}->{speed};
$w->{'wind_direction'} = 'разный';
my $dir = int($fc->{wind}->{deg} + 0);
my $dir = int ($fc->{wind}->{deg} + 0);
if ($dir == 0) {
$w->{'wind_direction'} = 'северный';
@@ -180,7 +180,7 @@ sub __weather($) {
return $w;
}
sub __urlencode($) {
sub __urlencode ($) {
my $str = shift;
my $urlobj = url $str;
$str = $urlobj->as_string;
@@ -206,11 +206,11 @@ sub logger {
}
}
sub trim($) {
sub trim ($) {
my $str = shift;
while (substr ($str, 0, 1) =~ /^\s$/) {
$str = substr($str, 1);
$str = substr ($str, 1);
}
while (substr ($str, -1, 1) =~ /^\s$/) {
@@ -220,7 +220,7 @@ sub trim($) {
return $str;
}
sub randomCommonPhrase() {
sub randomCommonPhrase () {
my @myphrase = (
"Так, блядь...",
"*Закатывает рукава* И ради этого ты меня позвал?",
@@ -234,7 +234,7 @@ sub randomCommonPhrase() {
"А как ты думаешь?"
);
return ($myphrase[rand($#myphrase -1)]);
return ($myphrase[rand ($#myphrase -1)]);
}
1;