1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Telegram Client Class |
5
|
|
|
* @author Alexander Rodriguez <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace App; |
9
|
|
|
|
10
|
|
|
class Telegram |
11
|
|
|
{ |
12
|
|
|
private $token; |
13
|
|
|
private $webhook; |
14
|
|
|
private $apiUrl; |
15
|
|
|
|
16
|
|
|
public function __construct($config, $pwrtelegram = false) |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Can use PWRTelegram for active more power of telegram. |
20
|
|
|
*/ |
21
|
|
|
$this->apiUrl = ($pwrtelegram) ? 'https://api.pwrtelegram.xyz/bot'.$config['TELEGRAM_TOKEN'].'/' : 'https://api.telegram.org/bot'.$config['TELEGRAM_TOKEN'].'/'; |
22
|
|
|
$this->webhook = $config['WEBHOOK_URL']; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $method |
27
|
|
|
*/ |
28
|
|
|
public function apiRequestWebhook($method, $parameters) |
29
|
|
|
{ |
30
|
|
|
if (!is_string($method)) { |
31
|
|
|
error_log("El nombre del método debe ser una cadena de texto\n"); |
32
|
|
|
|
33
|
|
|
return false; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
View Code Duplication |
if (!$parameters) { |
|
|
|
|
37
|
|
|
$parameters = []; |
38
|
|
|
} elseif (!is_array($parameters)) { |
39
|
|
|
error_log("Los parámetros deben ser un arreglo/matriz\n"); |
40
|
|
|
|
41
|
|
|
return false; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$parameters['method'] = $method; |
45
|
|
|
header('Content-Type: application/json'); |
46
|
|
|
echo json_encode($parameters); |
47
|
|
|
|
48
|
|
|
return true; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param resource $handle |
53
|
|
|
*/ |
54
|
|
|
public function exec_curl_request($handle) |
55
|
|
|
{ |
56
|
|
|
$response = curl_exec($handle); |
57
|
|
|
if ($response === false) { |
58
|
|
|
$errno = curl_errno($handle); |
59
|
|
|
$error = curl_error($handle); |
60
|
|
|
error_log("Curl retornó un error $errno: $error\n"); |
61
|
|
|
curl_close($handle); |
62
|
|
|
|
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$http_code = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE)); |
67
|
|
|
curl_close($handle); |
68
|
|
|
if ($http_code >= 500) { |
69
|
|
|
|
70
|
|
|
// do not wat to DDOS server if something goes wrong |
71
|
|
|
|
72
|
|
|
sleep(10); |
73
|
|
|
|
74
|
|
|
return false; |
75
|
|
|
} elseif ($http_code != 200) { |
76
|
|
|
$response = json_decode($response, true); |
77
|
|
|
error_log("La solicitud falló con el error {$response['error_code']}: {$response['description']}\n"); |
78
|
|
|
if ($http_code == 401) { |
79
|
|
|
throw new Exception('El token provisto es inválido'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return false; |
83
|
|
|
} else { |
84
|
|
|
$response = json_decode($response, true); |
85
|
|
|
if (isset($response['description'])) { |
86
|
|
|
error_log("La solicitud fue exitosa: {$response['description']}\n"); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$response = $response['result']; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $response; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $method |
97
|
|
|
*/ |
98
|
|
|
public function apiRequest($method, $parameters) |
99
|
|
|
{ |
100
|
|
|
if (!is_string($method)) { |
101
|
|
|
error_log("El nombre del método debe ser una cadena de texto\n"); |
102
|
|
|
|
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
View Code Duplication |
if (!$parameters) { |
|
|
|
|
107
|
|
|
$parameters = []; |
108
|
|
|
} elseif (!is_array($parameters)) { |
109
|
|
|
error_log("Los parámetros deben ser un arreglo/matriz\n"); |
110
|
|
|
|
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
foreach ($parameters as $key => &$val) { |
115
|
|
|
|
116
|
|
|
// encoding to JSON array parameters, for example reply_markup |
117
|
|
|
|
118
|
|
|
if (!is_numeric($val) && !is_string($val)) { |
119
|
|
|
$val = json_encode($val); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$url = $this->apiUrl.$method.'?'.http_build_query($parameters); |
124
|
|
|
$handle = curl_init($url); |
125
|
|
|
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); |
126
|
|
|
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5); |
127
|
|
|
curl_setopt($handle, CURLOPT_TIMEOUT, 60); |
128
|
|
|
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false); |
129
|
|
|
|
130
|
|
|
return self::exec_curl_request($handle); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string $method |
135
|
|
|
*/ |
136
|
|
|
public function apiRequestJson($method, $parameters) |
137
|
|
|
{ |
138
|
|
|
if (!is_string($method)) { |
139
|
|
|
error_log("El nombre del método debe ser una cadena de texto\n"); |
140
|
|
|
|
141
|
|
|
return false; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if (!$parameters) { |
145
|
|
|
$parameters = []; |
146
|
|
|
} elseif (!is_array($parameters)) { |
147
|
|
|
error_log("Los parámetros deben ser un arreglo/matriz\n"); |
148
|
|
|
|
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$parameters['method'] = $method; |
153
|
|
|
$handle = curl_init($this->apiUrl); |
154
|
|
|
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); |
155
|
|
|
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5); |
156
|
|
|
curl_setopt($handle, CURLOPT_TIMEOUT, 60); |
157
|
|
|
curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($parameters)); |
158
|
|
|
curl_setopt($handle, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); |
159
|
|
|
|
160
|
|
|
return exec_curl_request($handle); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function sendMessage($chat_id, $text, $args = []) |
164
|
|
|
{ |
165
|
|
|
$parameters = $args; |
166
|
|
|
$parameters['chat_id'] = $chat_id; |
167
|
|
|
$parameters['text'] = $text; |
168
|
|
|
|
169
|
|
|
return $this->apiRequest('sendMessage', $parameters); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function kickChatMember($chat_id, $user_id, $until_date = null) |
173
|
|
|
{ |
174
|
|
|
$parameters = []; |
175
|
|
|
$parameters['chat_id'] = $chat_id; |
176
|
|
|
$parameters['user_id'] = $user_id; |
177
|
|
|
$parameters['until_date'] = $until_date; |
178
|
|
|
|
179
|
|
|
return $this->apiRequest('kickChatMember', $parameters); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function deleteMessage($chat_id, $message_id) |
183
|
|
|
{ |
184
|
|
|
$parameters = []; |
185
|
|
|
$parameters['chat_id'] = $chat_id; |
186
|
|
|
$parameters['message_id'] = $message_id; |
187
|
|
|
|
188
|
|
|
return $this->apiRequest('deleteMessage', $parameters); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function setWebhook($certificate = null, $max_connections = null, $allowed_updates = []) |
192
|
|
|
{ |
193
|
|
|
$parameters['url'] = $this->webhook; |
|
|
|
|
194
|
|
|
$parameters['certificate'] = $certificate; |
195
|
|
|
$parameters['max_connections'] = $max_connections; |
196
|
|
|
$parameters['allowed_updates'] = $allowed_updates; |
197
|
|
|
|
198
|
|
|
return $this->apiRequest('setWebhook', $parameters); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.