8000 Develop by dimamir64 · Pull Request #2 · dimamir64/php-telegram-bot-core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Develop #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
- [:ledger: View file changes][Unreleased]
### Added
### Changed
- Improved error messages for empty input.
- Log update when processing it, not when fetching input.
### Deprecated
### Removed
### Fixed
- `getUpdates` method wrongly sends only 1 Update when a limit of 0 is passed.
### Security

## [0.70.1] - 2020-12-25
Expand Down
17 changes: 3 additions & 14 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,21 +293,15 @@ public static function setClient(ClientInterface $client): void
}

/**
* Set input from custom input or stdin and return it
* Get input from custom input or stdin and return it
*
* @return string
*/
public static function getInput(): string
{
// First check if a custom input has been set, else get the PHP input.
$input = self::$telegram->getCustomInput();
if (empty($input)) {
$input = file_get_contents('php://input');
}

TelegramLog::update($input);

return $input;
return self::$telegram->getCustomInput()
?: file_get_contents('php://input');
}

/**
Expand Down Expand Up @@ -496,11 +490,6 @@ public static function execute(string $action, array $data = []): string
$request_params
);
$result = (string) $response->getBody();

//Logging getUpdates Update
if ($action === 'getUpdates') {
TelegramLog::update($result);
}
} catch (RequestException $e) {
$response = null;
$result = $e->getResponse() ? (string) $e->getResponse()->getBody() : '';
Expand Down
48 changes: 29 additions & 19 deletions src/Telegram.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,13 @@ public function getLastCommandResponse(): ServerResponse
/**
* Handle getUpdates method
*
* @param int $limit
* @param int $timeout
* @param int|null $limit
* @param int|null $timeout
*
* @return ServerResponse
* @throws TelegramException
*/
public function handleGetUpdates(int $limit = 0, int $timeout = 0): ServerResponse
public function handleGetUpdates(?int $limit = null, ?int $timeout = null): ServerResponse
{
if (empty($this->bot_username)) {
throw new TelegramException('Bot Username is not defined!');
Expand All @@ -417,17 +417,24 @@ public function handleGetUpdates(int $limit = 0, int $timeout = 0): ServerRespon

//Take custom input into account.
if ($custom_input = $this->getCustomInput()) {
$response = new ServerResponse(json_decode($custom_input, true), $this->bot_username);
try {
$input = json_decode($this->input, true, 512, JSON_THROW_ON_ERROR);
if (empty($input)) {
throw new TelegramException('Custom input is empty');
}
$response = new ServerResponse($input, $this->bot_username);
} catch (\Throwable $e) {
throw new TelegramException('Invalid custom input JSON: ' . $e->getMessage());
}
} else {
if (DB::isDbConnected() && $last_update = DB::selectTelegramUpdate(1)) {
//Get last update id from the database
$last_update = reset($last_update);

// Get last Update id from the database.
$last_update = reset($last_update);
$this->last_update_id = $last_update['id'] ?? null;
}

if ($this->last_update_id !== null) {
$offset = $this->last_update_id + 1; //As explained in the telegram bot API documentation
$offset = $this->last_update_id + 1; // As explained in the telegram bot API documentation.
}

$response = Request::getUpdates([
Expand All @@ -438,12 +445,13 @@ public function handleGetUpdates(int $limit = 0, int $timeout = 0): ServerRespon
}

if ($response->isOk()) {
$results = $response->getResult();
// Log update.
TelegramLog::update($response->toJson());

//Process all updates
/** @var Update $result */
foreach ($results as $result) {
$this->processUpdate($result);
// Process all updates
/** @var Update $update */
foreach ($response->getResult() as $update) {
$this->processUpdate($update);
}

if (!DB::isDbConnected() && !$custom_input && $this->last_update_id !== null && $offset === 0) {
Expand Down Expand Up @@ -472,15 +480,17 @@ public function handle(): bool
throw new TelegramException('Bot Username is not defined!');
}

$this->input = Request::getInput();

if (empty($this->input)) {
throw new TelegramException('Input is empty!');
$input = Request::getInput();
if (empty($input)) {
throw new TelegramException('Input is empty! The webhook must not be called manually, only by Telegram.');
}

$post = json_decode($this->input, true);
// Log update.
TelegramLog::update($input);

$post = json_decode($input, true);
if (empty($post)) {
throw new TelegramException('Invalid JSON!');
throw new TelegramException('Invalid input JSON! The webhook must not be called manually, only by Telegram.');
}

if ($response = $this->processUpdate(new Update($post, $this->bot_username))) {
Expand Down
0