Skip to content

Instantly share code, notes, and snippets.

@sh-sh-dev
Last active April 19, 2023 22:24
Show Gist options
  • Save sh-sh-dev/7638092c1b6b5230e669dd455357acdf to your computer and use it in GitHub Desktop.
Save sh-sh-dev/7638092c1b6b5230e669dd455357acdf to your computer and use it in GitHub Desktop.
Simple request bin, works with Telegram Bot
<?php
$botToken = 'x';
$chatId = 1234;
$dontIncludeHeadersName = 'nh';
$separator = PHP_EOL;
$twoSeparator = $separator . $separator;
function bot($method, $data = null) {
global $botToken;
$url = "https://api.telegram.org/bot$botToken/$method";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if (!empty($data))
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
if (curl_error($ch))
return curl_error($ch);
else
return json_decode($result);
}
function extractHeaders() {
$headerList = [];
foreach ($_SERVER as $name => $value) {
if (preg_match('/^HTTP_/',$name)) {
// convert HTTP_HEADER_NAME to Header-Name
$name = strtr(substr($name,5),'_',' ');
$name = ucwords(strtolower($name));
$name = strtr($name,' ','-');
// add to list
$headerList[$name] = $value;
}
}
return $headerList;
}
function readableSize($size, $precision = 2) {
static $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
$step = 1024;
$i = 0;
while (($size / $step) > 0.9) {
$size = $size / $step;
$i++;
}
return round($size, $precision).$units[$i];
}
$dontIncludeHeaders = isset($_GET[$dontIncludeHeadersName]);
$data = sprintf(
"%s %s %s",
$_SERVER['REQUEST_METHOD'],
$dontIncludeHeaders ? preg_replace('/[&?]' . $dontIncludeHeadersName . '[=]?/', '', $_SERVER['REQUEST_URI'], 1) : $_SERVER['REQUEST_URI'],
$_SERVER['SERVER_PROTOCOL']
);
if ($dontIncludeHeaders)
$data .= ' (' . $dontIncludeHeadersName . ')';
else {
$data .= $twoSeparator . '*Headers*' . $twoSeparator;
$headerList = extractHeaders();
foreach ($headerList as $name => $value) {
$data .= $name . ': ' . $value . $separator;
}
}
$body = file_get_contents('php://input');
if (!empty($body))
$data .= $twoSeparator . '*Data*' . $twoSeparator . $body;
if (!empty($_FILES)) {
$files = [];
$fileNames = null;
foreach ($_FILES as $key => $file) {
$files[] = [
'ket' => $key,
'name' => $file['name'],
'size' => $file['size'],
'path' => $file['tmp_name'],
'type' => $file['type']
];
$fileNames .= $file['name'] . ' (' . readableSize($file['size']) . ')' . $separator;
}
$data .= $separator . '*Files* (' . count($files) . ')' . $twoSeparator . $fileNames;
}
$mainMessage = bot('sendMessage', [
'chat_id' => $chatId,
'text' => '`'. $data . '`',
'parse_mode' => 'Markdown'
])->result->message_id;
if (isset($files)) {
foreach ($files as $file) {
$sendFile = bot('sendDocument', [
'chat_id' => $chatId,
'document' => new CURLFile($file['path'], $file['type'], $file['name']),
'reply_to_message_id' => $mainMessage
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment