вторник, 4 октября 2022 г.

send telegram




function t_me($tgText, $files=false, $chats_id = ["-xxx"]){

$token = "11111:aaaaa";
$site = $_SERVER['SERVER_NAME'];//$_SERVER['HTTP_ORIGIN']

foreach ($chats_id as $chat) {
$url = "https://api.telegram.org/bot{$token}/sendMessage";

$arrayQuery = [
"chat_id" => $chat,
"text" => $tgText.$site . $files[0],
"parse_mode" => "html",
"message_thread_id"=> 2//id нужного топика
];

if (is_array($files)) {
if(count($files) == 1) {
$url = 'https://api.telegram.org/bot' . $token . '/sendDocument';
$arrayQuery['document'] = $site . $files[0];
$arrayQuery["caption"] = $tgText;
}else {
$arMedia = array();
$url = 'https://api.telegram.org/bot' . $token . '/sendMediaGroup';
foreach ($files as $key => $path) {
if($key == 0) {
$arMedia[] = array('type' => 'photo', 'media' => $site.$path, "caption" => $tgText, "parse_mode"=>"html");
}else{
$arMedia[] = array('type' => 'photo', 'media' => $site.$path);
}
}
$arrayQuery['media'] = json_encode( $arMedia );
}
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $arrayQuery);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type:multipart/form-data"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$res = curl_exec($ch);
curl_close ($ch);

return $res;
}
}

Комментариев нет:

Отправить комментарий