Input API Token:
"; 54 | echo "Example: 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11
"; 55 | echo ""; 56 | 57 | } elseif (isset($_POST["connections"])) { 58 | 59 | $api = $_POST["api"]; 60 | $link = strip_tags($_POST["link"]); 61 | $connections = $_POST["connections"]; 62 | $link = $link . "?api=" . $api; 63 | 64 | $client = new Client($api); 65 | 66 | $responseWebhook = $client->setWebhook($link, null, null, $connections); 67 | $response = $client->getMe(); 68 | 69 | if (($responseWebhook->description == "Webhook was set" or $responseWebhook->description == "Webhook is already set") and $response->ok == true) { 70 | $username = $response->result->username; 71 | echo "Setup successful: @" . $username . ""; 72 | } else { 73 | echo "Setup failed: API TOKEN wrong or impossible to connect to Telegram"; 74 | echo "" . htmlspecialchars($response->result->description) . "
"; 75 | echo "Click here to try the setup again:
"; 76 | } 77 | 78 | } elseif (isset($_POST["api"])) { 79 | 80 | $api = $_POST["api"]; 81 | $link = strip_tags($_POST["link"]); 82 | 83 | echo ""; 84 | echo ""; 85 | echo "Input max connections:
"; 86 | echo "Maximum allowed number of simultaneous HTTPS connections to the webhook for update delivery, 1-100. Defaults of Bot APIs is 40. Use lower values to limit the load on your bot‘s server, and higher values to increase your bot’s throughput.
"; 87 | echo ""; 88 | echo "
"; 89 | 90 | } elseif (isset($_POST["no"])) { 91 | 92 | echo "Input Link:
"; 93 | echo "HTTPS link is required!
"; 94 | echo "Example: https://mysite.com/bot/index.php
"; 95 | echo ""; 96 | 97 | } else { 98 | 99 | $actual_link = "https://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]; 100 | $explode = explode("setupWebhook.php", $actual_link); 101 | 102 | echo "Is the link correct?
"; 103 | echo "" . htmlspecialchars($explode[0]) . "webhook.php
"; 104 | echo ""; 105 | echo "
"; 106 | 107 | } 108 | 109 | echo ""; 110 | -------------------------------------------------------------------------------- /examples/webhook.php: -------------------------------------------------------------------------------- 1 | getUpdate(); 15 | if (!isset($update)) { 16 | exit('json error'); 17 | } 18 | 19 | if (isset($update->message) or isset($update->edited_message)) { 20 | $chat_id = $client->easy->chat_id; 21 | $message_id = $client->easy->message_id; 22 | $text = $client->easy->text; 23 | 24 | if ($text === "/start") { 25 | $client->sendMessage($chat_id, "ping"); 26 | $client->sendMessage($chat_id, "pong"); 27 | $client->forwardMessage($chat_id, $chat_id, $message_id); 28 | // you need to put your own .png file in the same directory as this file 29 | $client->sendPhoto($chat_id, $client->inputFile("TuriPixel.png"), caption: "File upload"); 30 | } 31 | 32 | if ($text === "/help") { 33 | 34 | $menu["inline_keyboard"] = [ 35 | [ 36 | [ 37 | "text" => "Button 1", 38 | "callback_data" => "btn1", 39 | ], 40 | ], 41 | [ 42 | [ 43 | "text" => "Button 2", 44 | "callback_data" => "btn2", 45 | ], 46 | [ 47 | "text" => "Button 3", 48 | "callback_data" => "btn3", 49 | ], 50 | ], 51 | ]; 52 | 53 | $client->sendMessage($chat_id, "help", reply_markup: $menu); 54 | } 55 | 56 | if ($text === "/var") { 57 | $client->debug($chat_id, $client->easy->message_id, ["key" => "value"], "pong", 3.14); 58 | } 59 | } 60 | 61 | if (isset($update->callback_query)) { 62 | 63 | $id = $update->callback_query->id; 64 | $message_chat_id = $update->callback_query->message->chat->id; 65 | $message_message_id = $update->callback_query->message->message_id; 66 | 67 | $menu["inline_keyboard"] = [ 68 | [ 69 | [ 70 | "text" => "Button 1", 71 | "callback_data" => "btn1", 72 | ], 73 | ], 74 | [ 75 | [ 76 | "text" => "Button 2", 77 | "callback_data" => "btn2", 78 | ], 79 | [ 80 | "text" => "Button 3", 81 | "callback_data" => "btn3", 82 | ], 83 | ], 84 | ]; 85 | 86 | if ($update->callback_query->data === "btn1") { 87 | $client->answerCallbackQuery($id, "Button 1"); 88 | $client->editMessageText("Button 1", $message_chat_id, $message_message_id, reply_markup: $menu); 89 | } elseif ($update->callback_query->data === "btn2") { 90 | $client->answerCallbackQuery($id, "Button 2"); 91 | $client->editMessageText("Button 2", $message_chat_id, $message_message_id, reply_markup: $menu); 92 | } elseif ($update->callback_query->data === "btn3") { 93 | $client->answerCallbackQuery($id, "Button 3"); 94 | $client->editMessageText("Button 3", $message_chat_id, $message_message_id, reply_markup: $menu); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/ApiInterface.php: -------------------------------------------------------------------------------- 1 | endpoint = $endpoint . $token . "/"; 30 | $this->json_payload = $json_payload; 31 | $this->curl = curl_init(); 32 | 33 | curl_setopt_array($this->curl, [ 34 | CURLOPT_RETURNTRANSFER => true, 35 | CURLOPT_POST => true, 36 | CURLOPT_FORBID_REUSE => true, 37 | CURLOPT_HEADER => false, 38 | CURLOPT_TIMEOUT => 120, 39 | CURLOPT_HTTPHEADER => ["Connection: Keep-Alive", "Keep-Alive: 120"], 40 | ]); 41 | 42 | if (!empty($curl_options)) { 43 | curl_setopt_array($this->curl, $curl_options); 44 | } 45 | } 46 | 47 | 48 | /* 49 | * @return \stdClass of update received from webhook 50 | */ 51 | 52 | public function getUpdate(): ?\stdClass { 53 | $update = json_decode(file_get_contents("php://input")); 54 | $this->easy = new EasyVars($update); 55 | 56 | return $update; 57 | } 58 | 59 | 60 | /* 61 | * @param string $path Path of file 62 | * @return \CURLFile of $path 63 | */ 64 | 65 | public function inputFile(string $path): \CURLFile { 66 | $path = realpath($path); 67 | 68 | return new CURLFile($path); 69 | } 70 | 71 | 72 | /* 73 | * Make a request to Bot API 74 | * 75 | * @param string $method The method of Bot API 76 | * @param array $args Argument for the method of Bot API 77 | * 78 | * @return \stdClass getUpdate if jsonPayload, otherwise response of Telegram 79 | */ 80 | 81 | public function Request(string $method, array $args = []): \stdClass { 82 | if ($this->json_payload) { 83 | $args["method"] = $method; 84 | $request = json_encode($args); 85 | ob_start(); 86 | header("Content-Type: application/json"); 87 | header("Connection: close"); 88 | header("Content-Length: " . strlen($request)); 89 | echo $request; 90 | ob_end_flush(); 91 | ob_flush(); 92 | flush(); 93 | $this->json_payload = false; 94 | 95 | return $this->getUpdate(); 96 | } else { 97 | curl_setopt_array($this->curl, [ 98 | CURLOPT_URL => $this->endpoint . $method, 99 | CURLOPT_POSTFIELDS => empty($args) ? null : $args, 100 | ]); 101 | $resultCurl = curl_exec($this->curl); 102 | if ($resultCurl === false) { 103 | $arr = [ 104 | "ok" => false, 105 | "error_code" => curl_errno($this->curl), 106 | "description" => curl_error($this->curl), 107 | "curl_error" => true 108 | ]; 109 | 110 | $resultCurl = json_encode($arr); 111 | } 112 | 113 | $resultJson = json_decode($resultCurl); 114 | if ($resultJson === null) { 115 | $arr = [ 116 | "ok" => false, 117 | "error_code" => json_last_error(), 118 | "description" => json_last_error_msg(), 119 | "json_error" => true 120 | ]; 121 | $resultJson = json_decode(json_encode($arr)); 122 | } 123 | 124 | return $resultJson; 125 | } 126 | } 127 | 128 | 129 | /* 130 | * Make var_export() and send it in the actual chat_id 131 | * 132 | * @param int|string $chat_id for the target chat 133 | * @param mixed,... $var unlimited optional variable to send 134 | * 135 | * @return bool true if can send message, otherwise false 136 | */ 137 | 138 | public function debug($chat_id, ...$vars): bool { 139 | foreach ($vars as $debug) { 140 | $str = var_export($debug, true); 141 | $array_str = str_split($str, 4050); 142 | 143 | foreach ($array_str as $value) { 144 | $result = $this->sendMessage($chat_id, "Debug:" . PHP_EOL . $value); 145 | if ($result->ok === false) { 146 | return false; 147 | } 148 | } 149 | } 150 | 151 | return true; 152 | } 153 | 154 | } 155 | -------------------------------------------------------------------------------- /src/EasyVars.php: -------------------------------------------------------------------------------- 1 | types as $type) { 21 | if (isset($update->{$type})) { 22 | $this->type = $type; 23 | break; 24 | } 25 | } 26 | 27 | if (isset($this->type)) { 28 | $this->chat_type = $update->{$this->type}->chat->type ?? null; 29 | $this->chat_id = $update->{$this->type}->chat->id ?? null; 30 | $this->message_id = $update->{$this->type}->message_id ?? null; 31 | $this->message_thread_id = $update->{$this->type}->message_thread_id ?? null; 32 | 33 | if (isset($update->{$this->type}->from)) { 34 | $this->from_id = $update->{$this->type}->from->id ?? null; 35 | $this->first_name = $update->{$this->type}->from->first_name ?? null; 36 | } 37 | 38 | if (isset($update->{$this->type}->text)) { 39 | $this->text = $update->{$this->type}->text ?? null; 40 | } 41 | } 42 | } 43 | } 44 | --------------------------------------------------------------------------------