Advertisement
MyZik

Untitled

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