forked from anhao/TgMessage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
126 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
/** | ||
* 简单封装 tg bot 请求 | ||
* Class Bot | ||
* @author Alone88 | ||
* @link https://alone88.cn | ||
*/ | ||
class Bot | ||
{ | ||
// chat_id sign key | ||
private string $key; | ||
//Bot Token | ||
private string $token; | ||
|
||
public function __construct() | ||
{ | ||
$this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,21 @@ | ||
<?php | ||
require_once 'Bot.php'; | ||
|
||
header('content-type: application/json'); | ||
$type = $_REQUEST['type'] ?? 'message'; | ||
$token = $_REQUEST['token'] ?? null; | ||
$message = $_REQUEST['message'] ?? ''; | ||
|
||
$bot = new Bot(); | ||
// webhook | ||
if ($type === 'webhook') { | ||
// 获取/token 命令,获取聊天 id | ||
$data = json_decode(file_get_contents("php://input"), true); | ||
$message = $data['message']['text'] ?? ''; | ||
if ($message === '/token') { | ||
$chat_id = $data['message']['chat']['id']; | ||
$bot->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']]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
header('content-type: application/json'); | ||
|
||
$key = $_REQUEST['key'] ?? ''; | ||
|
||
if ($key !== $_ENV['key']) { | ||
echo json_encode(['code' => 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']]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
header('content-type: application/json'); | ||
require_once 'Bot.php'; | ||
$bot = new Bot(); | ||
// 获取/token 命令,获取聊天 id | ||
$data = json_decode(file_get_contents("php://input"), true); | ||
$message = $data['message']['text'] ?? ''; | ||
if ($message === '/token') { | ||
$chat_id = $data['message']['chat']['id']; | ||
$bot->sendMessage(['text' => $bot->encryption($chat_id), 'chat_id' => $chat_id]); | ||
} | ||
echo json_encode(['code' => 200, 'message' => 'success']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters