Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 36 additions & 11 deletions app/lib/Core/Transport/Telegram.pm
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ sub profile {
my $config = $self->config;

$self->{profile} = $name;
delete $self->{user_tg_settings}; # профиль изменился — сбрасываем кэш настроек

if ( my $profile = $config->{ $name } ) {
$self->{token} = $profile->{token};
Expand Down Expand Up @@ -283,6 +284,11 @@ sub send {
next;
}

if ( $self->user_tg_settings->{status} eq 'chat_not_found' ) {
push @ret, { msg => "Чат с ботом не найден: пользователь ещё не запустил бота", profile => $profile };
next;
}

my $response;
if ( ref $data ) {
my @arr;
Expand Down Expand Up @@ -519,20 +525,39 @@ sub http {
logger->dump('Answer from TG API', $response->decoded_content );

unless ( $response->is_success ) {
my $message = $response->decoded_content;
logger->error( $message );
logger->error( $response->decoded_content );
$self->set_profile_status_by_error( $response );
}
return $response;
}

if ( $response->code == 403 ) {
$self->user->set_settings({
telegram => {
$self->{profile} => {
status => 'kicked',
},
}
});
# Помечаем профиль пользователя по ошибке Telegram API,
# чтобы не отправлять сообщения в заведомо недоступный чат
sub set_profile_status_by_error {
my $self = shift;
my $response = shift;

my $status;
if ( $response->code == 403 ) {
# пользователь заблокировал бота
$status = 'kicked';
} elsif ( $response->code == 400 ) {
my $error = decode_json( $response->decoded_content ) || {};
# чата не существует: пользователь ещё не запускал этого бота
# (например, регистрация через Telegram Login Widget или внешний API)
if ( ( $error->{description} // '' ) =~ /chat not found/i ) {
$status = 'chat_not_found';
}
}
return $response;
return unless $status;

$self->user->set_settings({
telegram => {
$self->{profile} => {
status => $status,
},
}
});
}

sub sendMessage {
Expand Down