/**
  * Command execute method
  *
  * @return mixed
  * @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();
     //Preparing Response
     $data = ['chat_id' => $chat_id];
     if ($chat->isGroupChat() || $chat->isSuperGroup()) {
         //reply to message id is applied by default
         //Force reply is applied by default so it can work with privacy on
         $data['reply_markup'] = Keyboard::forceReply(['selective' => true]);
     }
     //Conversation start
     $this->conversation = new Conversation($user_id, $chat_id, $this->getName());
     $notes =& $this->conversation->notes;
     !is_array($notes) && ($notes = []);
     //cache data from the tracking session if any
     $state = 0;
     if (isset($notes['state'])) {
         $state = $notes['state'];
     }
     $result = Request::emptyResponse();
     //State machine
     //Entrypoint of the machine state if given by the track
     //Every time a step is achieved the track is updated
     switch ($state) {
         case 0:
             if ($text === '') {
                 $notes['state'] = 0;
                 $this->conversation->update();
                 $data['text'] = 'Type your name:';
                 $data['reply_markup'] = Keyboard::remove(['selective' => true]);
                 $result = Request::sendMessage($data);
                 break;
             }
             $notes['name'] = $text;
             $text = '';
             // no break
         // no break
         case 1:
             if ($text === '') {
                 $notes['state'] = 1;
                 $this->conversation->update();
                 $data['text'] = 'Type your surname:';
                 $result = Request::sendMessage($data);
                 break;
             }
             $notes['surname'] = $text;
             $text = '';
             // no break
         // no break
         case 2:
             if ($text === '' || !is_numeric($text)) {
                 $notes['state'] = 2;
                 $this->conversation->update();
                 $data['text'] = 'Type your age:';
                 if ($text !== '') {
                     $data['text'] = 'Type your age, must be a number:';
                 }
                 $result = Request::sendMessage($data);
                 break;
             }
             $notes['age'] = $text;
             $text = '';
             // no break
         // no break
         case 3:
             if ($text === '' || !in_array($text, ['M', 'F'], true)) {
                 $notes['state'] = 3;
                 $this->conversation->update();
                 $data['reply_markup'] = (new Keyboard(['M', 'F']))->setResizeKeyboard(true)->setOneTimeKeyboard(true)->setSelective(true);
                 $data['text'] = 'Select your gender:';
                 if ($text !== '') {
                     $data['text'] = 'Select your gender, choose a keyboard option:';
                 }
                 $result = Request::sendMessage($data);
                 break;
             }
             $notes['gender'] = $text;
             // no break
         // no break
         case 4:
             if ($message->getLocation() === null) {
                 $notes['state'] = 4;
                 $this->conversation->update();
                 $data['reply_markup'] = (new Keyboard((new KeyboardButton('Share Location'))->setRequestLocation(true)))->setOneTimeKeyboard(true)->setResizeKeyboard(true)->setSelective(true);
                 $data['text'] = 'Share your location:';
                 $result = Request::sendMessage($data);
                 break;
             }
             $notes['longitude'] = $message->getLocation()->getLongitude();
             $notes['latitude'] = $message->getLocation()->getLatitude();
             // no break
         // no break
         case 5:
             if ($message->getPhoto() === null) {
                 $notes['state'] = 5;
                 $this->conversation->update();
                 $data['text'] = 'Insert your picture:';
                 $result = Request::sendMessage($data);
                 break;
             }
             /** @var PhotoSize $photo */
             $photo = $message->getPhoto()[0];
             $notes['photo_id'] = $photo->getFileId();
             // no break
         // no break
         case 6:
             if ($message->getContact() === null) {
                 $notes['state'] = 6;
                 $this->conversation->update();
                 $data['reply_markup'] = (new Keyboard((new KeyboardButton('Share Contact'))->setRequestContact(true)))->setOneTimeKeyboard(true)->setResizeKeyboard(true)->setSelective(true);
                 $data['text'] = 'Share your contact information:';
                 $result = Request::sendMessage($data);
                 break;
             }
             $notes['phone_number'] = $message->getContact()->getPhoneNumber();
             // no break
         // no break
         case 7:
             $this->conversation->update();
             $out_text = '/Survey result:' . PHP_EOL;
             unset($notes['state']);
             foreach ($notes as $k => $v) {
                 $out_text .= PHP_EOL . ucfirst($k) . ': ' . $v;
             }
             $data['photo'] = $notes['photo_id'];
             $data['reply_markup'] = Keyboard::remove(['selective' => true]);
             $data['caption'] = $out_text;
             $this->conversation->stop();
             $result = Request::sendPhoto($data);
             break;
     }
     return $result;
 }
 /**
  * {@inheritdoc}
  */
 public function execute()
 {
     $chat_id = $this->getMessage()->getChat()->getId();
     $data = ['chat_id' => $chat_id, 'text' => 'Keyboard Hidden', 'reply_markup' => Keyboard::hide()];
     return Request::sendMessage($data);
 }
 public function testKeyboardAddRows()
 {
     $keyboard_obj = new Keyboard([]);
     $keyboard_obj->addRow('Button Text 1');
     $keyboard = $keyboard_obj->getProperty('keyboard');
     self::assertSame('Button Text 1', $keyboard[0][0]->getText());
     $keyboard_obj->addRow('Button Text 2', 'Button Text 3');
     $keyboard = $keyboard_obj->getProperty('keyboard');
     self::assertSame('Button Text 2', $keyboard[1][0]->getText());
     self::assertSame('Button Text 3', $keyboard[1][1]->getText());
     $keyboard_obj->addRow(['text' => 'Button Text 4']);
     $keyboard = $keyboard_obj->getProperty('keyboard');
     self::assertSame('Button Text 4', $keyboard[2][0]->getText());
 }
 /**
  * Command execute method
  *
  * @return \Longman\TelegramBot\Entities\ServerResponse|mixed
  * @throws \Longman\TelegramBot\Exception\TelegramException
  */
 public function execute()
 {
     $message = $this->getMessage();
     $chat_id = $message->getChat()->getId();
     $user_id = $message->getFrom()->getId();
     $type = $message->getType();
     // 'Cast' the command type into message to protect the machine state
     // if the commmad is recalled when the conversation is already started
     in_array($type, ['command', 'text'], true) && ($type = 'message');
     $text = trim($message->getText(true));
     $text_yes_or_no = $text === 'Yes' || $text === 'No';
     $data = ['chat_id' => $chat_id];
     // Conversation
     $this->conversation = new Conversation($user_id, $chat_id, $this->getName());
     $notes =& $this->conversation->notes;
     !is_array($notes) && ($notes = []);
     $channels = (array) $this->getConfig('your_channel');
     if (isset($notes['state'])) {
         $state = $notes['state'];
     } else {
         $state = count($channels) === 0 ? -1 : 0;
         $notes['last_message_id'] = $message->getMessageId();
     }
     switch ($state) {
         case -1:
             // getConfig has not been configured asking for channel to administer
             if ($type !== 'message' || $text === '') {
                 $notes['state'] = -1;
                 $this->conversation->update();
                 $data['text'] = 'Insert the channel name: (@yourchannel)';
                 $data['reply_markup'] = Keyboard::remove(['selective' => true]);
                 $result = Request::sendMessage($data);
                 break;
             }
             $notes['channel'] = $text;
             $notes['last_message_id'] = $message->getMessageId();
             // Jump to state 1
             goto insert;
             // no break
         // no break
         default:
         case 0:
             // getConfig has been configured choose channel
             if ($type !== 'message' || !in_array($text, $channels, true)) {
                 $notes['state'] = 0;
                 $this->conversation->update();
                 $keyboard = [];
                 foreach ($channels as $channel) {
                     $keyboard[] = [$channel];
                 }
                 $data['reply_markup'] = new Keyboard(['keyboard' => $keyboard, 'resize_keyboard' => true, 'one_time_keyboard' => true, 'selective' => true]);
                 $data['text'] = 'Select a channel from the keyboard:';
                 $result = Request::sendMessage($data);
                 break;
             }
             $notes['channel'] = $text;
             $notes['last_message_id'] = $message->getMessageId();
             // no break
         // no break
         case 1:
             insert:
             if ($type === 'message' && $text === '' || $notes['last_message_id'] === $message->getMessageId()) {
                 $notes['state'] = 1;
                 $this->conversation->update();
                 $data['reply_markup'] = Keyboard::remove(['selective' => true]);
                 $data['text'] = 'Insert the content you want to share: text, photo, audio...';
                 $result = Request::sendMessage($data);
                 break;
             }
             $notes['last_message_id'] = $message->getMessageId();
             $notes['message'] = $message->getRawData();
             $notes['message_type'] = $type;
             // no break
         // no break
         case 2:
             if (!$text_yes_or_no || $notes['last_message_id'] === $message->getMessageId()) {
                 $notes['state'] = 2;
                 $this->conversation->update();
                 // Execute this just with object that allow caption
                 if (in_array($notes['message_type'], ['video', 'photo'], true)) {
                     $data['reply_markup'] = new Keyboard(['keyboard' => [['Yes', 'No']], 'resize_keyboard' => true, 'one_time_keyboard' => true, 'selective' => true]);
                     $data['text'] = 'Would you like to insert a caption?';
                     if (!$text_yes_or_no && $notes['last_message_id'] !== $message->getMessageId()) {
                         $data['text'] .= PHP_EOL . 'Type Yes or No';
                     }
                     $result = Request::sendMessage($data);
                     break;
                 }
             }
             $notes['set_caption'] = $text === 'Yes';
             $notes['last_message_id'] = $message->getMessageId();
             // no break
         // no break
         case 3:
             if ($notes['set_caption'] && ($notes['last_message_id'] === $message->getMessageId() || $type !== 'message')) {
                 $notes['state'] = 3;
                 $this->conversation->update();
                 $data['text'] = 'Insert caption:';
                 $data['reply_markup'] = Keyboard::remove(['selective' => true]);
                 $result = Request::sendMessage($data);
                 break;
             }
             $notes['last_message_id'] = $message->getMessageId();
             $notes['caption'] = $text;
             // no break
         // no break
         case 4:
             if (!$text_yes_or_no || $notes['last_message_id'] === $message->getMessageId()) {
                 $notes['state'] = 4;
                 $this->conversation->update();
                 $data['text'] = 'Message will look like this:';
                 $result = Request::sendMessage($data);
                 if ($notes['message_type'] !== 'command') {
                     if ($notes['set_caption']) {
                         $data['caption'] = $notes['caption'];
                     }
                     $this->sendBack(new Message($notes['message'], $this->telegram->getBotName()), $data);
                     $data['reply_markup'] = new Keyboard(['keyboard' => [['Yes', 'No']], 'resize_keyboard' => true, 'one_time_keyboard' => true, 'selective' => true]);
                     $data['text'] = 'Would you like to post it?';
                     if (!$text_yes_or_no && $notes['last_message_id'] !== $message->getMessageId()) {
                         $data['text'] .= PHP_EOL . 'Type Yes or No';
                     }
                     $result = Request::sendMessage($data);
                 }
                 break;
             }
             $notes['post_message'] = $text === 'Yes';
             $notes['last_message_id'] = $message->getMessageId();
             // no break
         // no break
         case 5:
             $data['reply_markup'] = Keyboard::remove(['selective' => true]);
             if ($notes['post_message']) {
                 $data['text'] = $this->publish(new Message($notes['message'], $this->telegram->getBotName()), $notes['channel'], $notes['caption']);
             } else {
                 $data['text'] = 'Abort by user, message not sent..';
             }
             $this->conversation->stop();
             $result = Request::sendMessage($data);
     }
     return $result;
 }
 /**
  * {@inheritdoc}
  */
 public function execute()
 {
     $chat_id = $this->getMessage()->getChat()->getId();
     $data = ['chat_id' => $chat_id, 'text' => 'Write something:', 'reply_markup' => Keyboard::forceReply()];
     return Request::sendMessage($data);
 }
 /**
  * Remove the keyboard and output a text
  *
  * @param string $text
  *
  * @return \Longman\TelegramBot\Entities\ServerResponse
  * @throws \Longman\TelegramBot\Exception\TelegramException
  */
 private function removeKeyboard($text)
 {
     return Request::sendMessage(['reply_markup' => Keyboard::remove(['selective' => true]), 'chat_id' => $this->getMessage()->getChat()->getId(), 'text' => $text]);
 }