-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook.php
319 lines (292 loc) · 15.1 KB
/
hook.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php
/* This script should be installed as a webhook handler for api.telegram.org */
error_reporting(E_ALL);
spl_autoload_register(function (string $className) {
require_once __DIR__ . '/' . str_replace('\\', '/', $className) . '.php';
});
use lib\KeyBoards;
use lib\ParserProcessor;
use lib\ConfigurationManager;
use lib\TelegramOrgAPI;
use lib\ExchangeRatesAPI;
use lib\OpenWeatherAPI;
$configurationManagerHandler = new ConfigurationManager();
$telegram_server_input_request = file_get_contents('php://input');
$client_data = json_decode($telegram_server_input_request, true);
if (empty($client_data['message']['chat']['id'])) {
echo "NO DATA OR FORMAT ERROR";
exit();
}
$BotInterface = new TelegramOrgAPI(TELEGRAM_API_BOT_TOKEN);
$weatherKeyboard = KeyBoards::getKeyBoard([[["text" => "Weather in Moscow"], ["text" => "Weather in London"],
["text" => "Weather in New York"], ["text" => "Weather in Tokyo"]]]);
if (!empty($client_data['message']['text'])) {
$MySqlObj = new mysqli(MYSQL_DB_HOST, MYSQL_DB_USER,
MYSQL_DB_PASSWORD, MYSQL_MSG_LOGS_DB_NAME);
if ($MySqlObj->connect_errno) {
echo "Failed to connect to the database with msg logs: " . $MySqlObj->connect_error;
trigger_error("Failed to connect to the database with msg logs: " .
$MySqlObj->connect_error, E_USER_ERROR);
exit("Failed to connect to the database with msg logs: " . $MySqlObj->connect_error);
}
$client_msg_date = $client_data['message']["date"];
$client_first_name = $MySqlObj->real_escape_string($client_data['message']["from"]["first_name"]);
$client_last_name = $MySqlObj->real_escape_string($client_data['message']["from"]["last_name"]);
$client_username = $MySqlObj->real_escape_string($client_data['message']["from"]["username"]);
$client_msg = $MySqlObj->real_escape_string($client_data['message']["text"]);
$db_query = "INSERT INTO msg_logs (
telegram_date,
telegram_from_first_name,
telegram_from_last_name,
telegram_from_username,
telegram_text)
VALUES (
'$client_msg_date' ,
'$client_first_name' ,
'$client_last_name' ,
'$client_username' ,
'$client_msg')";
$MySqlObj->query("SET NAMES utf8mb4");
if ($MySqlObj->query($db_query) === FALSE) {
echo "Failed to insert data into a table: " . $MySqlObj->connect_error;
trigger_error("Failed to insert data into a table: " .
$MySqlObj->connect_error, E_USER_ERROR);
exit("Failed to insert data into a table: " . $MySqlObj->connect_error);
}
$MySqlObj->close();
$switcher_flag = ParserProcessor::clientMessageParser($client_data['message']['text']);
switch ($switcher_flag) {
case "USD_EXCHANGE_RATE":
$USD_data = ExchangeRatesAPI::getExchangeRates("USD");
if ($USD_data == "API_REQUEST_FAILURE") {
$BotInterface->sendTelegram(
'sendMessage',
array(
'chat_id' => $client_data['message']['chat']['id'],
'text' => "Sorry, something is wrong with the currency exchange service.⚙️ " . PHP_EOL .
"Please try again later..."
)
);
exit('System failure: It is not possible to connect to the exchange rate API');
}
$USD_style_header = "Ok, here is the result of your request❤️" . PHP_EOL;
$USD_current_value = "One dollar 💵 is equal to " . $USD_data['CurrentValue'] . " rubles";
$USD_previous_value = "Previous dollar 💵 value: " . $USD_data['PreviousValue'] . " rubles";
$USD_style_footer = PHP_EOL . "I expect new requests from you👋";
$BotInterface->sendTelegram(
'sendMessage',
array(
'chat_id' => $client_data['message']['chat']['id'],
'text' => $USD_style_header . PHP_EOL . $USD_current_value . PHP_EOL .
$USD_previous_value . PHP_EOL . $USD_style_footer,
)
);
exit();
break;
case "EUR_EXCHANGE_RATE":
$EUR_data = ExchangeRatesAPI::getExchangeRates("EUR");
if ($EUR_data == "API_REQUEST_FAILURE") {
$BotInterface->sendTelegram(
'sendMessage',
array(
'chat_id' => $client_data['message']['chat']['id'],
'text' => "Sorry, something is wrong with the currency exchange service.⚙️ " . PHP_EOL .
"Please try again later..."
)
);
exit('System failure: It is not possible to connect to the exchange rate API');
}
$EUR_style_header = "Ok, here is the result of your request❤️" . PHP_EOL;
$EUR_current_value = "One euro 💶 is equal to " . $EUR_data['CurrentValue'] . " rubles";
$EUR_previous_value = "Previous euro 💶 value: " . $EUR_data['PreviousValue'] . " rubles";
$EUR_style_footer = PHP_EOL . "I expect new requests from you👋";
$BotInterface->sendTelegram(
'sendMessage',
array(
'chat_id' => $client_data['message']['chat']['id'],
'text' => $EUR_style_header . PHP_EOL . $EUR_current_value . PHP_EOL .
$EUR_previous_value . PHP_EOL . $EUR_style_footer,
)
);
exit();
break;
case "WEATHER_SWITCHER":
$BotInterface->sendTelegram(
'sendMessage',
array(
'text' => "Well let's choose a location🗺",
'chat_id' => $client_data['message']['chat']['id'],
'reply_markup' => $weatherKeyboard,
)
);
exit();
break;
case "MOSCOW_WEATHER":
$weather_data = OpenWeatherAPI::getWeather("Moscow");
if ($weather_data == "API_REQUEST_FAILURE") {
$BotInterface->sendTelegram(
'sendMessage',
array(
'chat_id' => $client_data['message']['chat']['id'],
'text' => "Sorry, something is wrong with the weather service.⚙️ " . PHP_EOL .
"Please try again later..."
)
);
exit('System failure: It is not possible to connect to the OpenWeather API');
}
$weather_style_header = "Ok, here is the result of your request❤️" . PHP_EOL;
$weather_description = "Moscow weather: " . $weather_data['MainDescription'];
$weather_wind = "Wind speed: " . $weather_data['WindSpeed'] . " m/s";
$weather_temp = "Current temperature: " . $weather_data['TempCurrent'] . " ℃";
$max_temp = "Max temperature: " . $weather_data['TempMax'] . " ℃";
$min_min = "Min temperature: " . $weather_data['TempMin'] . " ℃";
$weather_style_footer = PHP_EOL . "I expect new requests from you👋";
$BotInterface->sendTelegram(
'sendMessage',
array(
'chat_id' => $client_data['message']['chat']['id'],
'text' => $weather_style_header . PHP_EOL . $weather_description . PHP_EOL .
$weather_wind . PHP_EOL . $weather_temp . PHP_EOL . $max_temp . PHP_EOL .
$min_min . PHP_EOL . $weather_style_footer,
)
);
exit();
break;
case "LONDON_WEATHER":
$weather_data = OpenWeatherAPI::getWeather("London");
if ($weather_data == "API_REQUEST_FAILURE") {
$BotInterface->sendTelegram(
'sendMessage',
array(
'chat_id' => $client_data['message']['chat']['id'],
'text' => "Sorry, something is wrong with the weather service.⚙️ " . PHP_EOL .
"Please try again later..."
)
);
exit('System failure: It is not possible to connect to the OpenWeather API');
}
$weather_style_header = "Ok, here is the result of your request❤️" . PHP_EOL;
$weather_description = "London weather: " . $weather_data['MainDescription'];
$weather_wind = "Wind speed: " . $weather_data['WindSpeed'] . " m/s";
$weather_temp = "Current temperature: " . $weather_data['TempCurrent'] . " ℃";
$max_temp = "Max temperature: " . $weather_data['TempMax'] . " ℃";
$min_min = "Min temperature: " . $weather_data['TempMin'] . " ℃";
$weather_style_footer = PHP_EOL . "I expect new requests from you👋";
$BotInterface->sendTelegram(
'sendMessage',
array(
'chat_id' => $client_data['message']['chat']['id'],
'text' => $weather_style_header . PHP_EOL . $weather_description . PHP_EOL .
$weather_wind . PHP_EOL . $weather_temp . PHP_EOL . $max_temp . PHP_EOL .
$min_min . PHP_EOL . $weather_style_footer,
)
);
exit();
break;
case "NEWYORK_WEATHER":
$weather_data = OpenWeatherAPI::getWeather("New York");
if ($weather_data == "API_REQUEST_FAILURE") {
$BotInterface->sendTelegram(
'sendMessage',
array(
'chat_id' => $client_data['message']['chat']['id'],
'text' => "Sorry, something is wrong with the weather service.⚙️ " . PHP_EOL .
"Please try again later..."
)
);
exit('System failure: It is not possible to connect to the OpenWeather API');
}
$weather_style_header = "Ok, here is the result of your request❤️" . PHP_EOL;
$weather_description = "New York weather: " . $weather_data['MainDescription'];
$weather_wind = "Wind speed: " . $weather_data['WindSpeed'] . " m/s";
$weather_temp = "Current temperature: " . $weather_data['TempCurrent'] . " ℃";
$max_temp = "Max temperature: " . $weather_data['TempMax'] . " ℃";
$min_min = "Min temperature: " . $weather_data['TempMin'] . " ℃";
$weather_style_footer = PHP_EOL . "I expect new requests from you👋";
$BotInterface->sendTelegram(
'sendMessage',
array(
'chat_id' => $client_data['message']['chat']['id'],
'text' => $weather_style_header . PHP_EOL . $weather_description . PHP_EOL .
$weather_wind . PHP_EOL . $weather_temp . PHP_EOL . $max_temp . PHP_EOL .
$min_min . PHP_EOL . $weather_style_footer,
)
);
exit();
break;
case "TOKYO_WEATHER":
$weather_data = OpenWeatherAPI::getWeather("Tokyo");
if ($weather_data == "API_REQUEST_FAILURE") {
$BotInterface->sendTelegram(
'sendMessage',
array(
'chat_id' => $client_data['message']['chat']['id'],
'text' => "Sorry, something is wrong with the weather service.⚙️ " . PHP_EOL .
"Please try again later..."
)
);
exit('System failure: It is not possible to connect to the OpenWeather API');
}
$weather_style_header = "Ok, here is the result of your request❤️" . PHP_EOL;
$weather_description = "Tokyo weather: " . $weather_data['MainDescription'];
$weather_wind = "Wind speed: " . $weather_data['WindSpeed'] . " m/s";
$weather_temp = "Current temperature: " . $weather_data['TempCurrent'] . " ℃";
$max_temp = "Max temperature: " . $weather_data['TempMax'] . " ℃";
$min_min = "Min temperature: " . $weather_data['TempMin'] . " ℃";
$weather_style_footer = PHP_EOL . "I expect new requests from you👋";
$BotInterface->sendTelegram(
'sendMessage',
array(
'chat_id' => $client_data['message']['chat']['id'],
'text' => $weather_style_header . PHP_EOL . $weather_description . PHP_EOL .
$weather_wind . PHP_EOL . $weather_temp . PHP_EOL . $max_temp . PHP_EOL .
$min_min . PHP_EOL . $weather_style_footer,
)
);
exit();
break;
case "HELP":
$HELP_style_header = 'All right, let me tell you what I can do🖖' . PHP_EOL;
$HELP_dollar = 'Use /dollar to get the dollar rate';
$HELP_euro = 'Use /euro to get the euro rate';
$HELP_weather = 'Use /weather to get the weather';
$HELP_code_source_url = 'https://github.com/GRISHNOV/TelegramBotAssistant2020';
$HELP_bot_author_ref = PHP_EOL . 'You can find the source code of this bot at the [link](' .
$HELP_code_source_url . ')' . PHP_EOL;
$HELP_style_footer = "*Good luck!👊*";
$BotInterface->sendTelegram(
'sendMessage',
array(
'chat_id' => $client_data['message']['chat']['id'],
'text' => $HELP_style_header . PHP_EOL . $HELP_dollar . PHP_EOL . $HELP_euro . PHP_EOL .
$HELP_weather . PHP_EOL . $HELP_bot_author_ref . PHP_EOL . $HELP_style_footer,
'parse_mode' => 'Markdown',
)
);
exit();
break;
case "START":
$user_name = $client_data['message']['from']['first_name'];
$BotInterface->sendTelegram(
'sendAnimation',
array(
'chat_id' => $client_data['message']['chat']['id'],
'animation' => curl_file_create(__DIR__ . '/img/start.gif'),
'caption' => "Hey! Nice to meet you, $user_name 😄. First look here /help 🏁",
)
);
exit();
break;
case "PARSER_ERROR":
$BotInterface->sendTelegram(
'sendAnimation',
array(
'chat_id' => $client_data['message']['chat']['id'],
'animation' => curl_file_create(__DIR__ . '/img/error.gif'),
'caption' => "Oops! I can not understand you💔. Perhaps it makes sense to look into the /help 😉",
)
);
exit();
break;
}
}