Longman\TelegramBot\Telegram::handleGetUpdates PHP Method

handleGetUpdates() public method

Handle getUpdates method
public handleGetUpdates ( integer | null $limit = null, integer | null $timeout = null ) : ServerResponse
$limit integer | null
$timeout integer | null
return Longman\TelegramBot\Entities\ServerResponse
    public function handleGetUpdates($limit = null, $timeout = null)
    {
        if (!DB::isDbConnected()) {
            return new ServerResponse(['ok' => false, 'description' => 'getUpdates needs MySQL connection!'], $this->bot_name);
        }
        //DB Query
        $last_update = DB::selectTelegramUpdate(1);
        $last_update = reset($last_update);
        //As explained in the telegram bot api documentation
        $offset = isset($last_update['id']) ? $last_update['id'] + 1 : null;
        $response = Request::getUpdates(['offset' => $offset, 'limit' => $limit, 'timeout' => $timeout]);
        if ($response->isOk()) {
            //Process all updates
            /** @var Update $result */
            foreach ((array) $response->getResult() as $result) {
                $this->processUpdate($result);
            }
        }
        return $response;
    }

Usage Example

Beispiel #1
0
    foreach ($triggers as $trigger) {
        $errors[] = $trigger->description;
    }
} catch (Exception $e) {
    // Exception in ZabbixApi catched.
    echo $e->getMessage();
}
try {
    // create Telegram API object
    $telegram = new Telegram($API_KEY, $BOT_NAME);
    $telegram->enableMySQL($credentials);
    $telegram->addCommandsPath($COMMANDS_FOLDER);
    $telegram->setLogRequests(true);
    $telegram->setLogPath('logs/' . $BOT_NAME . '.log');
    $telegram->setLogVerbosity(3);
    if (!empty($errors)) {
        $results = Request::sendToActiveChats('sendMessage', array('text' => "[Zabbix]\nWe have a problem\n" . implode(', ', $errors)), false, true, null, null);
    }
    $ServerResponse = $telegram->handleGetUpdates();
    if ($ServerResponse->isOk()) {
        $n_update = count($ServerResponse->getResult());
        print date('Y-m-d H:i:s', time()) . ' - Processed ' . $n_update . " updates\n";
    } else {
        print date('Y-m-d H:i:s', time()) . " - Fail fetch updates\n";
        print $ServerResponse->printError() . "\n";
    }
} catch (TelegramException $e) {
    // log telegram errors
    print $e->getMessage();
    $log->addError($e->getMessage());
}