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);
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,
]);
Most helpful comment
First off, you were using
fileinstead ofdocumentas the key to set the file to be sent.When sending a file from a local path, use
Request::encodeFile(...);: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 withRequest::encodeFile(...):or just stream it from the URL directly: