You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.1 KiB

1 year ago
  1. <?php
  2. /**
  3. * This file is part of the PHP Telegram Bot example-bot package.
  4. * https://github.com/php-telegram-bot/example-bot/
  5. *
  6. * (c) PHP Telegram Bot Team
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. * User "/whoami" command
  13. *
  14. * Simple command that returns info about the current user.
  15. */
  16. namespace Longman\TelegramBot\Commands\UserCommands;
  17. use Longman\TelegramBot\ChatAction;
  18. use Longman\TelegramBot\Commands\UserCommand;
  19. use Longman\TelegramBot\Entities\ServerResponse;
  20. use Longman\TelegramBot\Entities\UserProfilePhotos;
  21. use Longman\TelegramBot\Exception\TelegramException;
  22. use Longman\TelegramBot\Request;
  23. class WhoamiCommand extends UserCommand
  24. {
  25. /**
  26. * @var string
  27. */
  28. protected $name = 'whoami';
  29. /**
  30. * @var string
  31. */
  32. protected $description = 'Show your id, name and username';
  33. /**
  34. * @var string
  35. */
  36. protected $usage = '/whoami';
  37. /**
  38. * @var string
  39. */
  40. protected $version = '1.2.0';
  41. /**
  42. * @var bool
  43. */
  44. protected $private_only = true;
  45. /**
  46. * Main command execution
  47. *
  48. * @return ServerResponse
  49. * @throws TelegramException
  50. */
  51. public function execute(): ServerResponse
  52. {
  53. $message = $this->getMessage();
  54. $from = $message->getFrom();
  55. $user_id = $from->getId();
  56. $chat_id = $message->getChat()->getId();
  57. $message_id = $message->getMessageId();
  58. $data = [
  59. 'chat_id' => $chat_id,
  60. 'reply_to_message_id' => $message_id,
  61. ];
  62. // Send chat action "typing..."
  63. Request::sendChatAction([
  64. 'chat_id' => $chat_id,
  65. 'action' => ChatAction::TYPING,
  66. ]);
  67. $caption = sprintf(
  68. 'Your Id: %d' . PHP_EOL .
  69. 'Name: %s %s' . PHP_EOL .
  70. 'Username: %s',
  71. $user_id,
  72. $from->getFirstName(),
  73. $from->getLastName(),
  74. $from->getUsername()
  75. );
  76. // Fetch the most recent user profile photo
  77. $limit = 1;
  78. $offset = null;
  79. $user_profile_photos_response = Request::getUserProfilePhotos([
  80. 'user_id' => $user_id,
  81. 'limit' => $limit,
  82. 'offset' => $offset,
  83. ]);
  84. if ($user_profile_photos_response->isOk()) {
  85. /** @var UserProfilePhotos $user_profile_photos */
  86. $user_profile_photos = $user_profile_photos_response->getResult();
  87. if ($user_profile_photos->getTotalCount() > 0) {
  88. $photos = $user_profile_photos->getPhotos();
  89. // Get the best quality of the profile photo
  90. $photo = end($photos[0]);
  91. $file_id = $photo->getFileId();
  92. $data['photo'] = $file_id;
  93. $data['caption'] = $caption;
  94. return Request::sendPhoto($data);
  95. }
  96. }
  97. // No Photo just send text
  98. $data['text'] = $caption;
  99. return Request::sendMessage($data);
  100. }
  101. }