forked from anhao/TgMessage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
69 lines (63 loc) · 2.13 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?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']);
} else {
if (is_null($token)) {
echo json_encode(['code' => 422, 'message' => 'token 不能为空']);
} 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));
}
}