1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the TelegramBot package. |
4
|
|
|
* |
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Longman\TelegramBot; |
12
|
|
|
|
13
|
|
|
use GuzzleHttp\Client; |
14
|
|
|
use GuzzleHttp\Exception\RequestException; |
15
|
|
|
use Longman\TelegramBot\Entities\Update; |
16
|
|
|
use Longman\TelegramBot\Exception\TelegramException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Botan |
20
|
|
|
* |
21
|
|
|
* Integration with http://botan.io statistics service for Telegram bots |
22
|
|
|
*/ |
23
|
|
|
class Botan |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Botan.io API URL |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected static $api_base_uri = 'https://api.botan.io'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Yandex AppMetrica application key |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected static $token = ''; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Guzzle Client object |
41
|
|
|
* |
42
|
|
|
* @var \GuzzleHttp\Client |
43
|
|
|
*/ |
44
|
|
|
private static $client; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The actual command that is going to be reported |
48
|
|
|
* |
49
|
|
|
* Set as public to let the developers either: |
50
|
|
|
* - block tracking from inside commands by setting the value to non-existent command |
51
|
|
|
* - override which command is tracked when commands call other commands with executedCommand() |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
public static $command = ''; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Initialize Botan |
59
|
|
|
* |
60
|
|
|
* @param string $token |
61
|
|
|
* @param integer $timeout |
62
|
|
|
* |
63
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
64
|
|
|
*/ |
65
|
|
|
public static function initializeBotan($token, $timeout = 3) |
66
|
|
|
{ |
67
|
|
|
if (empty($token)) { |
68
|
|
|
throw new TelegramException('Botan token is empty!'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (!is_numeric($timeout)) { |
72
|
|
|
throw new TelegramException('Timeout must be a number!'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
self::$token = $token; |
76
|
|
|
self::$client = new Client(['base_uri' => self::$api_base_uri, 'timeout' => $timeout]); |
77
|
|
|
|
78
|
|
|
BotanDB::initializeBotanDb(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Lock function to make sure only the first command is reported (the one user requested) |
83
|
|
|
* |
84
|
|
|
* This is in case commands are calling other commands with executedCommand() |
85
|
|
|
* |
86
|
|
|
* @param string $command |
87
|
|
|
*/ |
88
|
|
|
public static function lock($command = '') |
89
|
|
|
{ |
90
|
|
|
if (empty(self::$command)) { |
91
|
|
|
self::$command = strtolower($command); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Track function |
97
|
|
|
* |
98
|
|
|
* @param \Longman\TelegramBot\Entities\Update $update |
99
|
|
|
* @param string $command |
100
|
|
|
* |
101
|
|
|
* @return bool|string |
102
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
103
|
|
|
*/ |
104
|
|
|
public static function track(Update $update, $command = '') |
105
|
|
|
{ |
106
|
|
|
if (empty(self::$token) || strtolower($command) !== self::$command) { |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if (empty($update)) { |
111
|
|
|
throw new TelegramException('Update object is empty!'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// Release the lock in case someone runs getUpdates in foreach loop |
115
|
|
|
self::$command = ''; |
116
|
|
|
|
117
|
|
|
// For now, this is the only way |
118
|
|
|
$update_data = (array) $update; |
119
|
|
|
|
120
|
|
|
$data = []; |
121
|
|
|
if ($update->getMessage()) { |
122
|
|
|
$data = $update_data['message']; |
123
|
|
|
$event_name = 'Generic Message'; |
124
|
|
|
|
125
|
|
|
if (!empty($data['entities']) && is_array($data['entities'])) { |
126
|
|
|
foreach ($data['entities'] as $entity) { |
127
|
|
|
if ($entity['type'] === 'bot_command' && $entity['offset'] === 0) { |
128
|
|
|
if (strtolower($command) === 'generic') { |
129
|
|
|
$command = 'Generic'; |
130
|
|
|
} elseif (strtolower($command) === 'genericmessage') { |
131
|
|
|
$command = 'Generic Message'; |
132
|
|
|
} else { |
133
|
|
|
$command = '/' . strtolower($command); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$event_name = 'Command (' . $command . ')'; |
137
|
|
|
break; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} elseif ($update->getEditedMessage()) { |
142
|
|
|
$data = $update_data['edited_message']; |
143
|
|
|
$event_name = 'Edited Message'; |
144
|
|
|
} elseif ($update->getChannelPost()) { |
145
|
|
|
$data = $update_data['channel_post']; |
146
|
|
|
$event_name = 'Channel Post'; |
147
|
|
|
} elseif ($update->getEditedChannelPost()) { |
148
|
|
|
$data = $update_data['edited_channel_post']; |
149
|
|
|
$event_name = 'Edited Channel Post'; |
150
|
|
|
} elseif ($update->getInlineQuery()) { |
151
|
|
|
$data = $update_data['inline_query']; |
152
|
|
|
$event_name = 'Inline Query'; |
153
|
|
|
} elseif ($update->getChosenInlineResult()) { |
154
|
|
|
$data = $update_data['chosen_inline_result']; |
155
|
|
|
$event_name = 'Chosen Inline Result'; |
156
|
|
|
} elseif ($update->getCallbackQuery()) { |
157
|
|
|
$data = $update_data['callback_query']; |
158
|
|
|
$event_name = 'Callback Query'; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if (empty($event_name)) { |
162
|
|
|
return false; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
// In case there is no from field (channel posts) assign chat id |
166
|
|
|
if (isset($data['from']['id'])) { |
167
|
|
|
$uid = $data['from']['id']; |
168
|
|
|
} elseif (isset($data['chat']['id'])) { |
169
|
|
|
$uid = $data['chat']['id']; |
170
|
|
|
} else { |
171
|
|
|
$uid = 0; // if that fails too assign id = 0 |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
try { |
175
|
|
|
$response = self::$client->request( |
176
|
|
|
'POST', |
177
|
|
|
str_replace( |
178
|
|
|
['#TOKEN', '#UID', '#NAME'], |
179
|
|
|
[self::$token, $uid, urlencode($event_name)], |
180
|
|
|
'/track?token=#TOKEN&uid=#UID&name=#NAME' |
181
|
|
|
), |
182
|
|
|
[ |
183
|
|
|
'headers' => [ |
184
|
|
|
'Content-Type' => 'application/json' |
185
|
|
|
], |
186
|
|
|
'json' => $data |
187
|
|
|
] |
188
|
|
|
); |
189
|
|
|
|
190
|
|
|
$response = (string) $response->getBody(); |
191
|
|
|
} catch (RequestException $e) { |
192
|
|
|
$response = ($e->getResponse()) ? (string) $e->getResponse()->getBody() : ''; |
193
|
|
|
} finally { |
194
|
|
|
$responseData = json_decode($response, true); |
195
|
|
|
|
196
|
|
|
if ($responseData['status'] !== 'accepted') { |
197
|
|
|
TelegramLog::debug("Botan.io API replied with error:\n$response\n\n"); |
198
|
|
|
return false; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $responseData; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Url Shortener function |
207
|
|
|
* |
208
|
|
|
* @param string $url |
209
|
|
|
* @param integer $user_id |
210
|
|
|
* |
211
|
|
|
* @return string |
212
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
213
|
|
|
*/ |
214
|
|
|
public static function shortenUrl($url, $user_id) |
215
|
|
|
{ |
216
|
|
|
if (empty(self::$token)) { |
217
|
|
|
return $url; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
if (empty($user_id)) { |
221
|
|
|
throw new TelegramException('User id is empty!'); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$cached = BotanDB::selectShortUrl($user_id, $url); |
225
|
|
|
|
226
|
|
|
if (!empty($cached)) { |
227
|
|
|
return $cached; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
try { |
231
|
|
|
$response = self::$client->request( |
232
|
|
|
'POST', |
233
|
|
|
str_replace( |
234
|
|
|
['#TOKEN', '#UID', '#URL'], |
235
|
|
|
[self::$token, $user_id, urlencode($url)], |
236
|
|
|
'/s/?token=#TOKEN&user_ids=#UID&url=#URL' |
237
|
|
|
) |
238
|
|
|
); |
239
|
|
|
|
240
|
|
|
$response = (string) $response->getBody(); |
241
|
|
|
} catch (RequestException $e) { |
242
|
|
|
$response = ($e->getResponse()) ? (string) $e->getResponse()->getBody() : ''; |
243
|
|
|
} finally { |
244
|
|
|
if (!filter_var($response, FILTER_VALIDATE_URL) === false) { |
245
|
|
|
BotanDB::insertShortUrl($user_id, $url, $response); |
246
|
|
|
return $response; |
247
|
|
|
} else { |
248
|
|
|
TelegramLog::error("Botan.io URL shortening failed - API replied with error:\n$response\n\n"); |
249
|
|
|
return $url; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|