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.
