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.

110 lines
2.8 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 "/image" command
  13. *
  14. * Randomly fetch any uploaded image from the Uploads path and send it to the user.
  15. */
  16. namespace Longman\TelegramBot\Commands\UserCommands;
  17. use Longman\TelegramBot\Commands\UserCommand;
  18. use Longman\TelegramBot\Entities\ServerResponse;
  19. use Longman\TelegramBot\Exception\TelegramException;
  20. use Longman\TelegramBot\Request;
  21. class ImageCommand extends UserCommand
  22. {
  23. /**
  24. * @var string
  25. */
  26. protected $name = 'image';
  27. /**
  28. * @var string
  29. */
  30. protected $description = 'Randomly fetch any uploaded image';
  31. /**
  32. * @var string
  33. */
  34. protected $usage = '/image';
  35. /**
  36. * @var string
  37. */
  38. protected $version = '1.2.0';
  39. /**
  40. * Main command execution
  41. *
  42. * @return ServerResponse
  43. * @throws TelegramException
  44. */
  45. public function execute(): ServerResponse
  46. {
  47. $message = $this->getMessage();
  48. // Use any extra parameters as the caption text.
  49. $caption = trim($message->getText(true));
  50. // Make sure the Upload path has been defined and exists.
  51. $upload_path = $this->telegram->getUploadPath();
  52. if (!is_dir($upload_path)) {
  53. return $this->replyToChat('Upload path has not been defined or does not exist.');
  54. }
  55. // Get a random picture from the Upload path.
  56. $random_image = $this->getRandomImagePath($upload_path);
  57. if ('' === $random_image) {
  58. return $this->replyToChat('No image found!');
  59. }
  60. // If no caption is set, use the filename.
  61. if ('' === $caption) {
  62. $caption = basename($random_image);
  63. }
  64. return Request::sendPhoto([
  65. 'chat_id' => $message->getFrom()->getId(),
  66. 'caption' => $caption,
  67. 'photo' => $random_image,
  68. ]);
  69. }
  70. /**
  71. * Return the path to a random image in the passed directory.
  72. *
  73. * @param string $dir
  74. *
  75. * @return string
  76. */
  77. private function getRandomImagePath($dir): string
  78. {
  79. if (!is_dir($dir)) {
  80. return '';
  81. }
  82. // Filter the file list to only return images.
  83. $image_list = array_filter(scandir($dir), function ($file) {
  84. $extension = pathinfo($file, PATHINFO_EXTENSION);
  85. return in_array($extension, ['png', 'jpg', 'jpeg', 'gif']);
  86. });
  87. if (!empty($image_list)) {
  88. shuffle($image_list);
  89. return $dir . '/' . $image_list[0];
  90. }
  91. return '';
  92. }
  93. }