Codice PHP:
<?php
error_reporting(E_ALL | E_NOTICE | E_STRICT);
ini_set("display_errors", "stdout");
ini_set("display_startup_errors", 1);
define("BOT_TOKEN", "{BOT_TOKEN}");
define("API_URL", "https://api.telegram.org/bot".BOT_TOKEN."/");
function exec_curl_request($handle) {
$response = curl_exec($handle);
if ($response === false) {
$errno = curl_errno($handle);
$error = curl_error($handle);
error_log("Curl returned error {$errno}: {$error}.\n");
curl_close($handle);
return false;
}
$http_code = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE));
curl_close($handle);
$logFile = "log/".date("Ymd");
//$logHandler = fopen($logFile, "a");
//fwrite($logHandler, date("Y-m-d H:i:s P")." - INFO - HTTP code: ".$http_code.".\n");
//fclose($logHandler);
if ($http_code >= 500) {
$logHandler = fopen($logFile, "a");
fwrite($logHandler, date("Y-m-d H:i:s P")." - ERROR - HTTP code: ".$http_code.".\n");
fclose($logHandler);
sleep(10);
return false;
} else if ($http_code != 200) {
$response = json_decode($response, True);
error_log("Request has failed with error {$response["error_code"]}: {$response["description"]}.\n");
$logHandler = fopen($logFile, "a");
fwrite($logHandler, date("Y-m-d H:i:s P")." - ERROR - Request has failed with error {$response["error_code"]}: {$response["description"]}.\n");
fclose($logHandler);
if ($http_code == 401) {
throw new Exception("Invalid access token provided");
}
return false;
} else {
$response = json_decode($response, True);
if (isset($response["description"])) {
error_log("Request was successfull: {$response["description"]}.\n");
}
$response = $response["result"];
}
return $response;
}
function apiRequest($method, $parameters) {
if (!is_string($method)) {
error_log("Method name must be a string.\n");
return false;
}
if (!$parameters) {
$parameters = array();
} else if (!is_array($parameters)) {
error_log("Parameters must be an array.\n");
return false;
}
foreach ($parameters as $key => &$val) {
if (!is_numeric($val) && !is_string($val)) {
$val = json_encode($val);
}
}
$url = API_URL.$method."?".http_build_query($parameters);
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_FAILONERROR, True);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, True);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($handle, CURLOPT_TIMEOUT, 60);
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, False);
return exec_curl_request($handle);
}
function apiRequestImage($type, $parameters) {
if (!$parameters) {
$parameters = array();
} else if (!is_array($parameters)) {
error_log("Parameters must be an array.\n");
return false;
}
$url = API_URL."sendPhoto?chat_id=".$parameters["chat_id"];
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_FAILONERROR, True);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, True);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($handle, CURLOPT_TIMEOUT, 300);
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, False);
//curl_setopt($handle, CURLOPT_SSL_VERIFYSTATUS, False);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, False);
curl_setopt($handle, CURLOPT_POSTFIELDS, $parameters);
switch ($type) {
case "jpeg":
case "jpg":
curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data"));
break;
case "png":
curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data"));
break;
case "gif":
curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data"));
break;
default:
curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data"));
}
return exec_curl_request($handle);
}
$content = file_get_contents("php://input");
$update = json_decode($content, True);
apiRequest("sendMessage", array("chat_id" => "248604001", "text" => "Oh, yeah! ".json_decode('"\uD83D\uDC44"')));
apiRequestImage("png", array("chat_id" => "248604001", "photo" => new CURLFile(realpath("prizes/Wallpaper_Spada_destra_1920x1080.png"))));
apiRequestImage("png", array("chat_id" => "248604001", "photo" => new CURLFile(realpath("prizes/Pagliacci.png"))));
if (!$update) {
exit;
}
if (isset($update["message"])) {
processMessage($update["message"]);
}
?>