|
20 | 20 | use Longman\TelegramBot\Commands\Command; |
21 | 21 | use Longman\TelegramBot\Commands\SystemCommand; |
22 | 22 | use Longman\TelegramBot\Commands\UserCommand; |
| 23 | +use Longman\TelegramBot\Entities\Chat; |
23 | 24 | use Longman\TelegramBot\Entities\ServerResponse; |
24 | 25 | use Longman\TelegramBot\Entities\Update; |
| 26 | +use Longman\TelegramBot\Entities\User; |
25 | 27 | use Longman\TelegramBot\Exception\TelegramException; |
26 | 28 | use PDO; |
27 | 29 | use RecursiveDirectoryIterator; |
@@ -1115,63 +1117,81 @@ public function enableLimiter(array $options = []): Telegram |
1115 | 1117 | * |
1116 | 1118 | * @param array $commands |
1117 | 1119 | * |
| 1120 | + * @return ServerResponse[] |
| 1121 | + * |
1118 | 1122 | * @throws TelegramException |
1119 | 1123 | */ |
1120 | | - public function runCommands(array $commands): void |
| 1124 | + public function runCommands(array $commands): array |
1121 | 1125 | { |
1122 | 1126 | if (empty($commands)) { |
1123 | 1127 | throw new TelegramException('No command(s) provided!'); |
1124 | 1128 | } |
1125 | 1129 |
|
1126 | 1130 | $this->run_commands = true; |
1127 | 1131 |
|
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 | + ]); |
1129 | 1143 |
|
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 | + } |
1132 | 1156 |
|
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()); |
1141 | 1159 |
|
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 | + } |
1144 | 1166 |
|
1145 | | - $newUpdate = static function ($text = '') use ($bot_id, $bot_name, $bot_username) { |
| 1167 | + $newUpdate = static function ($text = '') use ($from, $chat) { |
1146 | 1168 | return new Update([ |
1147 | | - 'update_id' => 0, |
| 1169 | + 'update_id' => -1, |
1148 | 1170 | '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, |
1155 | 1172 | '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), |
1160 | 1175 | 'text' => $text, |
1161 | 1176 | ], |
1162 | 1177 | ]); |
1163 | 1178 | }; |
1164 | 1179 |
|
| 1180 | + $responses = []; |
| 1181 | + |
1165 | 1182 | foreach ($commands as $command) { |
1166 | 1183 | $this->update = $newUpdate($command); |
1167 | 1184 |
|
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(); |
1172 | 1187 |
|
1173 | | - $this->executeCommand($this->update->getMessage()->getCommand()); |
| 1188 | + $responses[] = $this->executeCommand($this->update->getMessage()->getCommand()); |
1174 | 1189 | } |
| 1190 | + |
| 1191 | + // Reset Update to initial context. |
| 1192 | + $this->update = $userUpdate; |
| 1193 | + |
| 1194 | + return $responses; |
1175 | 1195 | } |
1176 | 1196 |
|
1177 | 1197 | /** |
|
0 commit comments