commands.pm 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. use Modern::Perl;
  2. use utf8;
  3. use Mojo::Base -strict, -async_await, -signatures;
  4. use Data::Dumper;
  5. our $client;
  6. our $fsa;
  7. our $abon_client;
  8. sub __
  9. {
  10. @_;
  11. }
  12. ##############################################
  13. our $commands = [
  14. {command=>"help", name=>__("Помощь"), description=>__("Список доступных команд"), hide_in_kb=>1},
  15. {command=>"info", name=>__("Информация"), description=>__("Информация о пользователе")},
  16. {command=>"balance", name=>__("Баланс"), description=>__("Проверка баланса")},
  17. {command=>"service", name=>__("Сервисы"), description=>__("Подключенные сервисы")},
  18. {command=>"credit", name=>__("Кредит"), description=>__("Установка кредита")},
  19. {command=>"logout", name=>__("Выход"), description=>__("Выход")},
  20. ];
  21. ##############################################
  22. sub command_help($chatid, $uid, $rest)
  23. {
  24. my @list = map { "<b>/$_->{command}</b> " . _($_->{description}) } @$commands;
  25. notify($chatid, join("\n", @list), $rest);
  26. }
  27. sub command_start($chatid, $uid, $from)
  28. {
  29. set_new_state("");
  30. }
  31. sub command_logout($chatid, $uid, $rest)
  32. {
  33. notify($chatid, _("Благодарим за использование нашего бота"));
  34. $client->del("client", "/client/$uid/telegram");
  35. if (my $err = $client->error)
  36. {
  37. report($chatid, $err);
  38. }
  39. $fsa->notes(uid => undef);
  40. set_new_state("logged_out");
  41. }
  42. async sub command_balance
  43. {
  44. my ($chatid, $uid, $rest) = @_;
  45. my $money = await $abon_client->get_p($chatid, "client", "/client/$uid/money?human=1");
  46. my $cur = $money->{human};
  47. my @lines = (
  48. sprintf("<u>%s:</u> <b>%.2f $cur</b> (%s %.2f $cur + %s %.2f $cur) ",
  49. _("Ваш баланс"), $money->{balance}, ("депозит"), $money->{deposit}, _("кредит"), $money->{credit}),
  50. );
  51. push @lines, sprintf("<u>%s:</u> %s", _("Оплачено до"), format_date($money->{last_day})) if $money->{last_day} ne "-";
  52. push @lines, sprintf("<u>%s:</u> %d%%", _("Скидка"), $money->{reduction}) if $money->{reduction};
  53. push @lines, sprintf("<u>%s:</u> <b>%.2f $cur</b> %s (%s)",
  54. _("Последнее снятие"), $money->{last_withdrawal}->{sum}, format_time($money->{last_withdrawal}->{date}), $money->{last_withdrawal}->{comment})
  55. if $money->{last_withdrawal}->{sum};
  56. push @lines, sprintf("<u>%s:</u> <b>%.2f $cur</b> %s", _("Последний платеж"), $money->{last_payment}->{sum}, format_time($money->{last_payment}->{date}))
  57. if $money->{last_payment}->{sum};
  58. for (keys %{ $money->{accounts} })
  59. {
  60. push @lines, sprintf("<u>%s:</u> <b>%.2f $cur</b>", $_, $money->{accounts}->{$_});
  61. }
  62. reply($rest, @lines);
  63. }
  64. async sub command_info
  65. {
  66. my ($chatid, $uid, $rest) = @_;
  67. my $client = await $abon_client->get_p($chatid, "client", "/client/$uid");
  68. reply($rest,
  69. sprintf("<u>%s</u>: %d", _("Лицевой счет"), $client->{uid}),
  70. sprintf("<u>%s</u>: %s", _("Логин"), $client->{login}),
  71. sprintf("<u>%s</u>: %s", _("ФИО"), $client->{fio}),
  72. sprintf("<u>%s</u>: %s", _("Адрес"), $client->{address}),
  73. sprintf("<u>%s</u>: %s", _("Телефон"), $client->{phone}),
  74. );
  75. }
  76. async sub command_credit
  77. {
  78. my ($chatid, $uid, $rest) = @_;
  79. my $money = await $abon_client->get_p($chatid, "client", "/client/$uid/money?human=1");
  80. if ($money->{credit} > 0)
  81. {
  82. return reply($rest, sprintf("%s <b>%.2f %s</b>", _("У вас уже установлен кредит"), $money->{credit}, $money->{human}));
  83. }
  84. reply_with($rest, {
  85. inline_menu => [[
  86. { text=>_("Я согласен с условиями"), callback_data=>"/set-credit" },
  87. ]],
  88. },
  89. _("Вы можете самостоятельно установить кредит на два дня"),
  90. _("<b>Ограничения</b>: только для физических лиц, продлевать кредит повторно до оплаты нельзя. При следующей оплате кредит будет погашен"),
  91. );
  92. }
  93. async sub command_set_credit
  94. {
  95. my ($chatid, $uid, $rest) = @_;
  96. my $res = await $abon_client->post_p($chatid, "client", "/client/$uid/credit", {human=>1});
  97. return reply($rest, _("Кредит не имеет смысла для бесплатных тарифных планов")) if $res->{credit} == 0;
  98. await reply($rest,
  99. sprintf("%s <b>%.2f %s</b>", _("Установлен кредит "), $res->{credit}, $res->{human}),
  100. "",
  101. );
  102. command_balance($chatid, $uid, $rest);
  103. }
  104. =cut
  105. $VAR1 = [
  106. {
  107. 'tariff' => {
  108. 'monthly' => '0',
  109. 'dayly' => '0',
  110. 'entity' => 1000,
  111. 'speed' => 0,
  112. '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"
  113. },
  114. 'name' => 'pppoe',
  115. 'name_ru' => 'PPPoE',
  116. 'entity' => 32320,
  117. 'disabled' => 0
  118. },
  119. {
  120. 'tariff' => {
  121. 'speed' => 0,
  122. '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",
  123. 'entity' => 1000,
  124. 'monthly' => '0',
  125. 'dayly' => '0'
  126. },
  127. 'name' => 'ipoe',
  128. 'entity' => 17,
  129. 'name_ru' => 'IPoE',
  130. 'disabled' => 0
  131. }
  132. ];
  133. =cut
  134. async sub command_service
  135. {
  136. my ($chatid, $uid, $rest) = @_;
  137. my $res = await $abon_client->get_p($chatid, "client", "/client/$uid/service?human=1&as-array=1");
  138. my @list = map { sprintf("<u>%s:</u> %s (%s '%s')", $_->{name_ru}, format_wd($_->{tariff}, $_->{human}), _("тариф"), $_->{tariff}->{name_ru}) }
  139. grep { !$_->{disabled} } @$res;
  140. reply($rest, @list);
  141. };
  142. sub format_wd($rec, $cur)
  143. {
  144. return _("бесплатно") if $rec->{dayly} == 0 && $rec->{monthly} == 0;
  145. my $m = sprintf("<b>%.2f $cur</b> %s", $rec->{monthly}, _("в месяц")) if $rec->{monthly} != 0;
  146. my $d = sprintf("<b>%.2f $cur</b> %s", $rec->{dayly}, _("в месяц")) if $rec->{dayly} != 0;
  147. return ("$m + $d") if $m && $d;
  148. return $m if $m;
  149. return $d if $d;
  150. }
  151. ################################################
  152. sub parse_error
  153. {
  154. my $e = shift;
  155. return $e unless ref $e;
  156. return "$e->{code} $e->{message} $e->{body}";
  157. }
  158. 1;
  159. # что будет, если, к примеру, установка кредита вернет ошибку?
  160. # сервисы
  161. # перевод денег