Core: How to pictures from the bot will automatically download and save?

Created on 8 Jan 2017  路  7Comments  路  Source: php-telegram-bot/core

Required Information

  • PHP version: 5.5.0
  • PHP Telegram Bot version: ^0.38.1
  • Using MySQL database: yes
  • Update Method: Webhook
  • Self-signed certificate: no
  • RAW update (if available):

Expected behaviour

Download and save all picture from channel automatically

Actual behaviour

Need tuning

Steps to reproduce

Extra details

I tried to take advantage of this advice, but this has no effect((

Please, help(((

hook.php
// Set custom Upload and Download path $telegram->setDownloadPath(BASE_PATH . '/var/www/html/download'); $telegram->setUploadPath(BASE_PATH . '/var/www/html/upload');
I tried in different ways, but has no effect

Most helpful comment

Check out #221 again, as it's all there.

@jacklul Since 0.35, ->getType() returns the type in lowercase!

Here the corrected code:

...
if (in_array($message->getType(), ['audio', 'document', 'photo', 'video', 'voice'], true)) {
    $doc = call_user_func([$message, 'get' . ucfirst($message->getType())]);
    ($message->getType() === 'photo') && $doc = $doc[0];
...

All 7 comments

Adding something like this to GenericmessageCommand should keep saving all files sent to the bot:

        $message = $this->getMessage();

        if (in_array(strtolower($message->getType()), ['audio', 'document', 'photo', 'video', 'voice'])) {
            $doc = call_user_method('get' . $message->getType(), $message);
            (strtolower($message->getType()) === 'photo') && $doc = $doc[0];

            $file = Request::getFile(['file_id' => $doc->getFileId()]);
            if ($file->isOk()) {
                Request::downloadFile($file->getResult());
            }
        }

(corrected)

(literally cut code from https://github.com/akalongman/php-telegram-bot/issues/221#issuecomment-224727595)

Hangs on this instruction: $file_id = $doc->getFileId();
((

Define hangs? Script does nothing? Not even an error? Thats weird.

Check out #221 again, as it's all there.

@jacklul Since 0.35, ->getType() returns the type in lowercase!

Here the corrected code:

...
if (in_array($message->getType(), ['audio', 'document', 'photo', 'video', 'voice'], true)) {
    $doc = call_user_func([$message, 'get' . ucfirst($message->getType())]);
    ($message->getType() === 'photo') && $doc = $doc[0];
...

Its worked!!!! Thanks!!!
But very little size
How take full size pic?

The variable $doc is an array of images, starting from smallest to biggest.

So for the biggest version you could just get the last item like so: $doc = end($doc);

(that's also described in the other issue 鈽猴笍)

Thank you very match!!! Its work!!!

Was this page helpful?
0 / 5 - 0 ratings