Core: Multiply upload

Created on 15 Feb 2019  路  6Comments  路  Source: php-telegram-bot/core

Hi everyone.
I made this script:

<?php

...

class UploadCommand extends UserCommand
{

    ...

    public function execute()
    {
        $message = $this->getMessage();
        $chat    = $message->getChat();
        $chat_id = $chat->getId();
        $user_id = $message->getFrom()->getId();
        $text    = trim($message->getText(true));

        $update = json_decode($this->update->toJson(), true);

        $pdo = DB::getPdo();

        $keyboard = new Keyboard(
            ['馃敊 Cancel']
        );

        $data = [
            'chat_id' => $chat_id,
            'reply_markup' => $keyboard->setResizeKeyboard(true)->setOneTimeKeyboard(false)->setSelective(false),
            'parse_mode' => 'Markdown',
        ];

        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();

        switch ($state) {
            case 0:
                if ($message->getPhoto() === null) {
                    $notes['state'] = 0;
                    $this->conversation->update();

                    $data['text'] = '*Upload your picture.*';

                    $result = Request::sendMessage($data);
                    break;
                }

                    $doc = call_user_func([$message, 'get' . ucfirst($message->getType())]);
                    ($message->getType() === 'photo') && $doc = end($doc);
                    $notes['photo_id'] = $doc->getFileId();

            case 1:
                $this->conversation->update();

                $file = Request::getFile(['file_id' => $notes['photo_id']]);

                if ($file->isOk()) {
                    Request::downloadFile($file->getResult());

                    $data['text'] = "Your fileID is {$notes['photo_id']}";
                } else {
                    $msg = [
                        'chat_id' => $chat_id,
                        'text' => 'Upload error!',
                    ];
                    Request::sendMessage($msg);
                }

                $this->conversation->stop();
                Request::sendMessage($data);

                $update['message']['text'] = '/start';
                $result = (new StartCommand($this->telegram, new Update($update)))->preExecute();
                break;
        }

        return $result;
    }
}

And I want to upgrade my script. So, if I send 5 photos for one, I need to upload all 5 photos and get ID of files with reply from bot. Is it possible? It seems to me, that I need to remove a Conversation-structure from Command, but I don't know, how to implement it correctly?

Thanks in advance!

All 6 comments

You mean sending 5 photos in 1 message?

Your bot will still receive individual updates for each one. You'll have to link them together in the conversation notes for example.

Hi, @noplanman , thanks for your answer.

I mean more than 1 photo. 2, 4, 10...
We can send 1 album with 5 photos or send 5 photos without grouping. But I can not understand, how can I catch it. So it's an example:
screenshot_71

Your conversation needs to stay in the same state until the user sends some kind of "Finished" message.

You could add a message after each link saying that you can send next image to generate new link or press /done to finish. Could also use callback buttons here.

@noplanman thank you! I made it so and it works!

<?php

...

class UploadCommand extends UserCommand
{

    ...

    public function execute()
    {
        $message = $this->getMessage();
        $chat    = $message->getChat();
        $chat_id = $chat->getId();
        $user_id = $message->getFrom()->getId();
        $text    = trim($message->getText(true));

        $update = json_decode($this->update->toJson(), true);

        $pdo = DB::getPdo();

        $keyboard = new Keyboard(
            ['馃敊 Cancel']
        );

        $data = [
            'chat_id' => $chat_id,
            'reply_markup' => $keyboard->setResizeKeyboard(true)->setOneTimeKeyboard(false)->setSelective(false),
            'parse_mode' => 'Markdown',
        ];

        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();

        switch ($state) {
            case 0:
                if ($message->getPhoto() === null) {
                    $notes['state'] = 0;
//                    $this->conversation->update();

                    $data['text'] = '*Upload your picture(-s)*';

                    if ($text === '馃敊 Cancel') {
                        $notes['state'] = 0;
                        $this->conversation->stop();

                        $update['message']['text'] = '/start';
                        $result = (new StartCommand($this->telegram, new Update($update)))->execute();
                        break;
                    }

//                    $result = Request::sendMessage($data);
                    Request::sendMessage($data);
//                    break;
                }

                $doc = call_user_func([$message, 'get' . ucfirst($message->getType())]);
                ($message->getType() === 'photo') && $doc = end($doc);
                $notes['photo_id'] = $doc->getFileId();

                $file = Request::getFile(['file_id' => $notes['photo_id']]);

                if ($file->isOk()) {
                    Request::downloadFile($file->getResult());

                    Request::sendMessage([
                        'chat_id' => $chat_id,
                        'reply_to_message_id' => $message->getMessageId(),
                        'disable_web_page_preview' => true,
                        'text' => "Your fileID is {$notes['photo_id']}",
                    ]);

                } else {
                    $msg = [
                        'chat_id' => $chat_id,
                        'text' => 'Upload error!',
                    ];
                    Request::sendMessage($msg);
                }
        }

        return $result;
    }
}

Great @MyZik

By the way, I would encourage you to use the available reply helpers, to make your code simpler and less error-prone:

// instead of this:
$msg = [
    'chat_id' => $chat_id,
    'text' => 'Upload error!',
];
Request::sendMessage($msg);

// use this:
$this->replyToChat('Upload error!');
Was this page helpful?
0 / 5 - 0 ratings

Related issues

noplanman picture noplanman  路  3Comments

irmmr picture irmmr  路  3Comments

nesttle picture nesttle  路  4Comments

abbe-cipher picture abbe-cipher  路  4Comments

sineverba picture sineverba  路  3Comments