Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Telegram Bot sendDocument (php on hosted server)

I have a Telegram bot with webhook setted on an third-party hosted server. I can use any URL query string and they work fine.

Now I'm trying to make my bot send a text file. If I've understood correctly I need to make a POST request using multipart/form-data, and I'm struggling quite a lot to make it work on the hosting server.

$url = "https://api.telegram.org/bot<myToken>/sendDocument?chat_id=$<myId>";
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$post = array( 'document' => '@'.realpath('data.txt'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

$headers = array();
$headers[] = 'Content-Type: multipart/form-data';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

//DEBUGGING-------------------------------
$info = curl_getinfo($ch);
$buffer = "";
foreach ($info as $key => $value) {
    $buffer .= "$key => $value\n";
}
sendMessage($buffer, $<myId>);
//----------------------------------------

$result = curl_exec($ch);

//DEBUGGING-------------------------------
sendMessage($result, $<myId>);
//----------------------------------------

curl_close($ch);

(sendMessage is a function that I use for debugging since I can't use echo on the webhooked php page).

Obviously I don't receive data.txt and the two debug messages are:

url => https://api.telegram.org/bot<myToken>/sendDocument?chat_id=$<myId>
content_type => 
http_code => 0
header_size => 0
request_size => 0
filetime => 0
ssl_verify_result => 0
redirect_count => 0
total_time => 0
namelookup_time => 0
connect_time => 0
pretransfer_time => 0
size_upload => 0
size_download => 0
speed_download => 0
speed_upload => 0
download_content_length => -1
upload_content_length => -1
starttransfer_time => 0
redirect_time => 0
redirect_url =>
----------------------------------------------------------------------------
{"ok":false,"error_code":400,"description":"Bad Request: URL host is empty"}

I've also unsuccessfully tried to make a CURLFile... It seems harder than he needs to, considerig that I can do it very easily locally on my machine.

like image 431
Domenico Modica Avatar asked Mar 29 '26 01:03

Domenico Modica


1 Answers

  • No need to set the content headers

To send a local file, create a new CURLFile(), and add that to the CURL request;

<?php

    CONST CHAT_ID = '~~';
    CONST BOT = '~~';

    CONST FILENAME = './data.txt';

    // Create CURL object
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://api.telegram.org/bot".BOT."/sendDocument?chat_id=" . CHAT_ID);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);

    // Create CURLFile
    $finfo = finfo_file(finfo_open(FILEINFO_MIME_TYPE), FILENAME);
    $cFile = new CURLFile(FILENAME, $finfo);

    // Add CURLFile to CURL request
    curl_setopt($ch, CURLOPT_POSTFIELDS, [
        "document" => $cFile
    ]);

    // Call
    $result = curl_exec($ch);

    // Show result and close curl
    var_dump($result);
    curl_close($ch);

enter image description here

like image 165
0stone0 Avatar answered Apr 02 '26 04:04

0stone0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!