Core: Upload photo to server after survey

Created on 25 Sep 2017  Â·  14Comments  Â·  Source: php-telegram-bot/core

Hi everyone!
How can I upload photo to my path from hook.php in survey command? I tried to use this:

$result = Request::sendPhoto($data, $this->getTelegram()->getUploadPath() . "/" . $notes['photo_id'] . ".jpg");

But method sendPhoto uses only 1 parameter and my code didn't work.

Thank in advance!

Most helpful comment

$file->getResult()->file_path probably what you're looking for (in relevance with my code example above).

All 14 comments

Look here: https://github.com/php-telegram-bot/core/issues/649#issuecomment-331144955

What version of the bot are you using? Looks like you're using old code :grin:

@noplanman v0.49.0, that example for send photo from disk. I need upload photo to disk :)

Photo shows me in message in message by survey result, but not uploaded to server. The folder on the server is detected correctly

@noplanman here is my code. I tried, but I get nothing :(
Look please:

            case 9:
                if ($message->getPhoto() === null) {
                    $keyboard = $keyboards_nav[0]
                        ->setResizeKeyboard(true)
                        ->setOneTimeKeyboard(true)
                        ->setSelective(false);

                    $notes['state'] = 9;
                    $this->conversation->update();

                    $data['text'] = 'Upload the model photo:';

                    $data['reply_markup'] = $keyboard;
                    $result = Request::sendMessage($data);
                    break;
                }

                $photo = $message->getPhoto()[0];
                $notes['photo_id'] = $photo->getFileId();

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

                $out_text = '✅ Model successefully added!' . PHP_EOL;
                /**
                 * Download the photo to server
                 */
                $file = Request::getFile(['file_id' => $notes['photo_id']]);
                if ($file->isOk()) {
                    Request::downloadFile($file->getResult());
                    $out_text .= 'OK!';
                } else {
                    $out_text .= 'FAILED';
                }
                unset($notes['state']);

                $data['photo']        = $notes['photo'];
                $data['caption']      = $out_text;
                $data['reply_markup'] = Keyboard::remove(['selective' => true]);

                $this->conversation->stop();
                $result = Request::sendPhoto($data);
                break;
        }

Tried to use this in my case 10, but I get nothing again
https://github.com/php-telegram-bot/core/issues/221#issuecomment-224727595

@noplanman Tried to use this - i get a "BAD" error, but logs are empty.

            case 9:
                if ($message->getPhoto() === null) {
                    $keyboard = $keyboards_nav[0]
                        ->setResizeKeyboard(true)
                        ->setOneTimeKeyboard(true)
                        ->setSelective(false);

                    $notes['state'] = 9;
                    $this->conversation->update();

                    $data['text'] = 'Upload the model photo:';

                    $data['reply_markup'] = $keyboard;

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

                /**
                 * Download the photo to server
                 */
                $file = Request::getFile(['file_id' => $message->getPhoto()[0]]);
                if ($file->isOk()) {
                    Request::downloadFile($file->getResult());
                    $msg = [
                        'chat_id' => $chat_id,
                        'text' => 'OK',
                    ];
                    Request::sendMessage($msg);
                } else {
                    $msg = [
                        'chat_id' => $chat_id,
                        'text' => 'BAD',
                    ];
                    Request::sendMessage($msg);
                }

                $photo = $message->getPhoto()[0];
                $notes['photo'] = $photo->getFileId();

            case 10:
                $this->conversation->update();
                unset($notes['state']);

                $out_text = '✅ Model successefully added!' . PHP_EOL;

                $data['photo']        = $notes['photo'];
                $data['caption']      = $out_text;
                $data['reply_markup'] = Keyboard::remove(['selective' => true]);

                $this->conversation->stop();
                $result = Request::sendPhoto($data);
                break;

This is my code when uploading files taken from #221 (thank you @noplanman ), the path is defined in manager.php from telegram-bot-manager

                    $doc = call_user_func([$message, 'get' . ucfirst($message->getType())]);
                    ($message->getType() === 'photo') && $doc = end($doc);
                    $data['reply_markup'] = Keyboard::remove(['selective' => true]);

                    if (!empty($doc) && is_object($doc) && $doc->getFileId()) {
                        $file = Request::getFile(['file_id' => $doc->getFileId()]);
                        if ($file->isOk()) {
                            // save the state or send message
                            $notes['photo_id'] = $file->getResult()->file_id
                        }
                    }

I've used this in my bot and it's working (although there's a bit modification to save to database and accept only some extension in array - not pictured here). Maybe this will work in your case?

notes : did you check the upload file permission? I have to change the owner to www-data or set it to at least writable to other (777 maybe ?) in the directory.

just realized, there are 2 condition where a file doesn't save :

  1. when taking new pictures directly from gallery using camera (but taking same existing pictures after being taken in gallery works).
  2. video notes (tap on the audio record button to switch to video notes recording).

for case 1, there are no file info like file_path, mime_type, etc..., only file_id when I debug it.
for case 2, nothing shown when I debug it, only message from previous conversation.

Other method works flawlessly, including saving audio, video, images, and document from upload document icon, upload from gallery (choose existing files), and record audio notes.

@arkhub @noplanman thank you a lot!!!
I change the my folder permission to 777 and it worked. And last question.
File saved by name "file_15.jpg", how can I get that name in code for insert to db? Or how can I rename the filename?

$file->getResult()->file_path probably what you're looking for (in relevance with my code example above).

@arkhub thank you a lot :)

@arkhub Sorry! But how can I rename the filename before sending it to the database?
It saves by name file_17.jpg but I need to cut symbol "_" (file17.jpg).
Thanks!

Oh, I use markdown and bot think that symbol "_" is beginning of italics.

$message->escapeMarkdown($file_id);

Thanks @noplanman @arkhub :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

esomkin picture esomkin  Â·  3Comments

Zoha picture Zoha  Â·  3Comments

Bl0ck154 picture Bl0ck154  Â·  3Comments

abbe-cipher picture abbe-cipher  Â·  4Comments

sineverba picture sineverba  Â·  3Comments