Longman\TelegramBot\Commands\AdminCommands\SendtochannelCommand::execute PHP Method

execute() public method

Command execute method
public execute ( ) : ServerResponse | mixed
return Longman\TelegramBot\Entities\ServerResponse | mixed
    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;
    }