TBot/bot.php
jkhsjdhjs 2e9d7cca51
migrate to postgres
add getID3 as a submodule
2019-09-18 20:08:06 +00:00

293 lines
8.6 KiB
PHP
Executable File

<?php
class Bot {
const api = "https://api.telegram.org/bot";
private $token = "";
private $webhook = "";
private $validIDs = null;
private $psql_user = null;
private $psql_pw = null;
private $psql_db = null;
public function __construct($token, $webhook = "", $validIDs = null, $psqlUser = null, $psqlPw = null, $psqlDb = null) {
$this->setToken($token);
$this->setWebhookURL($webhook);
$this->validIDs = $validIDs;
$this->psql_user = $psqlUser;
$this->psql_pw = $psqlPw;
$this->psql_db = $psqlDb;
}
public function setToken($token) {
$this->token = $token;
}
public function setWebhookURL($webhook) {
$this->webhook = $webhook;
}
public function setWebhook() {
$this->curlRequest("setWebhook", ["url" => $this->webhook]);
}
public function deleteWebhook() {
$this->curlRequest("setWebhook", ["url" => ""]);
}
public function addID($id) {
array_push($this->validIDs, $id);
}
public function setPsqlUser($user) {
$this->psql_user = $user;
}
public function setPsqlPw($pw) {
$this->psql_pw = $pw;
}
public function setPsqlDb($db) {
$this->psql_db = $db;
}
private function psql_connect() {
$conn_string = "";
if($this->psql_user !== null)
$conn_string .= "user=$this->psql_user ";
if($this->psql_pw !== null)
$conn_string .= "password=$this->psql_pw ";
if($this->psql_db !== null)
$conn_string .= "dbname= $this->psql_db ";
return pg_connect($conn_string);
}
private function psql_check_file($filename) {
$link = $this->psql_connect();
$res = pg_query_params($link, "SELECT file_id FROM files WHERE file = $1 LIMIT 1", [$filename]);
pg_close($link);
if(pg_num_rows($res) === 0)
return false;
$res = pg_fetch_object($res);
return $res->file_id;
}
private function psql_add_file($filename, $file_id) {
$link = $this->psql_connect();
pg_query_params($link, "INSERT INTO files (file, file_id) VALUES ($1, $2)", [$filename, $file_id]);
pg_close($link);
}
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 = [];
}
else if (!is_array($parameters)) {
error_log("Parameters must be an array\n");
return false;
}
$parameters["method"] = $method;
$options = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 30,
CURLOPT_POSTFIELDS => json_encode($parameters),
CURLOPT_HTTPHEADER => ["Content-Type: application/json"]
];
$ch = curl_init(Bot::api . $this->token . "/");
curl_setopt_array($ch, $options);
var_dump(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->psql_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 = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $parameters,
CURLOPT_HTTPHEADER => ["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->psql_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 = [
"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 = [
"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 = [
"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 = [
"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 = [
"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 "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 = [
"chat_id" => $chat_id,
"action" => $action
];
$this->curlRequest("sendChatAction", $parameters);
}
}
?>