forked from atis/raid-pokemon-bot
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfunctions.php
397 lines (329 loc) · 9.51 KB
/
functions.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php
/**
* Send message.
* @param $chat_id
* @param array $text
*/
function sendMessage($chat_id, $text = array())
{
// Create response content array.
$reply_content = [
'method' => 'sendMessage',
'chat_id' => $chat_id,
'parse_mode' => 'HTML',
'text' => $text
];
if (isset($inline_keyboard)) {
$reply_content['reply_markup'] = ['inline_keyboard' => $inline_keyboard];
}
// Encode data to json.
$reply_json = json_encode($reply_content);
// Set header to json.
header('Content-Type: application/json');
// Write to log.
debug_log($reply_json, '>');
// Send request to telegram api.
curl_json_request($reply_json);
}
/**
* Send message.
* @param $chat_id
* @param array $text
* @param mixed $inline_keyboard
* @param array $merge_args
*/
function send_message($chat_id, $text = array(), $inline_keyboard = false, $merge_args = [])
{
// Create response content array.
$reply_content = [
'method' => 'sendMessage',
'chat_id' => $chat_id,
'parse_mode' => 'HTML',
'text' => $text
];
// Write to log.
debug_log('KEYS');
debug_log($inline_keyboard);
if (isset($inline_keyboard)) {
$reply_content['reply_markup'] = ['inline_keyboard' => $inline_keyboard];
}
if (is_array($merge_args) && count($merge_args)) {
$reply_content = array_merge_recursive($reply_content, $merge_args);
}
// Encode data to json.
$reply_json = json_encode($reply_content);
// Set header to json.
header('Content-Type: application/json');
// Write to log.
debug_log($reply_json, '>');
// Send request to telegram api.
curl_json_request($reply_json);
}
/**
* Send location.
* @param $chat_id
* @param $lat
* @param $lon
* @param bool $inline_keyboard
* @return mixed
*/
function send_location($chat_id, $lat, $lon, $inline_keyboard = false)
{
// Create reply content array.
$reply_content = [
'method' => 'sendLocation',
'chat_id' => $chat_id,
'latitude' => $lat,
'longitude' => $lon
];
// Write to log.
debug_log('KEYS');
debug_log($inline_keyboard);
if (is_array($inline_keyboard)) {
$reply_content['reply_markup'] = ['inline_keyboard' => $inline_keyboard];
}
// Encode data to json.
$reply_json = json_encode($reply_content);
// Set header to json.
header('Content-Type: application/json');
// Write to log.
debug_log($reply_json, '>');
// Send request to telegram api and return response.
return curl_json_request($reply_json);
}
/**
* Echo message.
* @param $chat_id
* @param $text
*/
function sendMessageEcho($chat_id, $text)
{
// Create reply content array.
$reply_content = [
'method' => 'sendMessage',
'chat_id' => $chat_id,
'parse_mode' => 'HTML',
'text' => $text
];
// Encode data to json.
$reply_json = json_encode($reply_content);
// Set header to json.
header('Content-Type: application/json');
// Write to log.
debug_log($reply_json, '>');
// Echo json.
echo($reply_json);
}
/**
* Answer callback query.
* @param $query_id
* @param $text
*/
function answerCallbackQuery($query_id, $text)
{
// Create response array.
$response = [
'method' => 'answerCallbackQuery',
'callback_query_id' => $query_id,
'text' => $text
];
// Encode response to json format.
$json_response = json_encode($response);
// Set header to json.
header('Content-Type: application/json');
// Write to log.
debug_log($json_response, '>');
// Send request to telegram api.
curl_json_request($json_response);
}
/**
* Answer inline query.
* @param $query_id
* @param $contents
*/
function answerInlineQuery($query_id, $contents)
{
// Init empty result array.
$results = array();
// For each content.
foreach ($contents as $key => $row) {
// Get raid poll.
$text = show_raid_poll($row);
// Get inline keyboard.
$inline_keyboard = keys_vote($row);
// Create input message content array.
$input_message_content = [
'parse_mode' => 'HTML',
'message_text' => $text,
'disable_web_page_preview' => true
];
// Fill results array.
$results[] = [
'type' => 'article',
'id' => $query_id . $key,
'title' => ucfirst($row['pokemon']) . ' ' . unix2tz($row['ts_end'], $row['timezone']),
'description' => strval($row['gym_name']),
'input_message_content' => $input_message_content,
'reply_markup' => [
'inline_keyboard' => $inline_keyboard
]
];
}
// Create reply content array.
$reply_content = [
'method' => 'answerInlineQuery',
'inline_query_id' => $query_id,
'is_personal' => true,
'cache_time' => 10,
'results' => $results
];
// Encode to json and send request to telegram api.
curl_json_request(json_encode($reply_content));
}
/**
* Edit message text.
* @param $id_val
* @param $text_val
* @param $markup_val
* @param null $chat_id
* @param mixed $merge_args
*/
function editMessageText($id_val, $text_val, $markup_val, $chat_id = NULL, $merge_args = false)
{
// Create response array.
$response = [
'method' => 'editMessageText',
'text' => $text_val,
'parse_mode' => 'HTML',
'reply_markup' => [
'inline_keyboard' => $markup_val
]
];
if ($markup_val == false) {
unset($response['reply_markup']);
$response['remove_keyboard'] = true;
}
// Valid chat id.
if ($chat_id != null) {
$response['chat_id'] = $chat_id;
$response['message_id'] = $id_val;
} else {
$response['inline_message_id'] = $id_val;
}
// Write to log.
debug_log($merge_args, 'K');
debug_log($response, 'K');
if (is_array($merge_args) && count($merge_args)) {
$response = array_merge_recursive($response, $merge_args);
}
// Write to log.
debug_log($response, 'K');
// Encode response to json format.
$json_response = json_encode($response);
// Write to log.
debug_log($response, '<-');
// Send request to telegram api.
curl_json_request($json_response);
}
/**
* Edit message reply markup.
* @param $id_val
* @param $markup_val
* @param $chat_id
*/
function editMessageReplyMarkup($id_val, $markup_val, $chat_id)
{
// Create response array.
$response = [
'method' => 'editMessageReplyMarkup',
'reply_markup' => [
'inline_keyboard' => $markup_val
]
];
// Valid chat id.
if ($chat_id != null) {
$response['chat_id'] = $chat_id;
$response['message_id'] = $id_val;
} else {
$response['inline_message_id'] = $id_val;
}
// Encode response to json format.
$json_response = json_encode($response);
// Write to log.
debug_log($response, '->');
// Send request to telegram api.
curl_json_request($json_response);
}
/**
* Edit message keyboard.
* @param $id_val
* @param $markup_val
* @param $chat_id
*/
function edit_message_keyboard($id_val, $markup_val, $chat_id)
{
// Create response array.
$response = [
'method' => 'editMessageReplyMarkup',
'reply_markup' => [
'inline_keyboard' => $markup_val
]
];
// Valid chat id.
if ($chat_id != null) {
$response['chat_id'] = $chat_id;
$response['message_id'] = $id_val;
} else {
$response['inline_message_id'] = $id_val;
}
// Encode response to json format.
$json_response = json_encode($response);
// Write to log.
debug_log($response, '->');
// Send request to telegram api.
curl_json_request($json_response);
}
/**
* Edit message.
* @param $update
* @param $message
* @param $keys
* @param bool $merge_args
*/
function edit_message($update, $message, $keys, $merge_args = false)
{
if (isset($update['callback_query']['inline_message_id'])) {
editMessageText($update['callback_query']['inline_message_id'], $message, $keys, NULL, $merge_args);
} else {
editMessageText($update['callback_query']['message']['message_id'], $message, $keys, $update['callback_query']['message']['chat']['id'], $merge_args);
}
}
/**
* Send request to telegram api.
* @param $json
* @return mixed
*/
function curl_json_request($json)
{
$curl = curl_init('https://api.telegram.org/bot' . API_KEY . '/');
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
// Write to log.
debug_log($json, '->');
// Execute curl request.
$json_response = curl_exec($curl);
// Write to log.
debug_log($json_response, '<-');
// Decode json response.
$response = json_decode($json_response, true);
// Validate response.
if ($response['ok'] != true || isset($response['update_id'])) {
// Write error to log.
debug_log('ERROR: ' . $json . "\n\n" . $json_response . "\n\n");
}
// Return response.
return $response;
}