Skip to content

Commit 5e99f4a

Browse files
committed
Fix Telegram::runCommands for User and nested calls
1 parent a3f329d commit 5e99f4a

File tree

2 files changed

+44
-29
lines changed

2 files changed

+44
-29
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
1212
### Deprecated
1313
### Removed
1414
### Fixed
15+
- Nested and user-triggered `Telegram::runCommands`.
1516
### Security
1617

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

src/Telegram.php

Lines changed: 43 additions & 29 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;
@@ -1125,38 +1127,49 @@ public function runCommands(array $commands): void
11251127

11261128
$this->run_commands = true;
11271129

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

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

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-
}
1155+
// Give Bot access to admin commands.
1156+
$this->enableAdmin($from->getId());
11411157

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

1145-
$newUpdate = static function ($text = '') use ($bot_id, $bot_name, $bot_username) {
1165+
$newUpdate = static function ($text = '') use ($from, $chat) {
11461166
return new Update([
1147-
'update_id' => 0,
1167+
'update_id' => -1,
11481168
'message' => [
1149-
'message_id' => 0,
1150-
'from' => [
1151-
'id' => $bot_id,
1152-
'first_name' => $bot_name,
1153-
'username' => $bot_username,
1154-
],
1169+
'message_id' => -1,
11551170
'date' => time(),
1156-
'chat' => [
1157-
'id' => $bot_id,
1158-
'type' => 'private',
1159-
],
1171+
'from' => json_decode($from->toJson(), true),
1172+
'chat' => json_decode($chat->toJson(), true),
11601173
'text' => $text,
11611174
],
11621175
]);
@@ -1165,13 +1178,14 @@ public function runCommands(array $commands): void
11651178
foreach ($commands as $command) {
11661179
$this->update = $newUpdate($command);
11671180

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

11731184
$this->executeCommand($this->update->getMessage()->getCommand());
11741185
}
1186+
1187+
// Reset Update to initial context.
1188+
$this->update = $userUpdate;
11751189
}
11761190

11771191
/**

0 commit comments

Comments
 (0)