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.

115 lines
3.4 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 "/upload" command
  13. *
  14. * A command that allows users to upload files to your bot, saving them to the bot's "Download" folder.
  15. *
  16. * IMPORTANT NOTICE
  17. * This is a "demo", do NOT use this as-is in your bot!
  18. * Know the security implications of allowing users to upload arbitrary files to your server!
  19. */
  20. namespace Longman\TelegramBot\Commands\UserCommands;
  21. use Longman\TelegramBot\Commands\UserCommand;
  22. use Longman\TelegramBot\Conversation;
  23. use Longman\TelegramBot\Entities\Keyboard;
  24. use Longman\TelegramBot\Entities\ServerResponse;
  25. use Longman\TelegramBot\Exception\TelegramException;
  26. use Longman\TelegramBot\Request;
  27. class UploadCommand extends UserCommand
  28. {
  29. /**
  30. * @var string
  31. */
  32. protected $name = 'upload';
  33. /**
  34. * @var string
  35. */
  36. protected $description = 'Upload and save files';
  37. /**
  38. * @var string
  39. */
  40. protected $usage = '/upload';
  41. /**
  42. * @var string
  43. */
  44. protected $version = '0.2.0';
  45. /**
  46. * @var bool
  47. */
  48. protected $need_mysql = true;
  49. /**
  50. * Main command execution
  51. *
  52. * @return ServerResponse
  53. * @throws TelegramException
  54. */
  55. public function execute(): ServerResponse
  56. {
  57. $message = $this->getMessage();
  58. $chat = $message->getChat();
  59. $chat_id = $chat->getId();
  60. $user_id = $message->getFrom()->getId();
  61. // Make sure the Download path has been defined and exists
  62. $download_path = $this->telegram->getDownloadPath();
  63. if (!is_dir($download_path)) {
  64. return $this->replyToChat('Download path has not been defined or does not exist.');
  65. }
  66. // Initialise the data array for the response
  67. $data = ['chat_id' => $chat_id];
  68. if ($chat->isGroupChat() || $chat->isSuperGroup()) {
  69. // Reply to message id is applied by default
  70. $data['reply_to_message_id'] = $message->getMessageId();
  71. // Force reply is applied by default to work with privacy on
  72. $data['reply_markup'] = Keyboard::forceReply(['selective' => true]);
  73. }
  74. // Start conversation
  75. $conversation = new Conversation($user_id, $chat_id, $this->getName());
  76. $message_type = $message->getType();
  77. if (in_array($message_type, ['audio', 'document', 'photo', 'video', 'voice'], true)) {
  78. $doc = $message->{'get' . ucfirst($message_type)}();
  79. // For photos, get the best quality!
  80. ($message_type === 'photo') && $doc = end($doc);
  81. $file_id = $doc->getFileId();
  82. $file = Request::getFile(['file_id' => $file_id]);
  83. if ($file->isOk() && Request::downloadFile($file->getResult())) {
  84. $data['text'] = $message_type . ' file is located at: ' . $download_path . '/' . $file->getResult()->getFilePath();
  85. } else {
  86. $data['text'] = 'Failed to download.';
  87. }
  88. $conversation->notes['file_id'] = $file_id;
  89. $conversation->update();
  90. $conversation->stop();
  91. } else {
  92. $data['text'] = 'Please upload the file now';
  93. }
  94. return Request::sendMessage($data);
  95. }
  96. }