diff --git a/api/Bot.php b/api/Bot.php new file mode 100644 index 0000000..b119c74 --- /dev/null +++ b/api/Bot.php @@ -0,0 +1,81 @@ +token = $_ENV['token']; + $this->key = $_ENV['sign_key'] ?? 'abc'; + } + + public function request($method, $data) + { + $curl = curl_init(); + + curl_setopt_array($curl, array( + CURLOPT_URL => sprintf('https://api.telegram.org/bot%s/%s', $this->token, $method), + CURLOPT_RETURNTRANSFER => true, + CURLOPT_ENCODING => '', + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => 'POST', + CURLOPT_POSTFIELDS => $data, + )); + $response = curl_exec($curl); + curl_close($curl); + return json_decode($response, true); + } + + public function sendMessage($data): array + { + return $this->request('sendMessage', $data); + } + + public function setWebHook($data) + { + return $this->request('setWebhook', $data); + } + + public function encryption($chat_id): string + { + return base64_encode($this->key . $chat_id); + } + + public function decryption($text): string + { + return substr(base64_decode($text), strlen($this->key)); + } + + /** + * @param string $key + * @return Bot + */ + public function setKey(string $key): Bot + { + $this->key = $key; + return $this; + } + + /** + * @param string $token + * @return Bot + */ + public function setToken(string $token): Bot + { + $this->token = $token; + return $this; + } +} \ No newline at end of file diff --git a/api/index.php b/api/index.php index 108c820..5ea77cb 100644 --- a/api/index.php +++ b/api/index.php @@ -1,69 +1,21 @@ sendMessage(['text' => $bot->encryption($chat_id), 'chat_id' => $chat_id]); - } - echo json_encode(['code' => 200, 'message' => 'success']); + +if (is_null($token)) { + echo json_encode(['code' => 422, 'message' => 'token 不能为空']); } else { - if (is_null($token)) { - echo json_encode(['code' => 422, 'message' => 'token 不能为空']); + // 发送消息 + $chat_id = $bot->decryption($token); + $ret = $bot->sendMessage(['text' => $message, 'chat_id' => $chat_id]); + if ($ret['ok']) { + echo json_encode(['code' => 200, 'message' => 'success']); } else { - // 发送消息 - $chat_id = $bot->decryption($token); - $ret = $bot->sendMessage(['text' => $message, 'chat_id' => $chat_id]); - if ($ret['ok']) { - echo json_encode(['code' => 200, 'message' => 'success']); - } else { - echo json_encode(['code' => 422, 'message' => 'error']); - } - } -} - -class Bot -{ - // chat_id sign key - public string $key = "abc"; - //Bot Token - public string $token = "1679016407:AAEHII057c26C5_vY8tSLbO8G6mnyXUOYbM"; - - public function sendMessage($data): array - { - $curl = curl_init(); - - curl_setopt_array($curl, array( - CURLOPT_URL => sprintf('https://api.telegram.org/bot%s/sendMessage', $this->token), - CURLOPT_RETURNTRANSFER => true, - CURLOPT_ENCODING => '', - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, - CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, - CURLOPT_CUSTOMREQUEST => 'POST', - CURLOPT_POSTFIELDS => $data, - )); - $response = curl_exec($curl); - curl_close($curl); - return json_decode($response, true); - } - - public function encryption($chat_id): string - { - return base64_encode($this->key . $chat_id); - } - - public function decryption($text): string - { - return substr(base64_decode($text), strlen($this->key)); + echo json_encode(['code' => 422, 'message' => $ret['description']]); } } \ No newline at end of file diff --git a/api/setWebHook.php b/api/setWebHook.php new file mode 100644 index 0000000..e5a5763 --- /dev/null +++ b/api/setWebHook.php @@ -0,0 +1,20 @@ + 422, 'message' => '设置秘钥错误']); +} else { + //开始设置回调url + $url = $_REQUEST['url'] ?? $_ENV['url']; + require_once 'Bot.php'; + $data = ['url' => $url]; + $bot = new Bot(); + $ret = $bot->setWebHook($data); + if ($ret['ok']) { + echo json_encode(['code' => 200, 'message' => '设置成功']); + } else { + echo json_encode(['code' => 422, 'message' => $ret['description']]); + } +} \ No newline at end of file diff --git a/api/webhook.php b/api/webhook.php new file mode 100644 index 0000000..19b8497 --- /dev/null +++ b/api/webhook.php @@ -0,0 +1,12 @@ +sendMessage(['text' => $bot->encryption($chat_id), 'chat_id' => $chat_id]); +} +echo json_encode(['code' => 200, 'message' => 'success']); \ No newline at end of file diff --git a/vercel.json b/vercel.json index 4191aa6..c93b8e6 100644 --- a/vercel.json +++ b/vercel.json @@ -6,11 +6,11 @@ }, "routes": [ { - "src": "setWebHook", + "src": "/setWebHook", "dest": "/api/setWebHook.php" }, { - "src": "webhook", + "src": "/webhook", "dest": "/api/webhook.php" } ]