Advertisement
MyZik

Untitled

Jun 17th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.79 KB | None | 0 0
  1. <?php
  2. ...
  3.  
  4. /**
  5.  * Команда "/answer"
  6.  *
  7.  * Ответ на обратную связь
  8.  */
  9. class AnswerCommand extends AdminCommand
  10. {
  11.     protected $name = 'answer';
  12.     protected $description = 'Ответ на обратную связь';
  13.     protected $usage = '/answer';
  14.     protected $version = '1.0.0';
  15.     protected $need_mysql = true;
  16.     protected $private_only = false;
  17.     protected $conversation;
  18.  
  19.     /**
  20.      * Проверка наличия фидбека
  21.      *
  22.      * @param $id
  23.      * @return bool
  24.      */
  25.     public static function getFeedback($id) {
  26.         $pdo = DB::getPdo();
  27.  
  28.         $stmt = $pdo->prepare("SELECT * FROM `feedback` WHERE `id` = :id");
  29.         $stmt->execute([
  30.             'id' => $id
  31.         ]);
  32.  
  33.         return ($stmt->rowCount() == 0 ? false : true);
  34.     }
  35.  
  36.     /**
  37.      * @return \Longman\TelegramBot\Entities\ServerResponse
  38.      * @throws \Longman\TelegramBot\Exception\TelegramException
  39.      */
  40.     public function execute()
  41.     {
  42.         $message = $this->getMessage();
  43.  
  44.         $chat    = $message->getChat();
  45.         $chat_id = $message->getChat()->getId();
  46.         $command = $message->getCommand();
  47.         $from = $message->getFrom();
  48.         $user_id = $from->getId();
  49.         $text    = trim($message->getText(true));
  50.  
  51.         $pdo = DB::getPdo();
  52.  
  53.         $data = [
  54.             'chat_id' => $chat_id,
  55.             'reply_markup' => Keyboard::remove([]),
  56.             'parse_mode' => 'Markdown'
  57.         ];
  58.  
  59.         /**
  60.          * Если не в админ чате используется команда
  61.          */
  62.         if ($chat_id !== -1001250405637) {
  63.             $data['text'] = 'Хм.';
  64.  
  65.             return Request::sendMessage($data);
  66.         }
  67.  
  68.         $this->conversation = new Conversation($user_id, $chat_id, $this->getName());
  69.         $notes = &$this->conversation->notes;
  70.         !is_array($notes) && $notes = [];
  71.  
  72.         $state = 0;
  73.         if (isset($notes['state'])) {
  74.             $state = $notes['state'];
  75.         }
  76.  
  77.         if ($command !== 'answer') {
  78.             $state = 1;
  79.             $notes['id'] = substr($command, 6);
  80. //            $notes['id'] = $text;
  81.         }
  82.  
  83.         $result = Request::emptyResponse();
  84.  
  85.         switch ($state) {
  86.             case 0:
  87.                 if ($text === '' || !is_numeric($text) || self::getFeedback($text) == false) {
  88.                     $notes['state'] = 0;
  89.                     $this->conversation->update();
  90.  
  91.                     $data['text'] = "Укажите ID фидбека";
  92.  
  93.                     if ($text !== '') {
  94.                         $data['text'] = "Укажите ID фидбека";
  95.                     }
  96.  
  97.                     $result = Request::sendMessage($data);
  98.                     break;
  99.                 }
  100.  
  101.                 $notes['id'] = $text;
  102.                 $text          = '';
  103.  
  104.             case 1:
  105.                 if ($text === '') {
  106.                     $notes['state'] = 1;
  107.                     $this->conversation->update();
  108.  
  109.                     $data['text'] = "Feedback ID: {$notes['id']} / Напишите ответ.";
  110.  
  111.                     if ($text !== '') {
  112.                         $data['text'] = "Напишите ответ на фидбек.";
  113.                     }
  114.  
  115.                     $result = Request::sendMessage($data);
  116. //                    break;
  117.                 }
  118.  
  119.                 $notes['answer'] = $text;
  120.                 $text          = '';
  121.  
  122.             case 2:
  123.                 $this->conversation->update();
  124.                 unset($notes['state']);
  125.  
  126.                 $data['text'] = "Feedback: {$notes['id']} / Answer: {$notes['answer']} ✅ Сообщение успешно отправлено!";
  127.  
  128.                 $answer = "📫 *Сообщение от администрации!*\n\n" .
  129.                     "{$notes['answer']}\n\n" .
  130.                     "_С уважением, команда CryptoDoggies_";
  131.  
  132.                 $getQuestion = $pdo->prepare("SELECT * FROM `feedback` WHERE `id` = :id");
  133.                 $getQuestion->execute([
  134.                     'id' => $notes['id'],
  135.                 ]);
  136.                 $question = $getQuestion->fetch();
  137.  
  138.                 Request::sendMessage([
  139.                     'chat_id' => $question['user_id'],
  140.                     'text' => $answer,
  141.                     'parse_mode' => 'Markdown',
  142.                 ]);
  143.  
  144.                 $stmt = $pdo->prepare("UPDATE `feedback` SET `answer` = :answer WHERE `id` = :id");
  145.                 $stmt->execute([
  146.                     'answer' => $notes['answer'],
  147.                     'id' => $notes['id']
  148.                 ]);
  149.                 $this->conversation->stop();
  150.  
  151.                 $result = Request::sendMessage($data);
  152.                 break;
  153.         }
  154.  
  155.         return $result;
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement