setToken($token); $this->setWebhookURL($webhook); $this->validIDs = $validIDs; $this->mysql_user = $mysqlUser; $this->mysql_pw = $mysqlPw; $this->mysql_db = $mysqlDb; } public function setToken($token) { $this->token = $token; } public function setWebhookURL($webhook) { $this->webhook = $webhook; } public function setWebhook() { $this->curlRequest("setWebhook", array("url" => $this->webhook)); } public function deleteWebhook() { $this->curlRequest("setWebhook", array("url" => "")); } public function addID($id) { array_push($this->validIDs, $id); } public function setMysqlUser($user) { $this->mysql_user = $user; } public function setMysqlPw($pw) { $this->mysql_pw = $pw; } public function setMysqlDb($db) { $this->mysql_db = $db; } private function mysql_connect() { return new mysqli("localhost", $this->mysql_user, $this->mysql_pw, $this->mysql_db); } private function mysql_check_file($filename) { $link = $this->mysql_connect(); $res = $link->query("SELECT file_id FROM files WHERE file = '$filename' LIMIT 1"); $link->close(); if($res->num_rows == 0) { return false; } $res = $res->fetch_object(); return $res->file_id; } private function mysql_add_file($filename, $file_id) { $link = $this->mysql_connect(); $link->query("INSERT INTO files (file, file_id) VALUES ('$filename', '$file_id')"); $link->close(); } public function checkID($id) { if(is_null($this->validIDs)) { return true; } foreach($this->validIDs as $idArray) { if($idArray === $id) { return true; } } return false; } private function curlRequest($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; } $parameters["method"] = $method; $options = array( CURLOPT_RETURNTRANSFER => true, CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_TIMEOUT => 30, CURLOPT_POSTFIELDS => json_encode($parameters), CURLOPT_HTTPHEADER => array("Content-Type: application/json") ); $ch = curl_init(Bot::api . $this->token . "/"); curl_setopt_array($ch, $options); curl_exec($ch); curl_close($ch); } private function curlRequestFile($method, $file, $parameters) { if(realpath("files/" . $file) === false) { return false; } $param = strtolower(str_replace("send", "", $method)); $rv = $this->mysql_check_file($file); if($rv !== false) { $parameters[$param] = $rv; $this->curlRequest($method, $parameters); } else { $parameters[$param] = new CURLFile(realpath("files/" . $file)); $parameters["method"] = $method; $options = array( CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => $parameters, CURLOPT_HTTPHEADER => array("Content-Type: multipart/form-data") ); $ch = curl_init(Bot::api . $this->token . "/"); curl_setopt_array($ch, $options); $response = curl_exec($ch); curl_close($ch); $response = json_decode($response, true); if($response["ok"] === true) { $file_id = $response["result"][$param]; if(array_key_exists("file_id", $file_id)) { $file_id = $file_id["file_id"]; } else { $file_id = end($file_id)["file_id"]; } $this->mysql_add_file($file, $file_id); } else { return false; } } } public function sendMessage($chat_id, $text, $reply_to_message_id = null, $parse_mode = null, $disable_web_page_preview = null, $disable_notification = null, $reply_markup = null) { if(!((is_integer($chat_id) || is_string($chat_id)) && is_string($text))) { return false; } $parameters = array( "chat_id" => $chat_id, "text" => $text ); if(is_integer($reply_to_message_id)) { $parameters["reply_to_message_id"] = $reply_to_message_id; } if(is_string($parse_mode)) { $parameters["parse_mode"] = $parse_mode; } if(is_bool($disable_web_page_preview) && $disable_web_page_preview !== false) { $parameters["disable_web_page_preview"] = $disable_web_page_preview; } if(is_bool($disable_notification) && $disable_notification !== false) { $parameters["disable_notification"] = $disable_notification; } if(is_object($reply_markup)) { $parameters["reply_markup"] = $reply_markup; } $this->curlRequest("sendMessage", $parameters); } public function sendPhoto($chat_id, $photo, $reply_to_message_id = null, $caption = null, $disable_notification = null, $reply_markup = null) { if(!((is_integer($chat_id) || is_string($chat_id)) && is_string($photo))) { return false; } $parameters = array( "chat_id" => $chat_id ); if(is_integer($reply_to_message_id)) { $parameters["reply_to_message_id"] = $reply_to_message_id; } if(is_string($caption)) { $parameters["caption"] = $caption; } if(is_bool($disable_notification)) { $parameters["disable_notification"] = $disable_notification; } if(is_object($reply_markup)) { $parameters["reply_markup"] = $reply_markup; } $this->curlRequestFile("sendPhoto", $photo, $parameters); } public function sendDocument($chat_id, $document, $reply_to_message_id = null, $caption = null, $disable_notification = null, $reply_markup = null) { if(!((is_integer($chat_id) || is_string($chat_id)) && is_string($document))) { return false; } $parameters = array( "chat_id" => $chat_id ); if(is_integer($reply_to_message_id)) { $parameters["reply_to_message_id"] = $reply_to_message_id; } if(is_string($caption)) { $parameters["caption"] = $caption; } if(is_bool($disable_notification)) { $parameters["disable_notification"] = $disable_notification; } if(is_object($reply_markup)) { $parameters["reply_markup"] = $reply_markup; } $this->curlRequestFile("sendDocument", $document, $parameters); } public function sendSticker($chat_id, $sticker, $reply_to_message_id = null, $disable_notification = null, $reply_markup = null) { if(!((is_integer($chat_id) || is_string($chat_id)) && is_string($sticker))) { return false; } $parameters = array( "chat_id" => $chat_id ); if(is_integer($reply_to_message_id)) { $parameters["reply_to_message_id"] = $reply_to_message_id; } if(is_bool($disable_notification)) { $parameters["disable_notification"] = $disable_notification; } if(is_object($reply_markup)) { $parameters["reply_markup"] = $reply_markup; } $this->curlRequestFile("sendSticker", $sticker, $parameters); } public function sendVideo($chat_id, $video, $caption = null, $reply_to_message_id = null, $disable_notification = null, $reply_markup = null) { if(!((is_integer($chat_id) || is_string($chat_id)) && is_string($video))) { return false; } $parameters = array( "chat_id" => $chat_id ); if(is_string($caption)) { $parameters["caption"] = $caption; } if(is_integer($reply_to_message_id)) { $parameters["reply_to_message_id"] = $reply_to_message_id; } if(is_bool($disable_notification)) { $parameters["disable_notification"] = $disable_notification; } if(is_object($reply_markup)) { $parameters["reply_markup"] = $reply_markup; } require_once("/var/www/php/getID3/getid3/getid3.php"); $getID3 = new getID3(); $info = $getID3->analyze("files/" . $video); if(isset($info["playtime_seconds"])) { $parameters["duration"] = intval($info["playtime_seconds"]); } if(isset($info["video"]["resolution_x"])) { $parameters["width"] = intval($info["video"]["resolution_x"]); } if(isset($info["video"]["resolution_y"])) { $parameters["height"] = intval($info["video"]["resolution_y"]); } $this->curlRequestFile("sendVideo", $video, $parameters); } public function sendChatAction($chat_id, $action) { if(!((is_integer($chat_id) || is_string($chat_id)) && is_string($action))) { return false; } $parameters = array( "chat_id" => $chat_id, "action" => $action ); $this->curlRequest("sendChatAction", $parameters); } } ?>