These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Codigo base encontrado en https://core.telegram.org/bots/samples/hellobot. |
||
4 | */ |
||
5 | define('BOT_TOKEN', '12345678:replace-me-with-real-token'); |
||
6 | define('WEBHOOK_URL', 'https://your.webhook/url/bot.php'); |
||
7 | define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/'); |
||
8 | |||
9 | /** |
||
10 | * @param string $method |
||
11 | */ |
||
12 | function apiRequestWebhook($method, $parameters) |
||
13 | { |
||
14 | if (!is_string($method)) { |
||
15 | error_log("El nombre del método debe ser una cadena de texto\n"); |
||
16 | |||
17 | return false; |
||
18 | } |
||
19 | |||
20 | View Code Duplication | if (!$parameters) { |
|
21 | $parameters = []; |
||
22 | } elseif (!is_array($parameters)) { |
||
23 | error_log("Los parámetros deben ser un arreglo/matriz\n"); |
||
24 | |||
25 | return false; |
||
26 | } |
||
27 | |||
28 | $parameters['method'] = $method; |
||
29 | header('Content-Type: application/json'); |
||
30 | echo json_encode($parameters); |
||
31 | |||
32 | return true; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @param resource $handle |
||
37 | */ |
||
38 | function exec_curl_request($handle) |
||
39 | { |
||
40 | $response = curl_exec($handle); |
||
41 | if ($response === false) { |
||
42 | $errno = curl_errno($handle); |
||
43 | $error = curl_error($handle); |
||
44 | error_log("Curl retornó un error $errno: $error\n"); |
||
45 | curl_close($handle); |
||
46 | |||
47 | return false; |
||
48 | } |
||
49 | |||
50 | $http_code = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE)); |
||
51 | curl_close($handle); |
||
52 | if ($http_code >= 500) { |
||
53 | |||
54 | // do not wat to DDOS server if something goes wrong |
||
55 | |||
56 | sleep(10); |
||
57 | |||
58 | return false; |
||
59 | } elseif ($http_code != 200) { |
||
60 | $response = json_decode($response, true); |
||
61 | error_log("La solicitud falló con el error {$response['error_code']}: {$response['description']}\n"); |
||
62 | if ($http_code == 401) { |
||
63 | throw new Exception('El token provisto es inválido'); |
||
64 | } |
||
65 | |||
66 | return false; |
||
67 | } else { |
||
68 | $response = json_decode($response, true); |
||
69 | if (isset($response['description'])) { |
||
70 | error_log("La solicitud fue exitosa: {$response['description']}\n"); |
||
71 | } |
||
72 | |||
73 | $response = $response['result']; |
||
74 | } |
||
75 | |||
76 | return $response; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @param string $method |
||
81 | */ |
||
82 | function apiRequest($method, $parameters) |
||
83 | { |
||
84 | if (!is_string($method)) { |
||
85 | error_log("El nombre del método debe ser una cadena de texto\n"); |
||
86 | |||
87 | return false; |
||
88 | } |
||
89 | |||
90 | View Code Duplication | if (!$parameters) { |
|
91 | $parameters = []; |
||
92 | } elseif (!is_array($parameters)) { |
||
93 | error_log("Los parámetros deben ser un arreglo/matriz\n"); |
||
94 | |||
95 | return false; |
||
96 | } |
||
97 | |||
98 | foreach ($parameters as $key => &$val) { |
||
99 | |||
100 | // encoding to JSON array parameters, for example reply_markup |
||
101 | |||
102 | if (!is_numeric($val) && !is_string($val)) { |
||
103 | $val = json_encode($val); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | $url = API_URL.$method.'?'.http_build_query($parameters); |
||
108 | $handle = curl_init($url); |
||
109 | curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); |
||
110 | curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5); |
||
111 | curl_setopt($handle, CURLOPT_TIMEOUT, 60); |
||
112 | curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false); |
||
113 | |||
114 | return exec_curl_request($handle); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param string $method |
||
119 | */ |
||
120 | function apiRequestJson($method, $parameters) |
||
121 | { |
||
122 | if (!is_string($method)) { |
||
123 | error_log("El nombre del método debe ser una cadena de texto\n"); |
||
124 | |||
125 | return false; |
||
126 | } |
||
127 | |||
128 | View Code Duplication | if (!$parameters) { |
|
0 ignored issues
–
show
|
|||
129 | $parameters = []; |
||
130 | } elseif (!is_array($parameters)) { |
||
131 | error_log("Los parámetros deben ser un arreglo/matriz\n"); |
||
132 | |||
133 | return false; |
||
134 | } |
||
135 | |||
136 | $parameters['method'] = $method; |
||
137 | $handle = curl_init(API_URL); |
||
138 | curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); |
||
139 | curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5); |
||
140 | curl_setopt($handle, CURLOPT_TIMEOUT, 60); |
||
141 | curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($parameters)); |
||
142 | curl_setopt($handle, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); |
||
143 | |||
144 | return exec_curl_request($handle); |
||
145 | } |
||
146 | |||
147 | function processMessage($message) |
||
148 | { |
||
149 | |||
150 | // process incoming message |
||
151 | |||
152 | $message_id = $message['message_id']; |
||
0 ignored issues
–
show
$message_id is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() |
|||
153 | $chat_id = $message['chat']['id']; |
||
154 | if (isset($message['text'])) { |
||
155 | |||
156 | // incoming text message |
||
157 | |||
158 | $text = $message['text']; |
||
159 | if (strpos($text, '/start') === 0) { |
||
160 | apiRequestJson('sendMessage', [ |
||
161 | 'chat_id' => $chat_id, |
||
162 | 'text' => 'Hola', |
||
163 | 'reply_markup' => [ |
||
164 | 'keyboard' => [['Hola', 'Epale']], |
||
165 | 'one_time_keyboard' => true, |
||
166 | 'resize_keyboard' => true, ], |
||
167 | ]); |
||
168 | } elseif ($text === 'Hola' || $text === 'Epale') { |
||
169 | apiRequest('sendMessage', [ |
||
170 | 'chat_id' => $chat_id, |
||
171 | 'text' => 'Es un placer conocerte', |
||
172 | ]); |
||
173 | } elseif (strpos($text, '/stop') === 0) { |
||
174 | |||
175 | // stop now |
||
176 | } else { |
||
177 | apiRequestWebhook('sendMessage', [ |
||
178 | 'chat_id' => $chat_id, |
||
179 | 'text' => 'Yo sólo entiendo mensajes de texto', |
||
180 | ]); |
||
181 | } |
||
182 | } else { |
||
183 | apiRequest('sendMessage', [ |
||
184 | 'chat_id' => $chat_id, |
||
185 | 'text' => 'Yo sólo entiendo mensajes de texto', |
||
186 | ]); |
||
187 | } |
||
188 | } |
||
189 | |||
190 | function saluteNewMember($message) |
||
191 | { |
||
192 | $chat_id = $message['chat']['id']; |
||
193 | $first_name = $message['new_chat_member']['first_name']; |
||
194 | $username = ($message['new_chat_member']['username']) ? '( @'.$message['new_chat_member']['username'].' )' : ''; |
||
195 | $welcome_text = "📢 Bienvenido/a *$first_name* $username a [PHP.ve](https://telegram.me/PHP_Ve), te invitamos a que leas la [Descripción y Normas del Grupo](http://telegra.ph/PHPve-11-24)"; |
||
196 | apiRequest('sendMessage', [ |
||
197 | 'chat_id' => $chat_id, |
||
198 | 'text' => $welcome_text, |
||
199 | 'parse_mode' => 'Markdown', |
||
200 | 'disable_web_page_preview' => true, |
||
201 | ]); |
||
202 | } |
||
203 | |||
204 | if (php_sapi_name() == 'cli') { |
||
205 | |||
206 | // if run from console, set or delete webhook |
||
207 | |||
208 | apiRequest('setWebhook', ['url' => isset($argv[1]) && $argv[1] == 'delete' ? '' : WEBHOOK_URL]); |
||
209 | exit; |
||
210 | } |
||
211 | |||
212 | $content = file_get_contents('php://input'); |
||
213 | $update = json_decode($content, true); |
||
214 | |||
215 | if (!$update) { |
||
216 | |||
217 | // receive wrong update, must not happen |
||
218 | |||
219 | exit; |
||
220 | } |
||
221 | |||
222 | if (isset($update['message']['new_chat_member'])) { |
||
223 | saluteNewMember($update['message']); |
||
224 | } elseif (isset($update['message']['text'])) { |
||
225 | processMessage($update['message']); |
||
226 | } |
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.