Skip to content

Instantly share code, notes, and snippets.

@ammarfaizi2
Created December 27, 2022 15:26
Show Gist options
  • Save ammarfaizi2/42c9612446aa113b7450a092f15b7d87 to your computer and use it in GitHub Desktop.
Save ammarfaizi2/42c9612446aa113b7450a092f15b7d87 to your computer and use it in GitHub Desktop.
<?php
// SPDX-License-Identifier: Public domain
/**
* @author Ammar Faizi <[email protected]>
*
* A simple example for uploading a file via bot Telegram.
*
* Link: https://discord.com/channels/845302963739033611/845302963739033613/1057316446662819860
*/
const BOT_TOKEN = "xxxxxxxxxxxxxxxxxxxxxxxxx";
function curl($url, $opt = [])
{
$ch = curl_init($url);
$opt[CURLOPT_RETURNTRANSFER] = true;
curl_setopt_array($ch, $opt);
$out = curl_exec($ch);
if (!$out) {
$ern = curl_errno($ch);
$err = curl_error($ch);
printf("Curl error: (%d) %s\n", $ern, $err);
}
return $out;
}
function sendDocument($arg)
{
$opt = [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $arg
];
return curl("https://api.telegram.org/bot".BOT_TOKEN."/sendDocument", $opt);
}
$chat_id = -1001226735471;
$a = sendDocument([
"chat_id" => $chat_id,
"caption" => "Ini file a.txt",
"document" => new CurlFile("/tmp/a.txt")
]);
printf("a = %s\n\n", $a);
$b = sendDocument([
"chat_id" => $chat_id,
"caption" => "Ini file b.txt",
"document" => new CurlFile("/tmp/b.txt")
]);
printf("b = %s\n\n", $b);
$c = sendDocument([
"chat_id" => $chat_id,
"caption" => "Ini file c.txt",
"document" => new CurlFile("/tmp/c.txt")
]);
printf("c = %s\n\n", $c);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment