Skip to content

Commit 4b6bc88

Browse files
authored
Merge pull request #1223 from noplanman/1214-fix-nested-run-commands
Fix Telegram::runCommands for User and nested calls
2 parents 236d3b5 + 5bf9688 commit 4b6bc88

File tree

2 files changed

+53
-31
lines changed

2 files changed

+53
-31
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
99
### Added
1010
- Bot API 5.2 (Payments 2.0).
1111
### Changed
12+
- `Telegram::runCommands` returns array of `ServerResponse` objects of executed commands.
1213
### Deprecated
1314
### Removed
1415
### Fixed
1516
- Regex for namespace extraction from custom command classes.
17+
- Nested and user-triggered `Telegram::runCommands`.
1618
### Security
1719

1820
## [0.72.0] - 2021-04-16

src/Telegram.php

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
use Longman\TelegramBot\Commands\Command;
2121
use Longman\TelegramBot\Commands\SystemCommand;
2222
use Longman\TelegramBot\Commands\UserCommand;
23+
use Longman\TelegramBot\Entities\Chat;
2324
use Longman\TelegramBot\Entities\ServerResponse;
2425
use Longman\TelegramBot\Entities\Update;
26+
use Longman\TelegramBot\Entities\User;
2527
use Longman\TelegramBot\Exception\TelegramException;
2628
use PDO;
2729
use RecursiveDirectoryIterator;
@@ -1115,63 +1117,81 @@ public function enableLimiter(array $options = []): Telegram
11151117
*
11161118
* @param array $commands
11171119
*
1120+
* @return ServerResponse[]
1121+
*
11181122
* @throws TelegramException
11191123
*/
1120-
public function runCommands(array $commands): void
1124+
public function runCommands(array $commands): array
11211125
{
11221126
if (empty($commands)) {
11231127
throw new TelegramException('No command(s) provided!');
11241128
}
11251129

11261130
$this->run_commands = true;
11271131

1128-
$result = Request::getMe();
1132+
// Check if this request has a user Update / comes from Telegram.
1133+
if ($userUpdate = $this->update) {
1134+
$from = $this->update->getMessage()->getFrom();
1135+
$chat = $this->update->getMessage()->getChat();
1136+
} else {
1137+
// Fall back to the Bot user.
1138+
$from = new User([
1139+
'id' => $this->getBotId(),
1140+
'first_name' => $this->getBotUsername(),
1141+
'username' => $this->getBotUsername(),
1142+
]);
11291143

1130-
if ($result->isOk()) {
1131-
$result = $result->getResult();
1144+
// Try to get "live" Bot info.
1145+
$response = Request::getMe();
1146+
if ($response->isOk()) {
1147+
/** @var User $result */
1148+
$result = $response->getResult();
1149+
1150+
$from = new User([
1151+
'id' => $result->getId(),
1152+
'first_name' => $result->getFirstName(),
1153+
'username' => $result->getUsername(),
1154+
]);
1155+
}
11321156

1133-
$bot_id = $result->getId();
1134-
$bot_name = $result->getFirstName();
1135-
$bot_username = $result->getUsername();
1136-
} else {
1137-
$bot_id = $this->getBotId();
1138-
$bot_name = $this->getBotUsername();
1139-
$bot_username = $this->getBotUsername();
1140-
}
1157+
// Give Bot access to admin commands.
1158+
$this->enableAdmin($from->getId());
11411159

1142-
// Give bot access to admin commands
1143-
$this->enableAdmin($bot_id);
1160+
// Lock the bot to a private chat context.
1161+
$chat = new Chat([
1162+
'id' => $from->getId(),
1163+
'type' => 'private',
1164+
]);
1165+
}
11441166

1145-
$newUpdate = static function ($text = '') use ($bot_id, $bot_name, $bot_username) {
1167+
$newUpdate = static function ($text = '') use ($from, $chat) {
11461168
return new Update([
1147-
'update_id' => 0,
1169+
'update_id' => -1,
11481170
'message' => [
1149-
'message_id' => 0,
1150-
'from' => [
1151-
'id' => $bot_id,
1152-
'first_name' => $bot_name,
1153-
'username' => $bot_username,
1154-
],
1171+
'message_id' => -1,
11551172
'date' => time(),
1156-
'chat' => [
1157-
'id' => $bot_id,
1158-
'type' => 'private',
1159-
],
1173+
'from' => json_decode($from->toJson(), true),
1174+
'chat' => json_decode($chat->toJson(), true),
11601175
'text' => $text,
11611176
],
11621177
]);
11631178
};
11641179

1180+
$responses = [];
1181+
11651182
foreach ($commands as $command) {
11661183
$this->update = $newUpdate($command);
11671184

1168-
// Load up-to-date commands list
1169-
if (empty($this->commands_objects)) {
1170-
$this->commands_objects = $this->getCommandsList();
1171-
}
1185+
// Refresh commands list for new Update object.
1186+
$this->commands_objects = $this->getCommandsList();
11721187

1173-
$this->executeCommand($this->update->getMessage()->getCommand());
1188+
$responses[] = $this->executeCommand($this->update->getMessage()->getCommand());
11741189
}
1190+
1191+
// Reset Update to initial context.
1192+
$this->update = $userUpdate;
1193+
1194+
return $responses;
11751195
}
11761196

11771197
/**

0 commit comments

Comments
 (0)