public static function track($input, $command = '')
{
if (empty(self::$token) || $command !== self::$command) {
return false;
}
if (empty($input)) {
throw new TelegramException('Input is empty!');
}
self::$command = '';
$obj = json_decode($input, true);
$data = [];
if (isset($obj['message'])) {
$data = $obj['message'];
$event_name = 'Message';
if (isset($obj['message']['entities']) && is_array($obj['message']['entities'])) {
foreach ($obj['message']['entities'] as $entity) {
if ($entity['type'] === 'bot_command' && $entity['offset'] === 0) {
if (strtolower($command) === 'generic') {
$command = 'Generic';
} elseif (strtolower($command) === 'genericmessage') {
$command = 'Generic Message';
} else {
$command = '/' . strtolower($command);
}
$event_name = 'Command (' . $command . ')';
break;
}
}
}
} elseif (isset($obj['edited_message'])) {
$data = $obj['edited_message'];
$event_name = 'Edited Message';
} elseif (isset($obj['channel_post'])) {
$data = $obj['channel_post'];
$event_name = 'Channel Message';
} elseif (isset($obj['edited_channel_post'])) {
$data = $obj['edited_channel_post'];
$event_name = 'Edited Channel Message';
} elseif (isset($obj['inline_query'])) {
$data = $obj['inline_query'];
$event_name = 'Inline Query';
} elseif (isset($obj['chosen_inline_result'])) {
$data = $obj['chosen_inline_result'];
$event_name = 'Chosen Inline Result';
} elseif (isset($obj['callback_query'])) {
$data = $obj['callback_query'];
$event_name = 'Callback Query';
}
if (empty($event_name)) {
return false;
}
$uid = $data['from']['id'];
$request = str_replace(['#TOKEN', '#UID', '#NAME'], [self::$token, $uid, urlencode($event_name)], self::$track_url);
$options = ['http' => ['header' => 'Content-Type: application/json', 'method' => 'POST', 'content' => json_encode($data), 'ignore_errors' => true]];
$context = stream_context_create($options);
$response = @file_get_contents($request, false, $context);
$responseData = json_decode($response, true);
if ($responseData['status'] !== 'accepted') {
TelegramLog::debug('Botan.io API replied with error: ' . $response);
}
return $responseData;
}