Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Longman\TelegramBot\Commands\UserCommands;
- use Longman\TelegramBot\Commands\SystemCommands\StartCommand;
- use Longman\TelegramBot\Commands\UserCommand;
- use Longman\TelegramBot\Conversation;
- use Longman\TelegramBot\DB;
- use Longman\TelegramBot\Entities\InputMedia\InputMediaPhoto;
- use Longman\TelegramBot\Entities\Keyboard;
- use Longman\TelegramBot\Entities\Update;
- use Longman\TelegramBot\Request;
- class PhotoCommand extends UserCommand
- {
- protected $name = 'photo';
- protected $description = 'Загрузка фото';
- protected $usage = '/photo';
- protected $version = '0.3.0';
- protected $need_mysql = true;
- protected $private_only = true;
- protected $conversation;
- /**
- * Set the value of a private/protected property of an object
- *
- * @param object $object Object that contains the property
- * @param string $property Name of the property who's value we want to set
- * @param mixed $value The value to set to the property
- */
- private function setObjectProperty($object, $property, $value)
- {
- $ref_object = new \ReflectionObject($object);
- $ref_property = $ref_object->getProperty($property);
- $ref_property->setAccessible(true);
- $ref_property->setValue($object, $value);
- }
- /**
- * @param $file_id
- * @throws \Longman\TelegramBot\Exception\TelegramException
- */
- private function downloadFile($file_id)
- {
- $r = Request::getFile(['file_id' => $file_id]);
- if ($r->isOk()) {
- Request::downloadFile($r->getResult());
- }
- }
- /**
- * @return \Longman\TelegramBot\Entities\ServerResponse
- * @throws \Longman\TelegramBot\Exception\TelegramException
- */
- public function execute()
- {
- $message = $this->getMessage();
- $chat = $message->getChat();
- $user = $message->getFrom();
- $text = trim($message->getText(true));
- $chat_id = $chat->getId();
- $user_id = $user->getId();
- $data = [
- 'chat_id' => $chat_id,
- 'reply_markup' => $keyboard->setResizeKeyboard(true)->setOneTimeKeyboard(true)->setSelective(true)
- ];
- if ($chat->isGroupChat() || $chat->isSuperGroup()) {
- $data['reply_markup'] = Keyboard::forceReply(['selective' => true]);
- }
- $this->conversation = new Conversation($user_id, $chat_id, $this->getName());
- $notes = &$this->conversation->notes;
- !is_array($notes) && $notes = [];
- $state = 0;
- if (isset($notes['state'])) {
- $state = $notes['state'];
- }
- $result = Request::emptyResponse();
- $count = 0; // Будем считать фотографии
- switch ($state) {
- case 0:
- if ($message->getPhoto() === null || (isset($text) && $text !== 'Done')) {
- $notes['state'] = 0;
- $this->conversation->update();
- /**
- * Клавиатура
- */
- $keyboard = new Keyboard(
- ['Done']
- );
- $data['text'] = 'Отправьте фотографии, после того, как закончите, нажмите на кнопку ниже.';
- $data['reply_markup'] = $keyboard->setResizeKeyboard(true)->setOneTimeKeyboard(false)->setSelective(true);
- /**
- * Если тыкнули на кнопку, отправляем но новый шаг
- */
- if ($text === 'Done') {
- $notes['state'] = 1;
- $this->conversation->update();
- $data['reply_markup'] = Keyboard::remove();
- }
- $result = Request::sendMessage($data);
- break;
- }
- $notes['files'][] = $message->getPhoto()[$count]->getFileId(); // Получаем ID файла
- $this->downloadFile($notes['files']); // Скачиваем
- $this->setObjectProperty($message, 'photo', null); // Нулляем объект message
- $count++;
- $text = '';
- case 1:
- $this->conversation->update();
- unset($notes['state']);
- $data['text'] = 'Тест';
- $data['reply_markup'] = Keyboard::remove();
- $this->conversation->stop();
- $result = Request::sendMessage($data);
- break;
- }
- return $result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement