SecretService.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Services;
  3. use App\Constants\StepStatus;
  4. use Illuminate\Support\Facades\Cache;
  5. use Telegram\Bot\Api;
  6. use Telegram\Bot\Exceptions\TelegramSDKException;
  7. class SecretService
  8. {
  9. /**
  10. * @param Api $telegram
  11. * @param $data
  12. * @param $chatId
  13. * @param $firstName
  14. * @param $messageId
  15. * @throws TelegramSDKException
  16. */
  17. public static function init(Api $telegram, $data, $chatId, $firstName, $messageId): void
  18. {
  19. if ($data === "secret@@index") {
  20. $res = SecretService::index($chatId, $messageId);
  21. $telegram->editMessageText($res);
  22. }
  23. //找回账号
  24. if ($data === "secret@@retrieve") {
  25. $res = SecretService::retrieve($chatId, $messageId);
  26. $telegram->editMessageText($res);
  27. }
  28. }
  29. public static function onMessage($chatId, $text, $messageId, $stepStatus): null|array
  30. {
  31. return match ($stepStatus) {
  32. StepStatus::MY_INPUT_OLD_SECRET => SecretService::inputOldSecret($chatId, $text, $messageId),
  33. default => null
  34. };
  35. }
  36. private static function inputOldSecret($chatId, $secret, $messageId): array
  37. {
  38. $text = "";
  39. return [
  40. 'chat_id' => $chatId,
  41. 'text' => $text,
  42. ];
  43. }
  44. private static function retrieve($chatId, $messageId): array
  45. {
  46. Cache::put(get_step_key($chatId), StepStatus::MY_INPUT_OLD_SECRET);
  47. return [
  48. 'chat_id' => $chatId,
  49. 'text' => '请输入原账号的秘钥',
  50. 'message_id' => $messageId,
  51. ];
  52. }
  53. private static function index($chatId, $messageId): array
  54. {
  55. $keyboard = [
  56. [
  57. ['text' => '查看秘钥', 'callback_data' => 'secret@@view'],
  58. ['text' => '找回账号', 'callback_data' => 'secret@@retrieve'],
  59. ],
  60. [
  61. ['text' => '返回', 'callback_data' => 'topUp@@home'],
  62. ]
  63. ];
  64. $text = "秘钥管理\n";
  65. $text .= "请选择业务类型";
  66. return [
  67. 'chat_id' => $chatId,
  68. 'text' => $text,
  69. 'message_id' => $messageId,
  70. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  71. ];
  72. }
  73. }