Rework BotAPI error handling, this time it looks kinda working, still wip, though
This commit is contained in:
@@ -13,7 +13,7 @@ use Log::Any qw ($log);
|
|||||||
use Mojo::Redis ();
|
use Mojo::Redis ();
|
||||||
use Mojo::Redis::Connection ();
|
use Mojo::Redis::Connection ();
|
||||||
|
|
||||||
use BotLib::Admin qw (MigrateSettingsToNewChatID);
|
use BotLib::Admin qw (MigrateSettingsToNewChatID FortuneToggle);
|
||||||
use BotLib::Conf qw (LoadConf);
|
use BotLib::Conf qw (LoadConf);
|
||||||
|
|
||||||
use version; our $VERSION = qw (1.0);
|
use version; our $VERSION = qw (1.0);
|
||||||
@@ -53,6 +53,7 @@ sub redis_parse_message {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$message->{text} = "$m->{message}";
|
$message->{text} = "$m->{message}";
|
||||||
|
|
||||||
if (defined ($m->{threadid}) && $m->{threadid} ne '') {
|
if (defined ($m->{threadid}) && $m->{threadid} ne '') {
|
||||||
$message->{message_thread_id} = 0 + $m->{threadid};
|
$message->{message_thread_id} = 0 + $m->{threadid};
|
||||||
}
|
}
|
||||||
@@ -61,7 +62,7 @@ sub redis_parse_message {
|
|||||||
my $can_talk = $main::TGM->can_talk ({ chat_id => 0 + $m->{chatid}});
|
my $can_talk = $main::TGM->can_talk ({ chat_id => 0 + $m->{chatid}});
|
||||||
|
|
||||||
if ($can_talk->{error}) {
|
if ($can_talk->{error}) {
|
||||||
$log->error('[ERROR] Unable to guess if i can talk in this chat: ' . Dumper ($can_talk));
|
$log->error ('[ERROR] Unable to guess if i can talk in this chat: ' . Dumper ($can_talk));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,52 +70,46 @@ sub redis_parse_message {
|
|||||||
my $r = $main::TGM->sendMessage ($message);
|
my $r = $main::TGM->sendMessage ($message);
|
||||||
|
|
||||||
if ($r->{error}) {
|
if ($r->{error}) {
|
||||||
if (defined ($r->{http_code}) && $r->{http_code} == 400) {
|
# TODO: handle 50x errors? They occur when api servers being updated.
|
||||||
unless (defined $r->{$message}) {
|
$log->error ('[ERROR] Unable to call sendMessage() BotAPI method: ' . Dumper ($r));
|
||||||
$log->error ("[ERROR] An error occured while sending message to chat $m->{chatid}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
my $j = eval { decode_json ($r->{$message}); };
|
my $resp = eval { $r->{debug}->json };
|
||||||
|
|
||||||
unless (defined $j) {
|
if (defined $resp) {
|
||||||
$log->error ("[ERROR] An error occured while sending message to chat $m->{chatid}: $EVAL_ERROR");
|
$log->error ('[ERROR] BotAPI returns parsable error: ' . Dumper ($resp));
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (defined ($j->{parameters}) && defined $j->{parameters}->{migrate_to_chat_id}) {
|
if (defined ($resp->{error_code}) && $resp->{error_code} == 400) {
|
||||||
MigrateSettingsToNewChatID ($m->{chatid}, $j->{parameters}->{migrate_to_chat_id});
|
if (defined $resp->{description}) {
|
||||||
$message->{chat_id} = $j->{parameters}->{migrate_to_chat_id};
|
if ($resp->{description} eq 'Bad Request: have no rights to send a message') {
|
||||||
} elsif (defined ($j->{description})) {
|
$log->notice ("[NOTICE] Have no rights to send message to $m->{chatid}");
|
||||||
if ($j->{description} eq 'Bad Request: chat not found') {
|
# TODO: disable fortune if message is good morning fortune
|
||||||
if (defined $j->{param}->{chat_id}) {
|
} elsif ($resp->{description} eq 'Bad Request: chat not found') {
|
||||||
# Выключим явным образом посылку фортунки с утра
|
$log->notice (
|
||||||
FortuneToggle ($j->{param}->{chat_id}, 0);
|
"[NOTICE] Chat not found toggling good morning fortune off for $m->{chatid}"
|
||||||
|
);
|
||||||
|
|
||||||
|
FortuneToggle($m->{chatid}, 0);
|
||||||
return;
|
return;
|
||||||
|
} elsif ($resp->description eq 'Bad Request: group chat was upgraded to a supergroup chat') {
|
||||||
|
if (defined ($resp->{parameters}) &&
|
||||||
|
defined ($resp->{parameters}->{migrate_to_chat_id})) {
|
||||||
|
my $tpl = '[NOTICE] group upgraded to supergroup, chat_id changed, ';
|
||||||
|
$tpl .= 'migrating settings from %s to %s';
|
||||||
|
|
||||||
|
$log->notice (
|
||||||
|
sprintf($tpl, $m->{chatid}, $resp->{parameters}->{migrate_to_chat_id})
|
||||||
|
);
|
||||||
|
|
||||||
|
MigrateSettingsToNewChatID (
|
||||||
|
$m->{chatid},
|
||||||
|
$resp->{parameters}->{migrate_to_chat_id}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$log->error ('[ERROR] Unable to call sendMessage() BotAPI method: ' . Dumper ($r));
|
||||||
}
|
}
|
||||||
} elsif ($j->{description} eq 'Bad Request: have no rights to send a message') {
|
|
||||||
# Ебитес с этим как хотите, по идее $can_talk->{can_talk} должен был вернуть 0
|
|
||||||
# looks like race condition :)
|
|
||||||
$log->error ("[ERROR] Unable to send messages to chat $m->{chatid}: $j->{description}");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
$log->error ("[ERROR] An error occured while sending message to chat $m->{chatid}");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$r = undef;
|
|
||||||
$r = $main::TGM->sendMessage ($message);
|
|
||||||
|
|
||||||
if ($r->{error}) {
|
|
||||||
$log->error (
|
|
||||||
"[ERROR] An error occured while sending message to chat $message->{chat_id}: " .
|
|
||||||
Dumper ($r)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
# TODO: handle 50x errors? They occur when api servers being updated.
|
|
||||||
$log->error ('[ERROR] Unable to call sendMessage() BotAPI method: ' . Dumper ($r));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user