Advertisement
MyZik

Untitled

Feb 9th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.72 KB | None | 0 0
  1. <?php
  2. namespace Longman\TelegramBot\Commands\UserCommands;
  3.  
  4. use Longman\TelegramBot\Commands\SystemCommands\StartCommand;
  5. use Longman\TelegramBot\Commands\UserCommand;
  6. use Longman\TelegramBot\Conversation;
  7. use Longman\TelegramBot\DB;
  8. use Longman\TelegramBot\Entities\InputMedia\InputMediaPhoto;
  9. use Longman\TelegramBot\Entities\Keyboard;
  10. use Longman\TelegramBot\Entities\Update;
  11. use Longman\TelegramBot\Request;
  12.  
  13. class PhotoCommand extends UserCommand
  14. {
  15.     protected $name = 'photo';
  16.     protected $description = 'Загрузка фото';
  17.     protected $usage = '/photo';
  18.     protected $version = '0.3.0';
  19.     protected $need_mysql = true;
  20.     protected $private_only = true;
  21.     protected $conversation;
  22.  
  23.     /**
  24.      * Set the value of a private/protected property of an object
  25.      *
  26.      * @param object $object   Object that contains the property
  27.      * @param string $property Name of the property who's value we want to set
  28.      * @param mixed  $value    The value to set to the property
  29.      */
  30.     private function setObjectProperty($object, $property, $value)
  31.     {
  32.         $ref_object   = new \ReflectionObject($object);
  33.         $ref_property = $ref_object->getProperty($property);
  34.         $ref_property->setAccessible(true);
  35.         $ref_property->setValue($object, $value);
  36.     }
  37.  
  38.     /**
  39.      * @param $file_id
  40.      * @throws \Longman\TelegramBot\Exception\TelegramException
  41.      */
  42.     private function downloadFile($file_id)
  43.     {
  44.         $r = Request::getFile(['file_id' => $file_id]);
  45.         if ($r->isOk()) {
  46.             Request::downloadFile($r->getResult());
  47.         }
  48.     }
  49.  
  50.     /**
  51.      * @return \Longman\TelegramBot\Entities\ServerResponse
  52.      * @throws \Longman\TelegramBot\Exception\TelegramException
  53.      */
  54.     public function execute()
  55.     {
  56.         $message = $this->getMessage();
  57.         $chat    = $message->getChat();
  58.         $user    = $message->getFrom();
  59.         $text    = trim($message->getText(true));
  60.         $chat_id = $chat->getId();
  61.         $user_id = $user->getId();
  62.  
  63.         $data = [
  64.             'chat_id' => $chat_id,
  65.             'reply_markup' => $keyboard->setResizeKeyboard(true)->setOneTimeKeyboard(true)->setSelective(true)
  66.         ];
  67.  
  68.         if ($chat->isGroupChat() || $chat->isSuperGroup()) {
  69.             $data['reply_markup'] = Keyboard::forceReply(['selective' => true]);
  70.         }
  71.  
  72.         $this->conversation = new Conversation($user_id, $chat_id, $this->getName());
  73.         $notes = &$this->conversation->notes;
  74.         !is_array($notes) && $notes = [];
  75.  
  76.         $state = 0;
  77.         if (isset($notes['state'])) {
  78.             $state = $notes['state'];
  79.         }
  80.  
  81.         $result = Request::emptyResponse();
  82.         $count = 0; // Будем считать фотографии
  83.  
  84.         switch ($state) {
  85.             case 0:
  86.                 if ($message->getPhoto() === null || (isset($text) && $text !== 'Done')) {
  87.                     $notes['state'] = 0;
  88.                     $this->conversation->update();
  89.  
  90.                     /**
  91.                      * Клавиатура
  92.                      */
  93.                     $keyboard = new Keyboard(
  94.                         ['Done']
  95.                     );
  96.  
  97.                     $data['text'] = 'Отправьте фотографии, после того, как закончите, нажмите на кнопку ниже.';
  98.  
  99.                     $data['reply_markup'] = $keyboard->setResizeKeyboard(true)->setOneTimeKeyboard(false)->setSelective(true);
  100.  
  101.                     /**
  102.                      * Если тыкнули на кнопку, отправляем но новый шаг
  103.                      */
  104.                     if ($text === 'Done') {
  105.                         $notes['state'] = 1;
  106.                         $this->conversation->update();
  107.                         $data['reply_markup'] = Keyboard::remove();
  108.                     }
  109.  
  110.                     $result = Request::sendMessage($data);
  111.                     break;
  112.                 }
  113.  
  114.                 $notes['files'][] = $message->getPhoto()[$count]->getFileId(); // Получаем ID файла
  115.                 $this->downloadFile($notes['files']); // Скачиваем
  116.                 $this->setObjectProperty($message, 'photo', null); // Нулляем объект message
  117.                 $count++;
  118.  
  119.                 $text = '';
  120.  
  121.             case 1:
  122.                 $this->conversation->update();
  123.                 unset($notes['state']);
  124.  
  125.                 $data['text'] = 'Тест';
  126.  
  127.                 $data['reply_markup'] = Keyboard::remove();
  128.  
  129.                 $this->conversation->stop();
  130.                 $result = Request::sendMessage($data);
  131.  
  132.                 break;
  133.         }
  134.         return $result;
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement