use Modern::Perl;
use utf8;
use Mojo::Base -strict, -async_await, -signatures;
use Data::Dumper;
our $client;
our $fsa;
our $abon_client;
sub __
{
@_;
}
##############################################
our $commands = [
{command=>"help", name=>__("Помощь"), description=>__("Список доступных команд"), hide_in_kb=>1},
{command=>"info", name=>__("Информация"), description=>__("Информация о пользователе")},
{command=>"balance", name=>__("Баланс"), description=>__("Проверка баланса")},
{command=>"service", name=>__("Сервисы"), description=>__("Подключенные сервисы")},
{command=>"credit", name=>__("Кредит"), description=>__("Установка кредита")},
{command=>"logout", name=>__("Выход"), description=>__("Выход")},
];
##############################################
sub command_help($chatid, $uid, $rest)
{
my @list = map { "/$_->{command} " . _($_->{description}) } @$commands;
notify($chatid, join("\n", @list), $rest);
}
sub command_start($chatid, $uid, $from)
{
set_new_state("");
}
sub command_logout($chatid, $uid, $rest)
{
notify($chatid, _("Благодарим за использование нашего бота"));
$client->del("client", "/client/$uid/telegram");
if (my $err = $client->error)
{
report($chatid, $err);
}
$fsa->notes(uid => undef);
set_new_state("logged_out");
}
async sub command_balance
{
my ($chatid, $uid, $rest) = @_;
my $money = await $abon_client->get_p($chatid, "client", "/client/$uid/money?human=1");
my $cur = $money->{human};
my @lines = (
sprintf("%s: %.2f $cur (%s %.2f $cur + %s %.2f $cur) ",
_("Ваш баланс"), $money->{balance}, ("депозит"), $money->{deposit}, _("кредит"), $money->{credit}),
);
push @lines, sprintf("%s: %s", _("Оплачено до"), format_date($money->{last_day})) if $money->{last_day} ne "-";
push @lines, sprintf("%s: %d%%", _("Скидка"), $money->{reduction}) if $money->{reduction};
push @lines, sprintf("%s: %.2f $cur %s (%s)",
_("Последнее снятие"), $money->{last_withdrawal}->{sum}, format_time($money->{last_withdrawal}->{date}), $money->{last_withdrawal}->{comment})
if $money->{last_withdrawal}->{sum};
push @lines, sprintf("%s: %.2f $cur %s", _("Последний платеж"), $money->{last_payment}->{sum}, format_time($money->{last_payment}->{date}))
if $money->{last_payment}->{sum};
for (keys %{ $money->{accounts} })
{
push @lines, sprintf("%s: %.2f $cur", $_, $money->{accounts}->{$_});
}
reply($rest, @lines);
}
async sub command_info
{
my ($chatid, $uid, $rest) = @_;
my $client = await $abon_client->get_p($chatid, "client", "/client/$uid");
reply($rest,
sprintf("%s: %d", _("Лицевой счет"), $client->{uid}),
sprintf("%s: %s", _("Логин"), $client->{login}),
sprintf("%s: %s", _("ФИО"), $client->{fio}),
sprintf("%s: %s", _("Адрес"), $client->{address}),
sprintf("%s: %s", _("Телефон"), $client->{phone}),
);
}
async sub command_credit
{
my ($chatid, $uid, $rest) = @_;
my $money = await $abon_client->get_p($chatid, "client", "/client/$uid/money?human=1");
if ($money->{credit} > 0)
{
return reply($rest, sprintf("%s %.2f %s", _("У вас уже установлен кредит"), $money->{credit}, $money->{human}));
}
reply_with($rest, {
inline_menu => [[
{ text=>_("Я согласен с условиями"), callback_data=>"/set-credit" },
]],
},
_("Вы можете самостоятельно установить кредит на два дня"),
_("Ограничения: только для физических лиц, продлевать кредит повторно до оплаты нельзя. При следующей оплате кредит будет погашен"),
);
}
async sub command_set_credit
{
my ($chatid, $uid, $rest) = @_;
my $res = await $abon_client->post_p($chatid, "client", "/client/$uid/credit", {human=>1});
return reply($rest, _("Кредит не имеет смысла для бесплатных тарифных планов")) if $res->{credit} == 0;
await reply($rest,
sprintf("%s %.2f %s", _("Установлен кредит "), $res->{credit}, $res->{human}),
"",
);
command_balance($chatid, $uid, $rest);
}
=cut
$VAR1 = [
{
'tariff' => {
'monthly' => '0',
'dayly' => '0',
'entity' => 1000,
'speed' => 0,
'name_ru' => "\x{414}\x{43b}\x{44f} \x{441}\x{43e}\x{442}\x{440}\x{443}\x{434}\x{43d}\x{438}\x{43a}\x{43e}\x{432} MOL"
},
'name' => 'pppoe',
'name_ru' => 'PPPoE',
'entity' => 32320,
'disabled' => 0
},
{
'tariff' => {
'speed' => 0,
'name_ru' => "\x{414}\x{43b}\x{44f} \x{441}\x{43e}\x{442}\x{440}\x{443}\x{434}\x{43d}\x{438}\x{43a}\x{43e}\x{432} MOL",
'entity' => 1000,
'monthly' => '0',
'dayly' => '0'
},
'name' => 'ipoe',
'entity' => 17,
'name_ru' => 'IPoE',
'disabled' => 0
}
];
=cut
async sub command_service
{
my ($chatid, $uid, $rest) = @_;
my $res = await $abon_client->get_p($chatid, "client", "/client/$uid/service?human=1&as-array=1");
my @list = map { sprintf("%s: %s (%s '%s')", $_->{name_ru}, format_wd($_->{tariff}, $_->{human}), _("тариф"), $_->{tariff}->{name_ru}) }
grep { !$_->{disabled} } @$res;
reply($rest, @list);
};
sub format_wd($rec, $cur)
{
return _("бесплатно") if $rec->{dayly} == 0 && $rec->{monthly} == 0;
my $m = sprintf("%.2f $cur %s", $rec->{monthly}, _("в месяц")) if $rec->{monthly} != 0;
my $d = sprintf("%.2f $cur %s", $rec->{dayly}, _("в месяц")) if $rec->{dayly} != 0;
return ("$m + $d") if $m && $d;
return $m if $m;
return $d if $d;
}
################################################
sub parse_error
{
my $e = shift;
return $e unless ref $e;
return "$e->{code} $e->{message} $e->{body}";
}
1;
# что будет, если, к примеру, установка кредита вернет ошибку?
# сервисы
# перевод денег