Longman\TelegramBot\Botan::track PHP Method

track() public static method

Track function
public static track ( string $input, string $command = '' ) : boolean | string
$input string
$command string
return boolean | string
    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;
    }

Usage Example

 /**
  * Execute /command
  *
  * @param string $command
  *
  * @return mixed
  */
 public function executeCommand($command)
 {
     $command_obj = $this->getCommandObject($command);
     if (!$command_obj || !$command_obj->isEnabled()) {
         //Failsafe in case the Generic command can't be found
         if ($command === 'Generic') {
             throw new TelegramException('Generic command missing!');
         }
         //Handle a generic command or non existing one
         $this->last_command_response = $this->executeCommand('Generic');
     } else {
         //Botan.io integration, make sure only the command user executed is reported
         if ($this->botan_enabled) {
             Botan::lock($command);
         }
         //execute() method is executed after preExecute()
         //This is to prevent executing a DB query without a valid connection
         $this->last_command_response = $command_obj->preExecute();
         //Botan.io integration, send report after executing the command
         if ($this->botan_enabled) {
             Botan::track($this->update, $command);
         }
     }
     return $this->last_command_response;
 }