Core: How can i send file from local file or url ?

Created on 23 Mar 2018  路  4Comments  路  Source: php-telegram-bot/core

hi
How can i send file from local file or url ?

my code

            $data = [
                'caption' => 'caption file',
                'chat_id' => $chat_id,
                'file'  => $this->telegram->getUploadPath() . "/" . $file_name
                // 'file' => 'https://ballooncrm.com/assets/img/img_backgroud_footer.png'
            ];

            return Request::sendDocument($data);

Most helpful comment

First off, you were using file instead of document as the key to set the file to be sent.

When sending a file from a local path, use Request::encodeFile(...);:

return Request::sendDocument([
    'caption'  => 'caption file',
    'chat_id'  => $chat_id,
    'document' => Request::encodeFile($this->telegram->getUploadPath() . '/' . $file_name),
]);

Take a look at the official Telegram docs about sending files here:
https://core.telegram.org/bots/api#sending-files

You'll see that only certain file types can be sent with sendDocument. To send any type, either just download it to your server first and then send it the local way with Request::encodeFile(...):

$file_url  = 'https://ballooncrm.com/assets/img/img_backgroud_footer.png';
$file_name = 'balloon.png';
$file_path = $this->getTelegram()->getUploadPath() . '/' . $file_name;

// First download the file locally.
copy($file_url, $file_path);

return Request::sendDocument([
    'caption'  => 'caption file',
    'chat_id'  => $chat_id,
    'document' => Request::encodeFile($file_path),
]);

or just stream it from the URL directly:

$file_url = 'https://ballooncrm.com/assets/img/img_backgroud_footer.png';

return Request::sendDocument([
    'caption'  => 'caption file',
    'chat_id'  => $this->getMessage()->getChat()->getId(),
    'document' => fopen($file_url, 'rb'),
]);

All 4 comments

First off, you were using file instead of document as the key to set the file to be sent.

When sending a file from a local path, use Request::encodeFile(...);:

return Request::sendDocument([
    'caption'  => 'caption file',
    'chat_id'  => $chat_id,
    'document' => Request::encodeFile($this->telegram->getUploadPath() . '/' . $file_name),
]);

Take a look at the official Telegram docs about sending files here:
https://core.telegram.org/bots/api#sending-files

You'll see that only certain file types can be sent with sendDocument. To send any type, either just download it to your server first and then send it the local way with Request::encodeFile(...):

$file_url  = 'https://ballooncrm.com/assets/img/img_backgroud_footer.png';
$file_name = 'balloon.png';
$file_path = $this->getTelegram()->getUploadPath() . '/' . $file_name;

// First download the file locally.
copy($file_url, $file_path);

return Request::sendDocument([
    'caption'  => 'caption file',
    'chat_id'  => $chat_id,
    'document' => Request::encodeFile($file_path),
]);

or just stream it from the URL directly:

$file_url = 'https://ballooncrm.com/assets/img/img_backgroud_footer.png';

return Request::sendDocument([
    'caption'  => 'caption file',
    'chat_id'  => $this->getMessage()->getChat()->getId(),
    'document' => fopen($file_url, 'rb'),
]);

@irvatoDev Did you manage to get it working for you? Can we close off here?

i have file id, what i am doing ?

@enghelab If you have the file ID ($file_id), you can use it directly, like this:

return Request::sendDocument([
    'caption'  => 'file caption',
    'chat_id'  => $this->getMessage()->getChat()->getId(),
    'document' => $file_id,
]);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Zoha picture Zoha  路  3Comments

irmmr picture irmmr  路  3Comments

TheSleepySlee picture TheSleepySlee  路  3Comments

Bl0ck154 picture Bl0ck154  路  3Comments

sineverba picture sineverba  路  3Comments