Skip to content

Instantly share code, notes, and snippets.

@AmirSbss
Last active October 20, 2022 16:28
Show Gist options
  • Save AmirSbss/c20cc0cac1ddef2321809ad0b7c0bdab to your computer and use it in GitHub Desktop.
Save AmirSbss/c20cc0cac1ddef2321809ad0b7c0bdab to your computer and use it in GitHub Desktop.
Request to telegram API asynchronous from php (2 times faster than normal request)
<?php
/**
* Created by PhpStorm.
* User: amirsbss
* Date: 11/3/18
* Time: 1:08 AM
*/
define('BOT_TOKEN', 'TOKEN');
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
function multipartFormData($data=[], $files=[])
{
$eol = "\r\n";
// Build test string
$testStr = '';
array_walk_recursive($data, function($item, $key) use (&$testStr, &$eol) {
$testStr .= $key . $eol . $item . $eol;
});
// Get file content type and content. Add to test string
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$fileContent = array();
$fileContentTypes = array();
foreach ($files as $key=>$filename)
{
$fileContent[$key] = file_get_contents($filename);
$fileContentTypes[$key] = finfo_file($finfo, $filename);
$testStr .= $key . $eol . $fileContentTypes[$key] . $eol. $fileContent[$key] . $eol;
}
finfo_close($finfo);
// Find a boundary not present in the test string
$boundaryLen = 6;
$alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
do {
$boundary = '--';
for ($i = 0; $i < $boundaryLen; $i++)
{
$c = rand(0, 61);
$boundary .= $alpha[$c];
}
// Check test string
if (strpos($testStr, $boundary) !== false)
{
$boundary = null;
$boundaryLen++;
}
} while ($boundary === null);
unset($testStr);
// Build data
$rtn = '';
$func = function($data, $baseKey = null) use (&$func, &$boundary, &$eol)
{
$rtn = '';
foreach ($data as $key=>&$value)
{
// Get key
$key = $baseKey === null ? $key : ($baseKey . '[' . $key . ']');
// Add data
if (is_array($value))
$rtn .= $func($value, $key);
else
$rtn .= '--' . $boundary . $eol . 'Content-Disposition: form-data; name="'.$key.'"' . $eol . $eol . $value . $eol;
}
return $rtn;
};
$rtn = $func($data);
// Add files
foreach ($files as $key=>$filename)
{
$rtn .= '--' . $boundary . $eol . 'Content-Disposition: form-data; name="'.$key.'"; filename="'.basename($filename).'"' . $eol
. 'Content-Type: ' . $fileContentTypes[$key] . $eol
. 'Content-Transfer-Encoding: binary' . $eol . $eol
. $fileContent[$key] . $eol;
}
return [$rtn . '--' . $boundary . '--' . $eol, 'multipart/form-data, boundary='.$boundary];
}
function asyncApiRequest($method, $params, $files=[])
{
if (count($files) == 0) {
$post_string = json_encode($params);
$content_type = "application/json";
} else {
list($post_string, $content_type) = multipartFormData($params, $files);
}
$path = "/bot".BOT_TOKEN."/".$method;
$fp = fsockopen("ssl://api.telegram.org",443,$errno, $errstr, 30);
$out = "POST $path HTTP/1.1\r\n";
$out.= "Host: api.telegram.org\r\n";
$out.= "Content-Type: $content_type\r\n";
$out.= "Content-Length: ".strlen($post_string)."\r\n";
$out.= "Connection: Close\r\n\r\n";
if (isset($post_string)) $out.= $post_string;
fwrite($fp, $out);
fclose($fp);
}
// Send normal data (texts)
asyncApiRequest("sendMessage", ["chat_id"=>123456789, "text"=>"Hello!"]);
// Send files
asyncApiRequest("sendDocument", ["chat_id"=>123456789], ["document"=>$filename]);
@ShoteeDevs
Copy link

how do I retrieve the information: CHAT_ID, MESSAGE ID, FIRSTNAME ETC...) using its base structure?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment