forked from anhao/TgMessage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bot.php
81 lines (71 loc) · 1.79 KB
/
Bot.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
70
71
72
73
74
75
76
77
78
79
80
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;
}
}