Commit 63a948dd authored by Jack'lul's avatar Jack'lul

Apply suggestions from @noplanman

parent 4be04d4a
...@@ -23,9 +23,8 @@ $BOT_NAME = 'username_bot'; ...@@ -23,9 +23,8 @@ $BOT_NAME = 'username_bot';
// 'database' => 'dbname', // 'database' => 'dbname',
//]; //];
// Your command(s) to run, use associative array to pass arguments // Your command(s) to run, pass it just like in a message (arguments supported)
$commands = ['whoami', 'echo']; $commands = ['/whoami', '/echo I\'m a bot!'];
//$commands = ['echo' => 'I\'m a bot!'];
try { try {
// Create Telegram API object // Create Telegram API object
...@@ -63,9 +62,6 @@ try { ...@@ -63,9 +62,6 @@ try {
// Run user selected commands // Run user selected commands
$telegram->runCommands($commands); $telegram->runCommands($commands);
// Run user selected commands and modify update array
//$telegram->runCommands($commands, ['message' => ['text' => 'Parameter']]);
} catch (Longman\TelegramBot\Exception\TelegramException $e) { } catch (Longman\TelegramBot\Exception\TelegramException $e) {
// Silence is golden! // Silence is golden!
//echo $e; //echo $e;
......
...@@ -835,13 +835,12 @@ class Telegram ...@@ -835,13 +835,12 @@ class Telegram
* Run provided commands * Run provided commands
* *
* @param array $commands * @param array $commands
* @param array $update
* *
* @throws TelegramException * @throws TelegramException
*/ */
public function runCommands($commands, array $update = []) public function runCommands($commands)
{ {
if (!is_array($commands)) { if (!is_array($commands) || empty($commands)) {
throw new TelegramException('No command(s) provided!'); throw new TelegramException('No command(s) provided!');
} }
...@@ -849,49 +848,39 @@ class Telegram ...@@ -849,49 +848,39 @@ class Telegram
$result = Request::getMe()->getResult(); $result = Request::getMe()->getResult();
if (!$result) { if (!$result->getId()) {
throw new TelegramException('Received invalid getMe result!'); throw new TelegramException('Received empty/invalid getMe result!');
} }
$bot_id = $result->getId(); $bot_id = $result->getId();
$bot_name = $result->getFirstName(); $bot_name = $result->getFirstName();
$bot_username = $result->getUsername();
$update_template = [
'update_id' => 0,
'message' => [
'message_id' => 0,
'from' => [
'id' => $bot_id,
'first_name' => $bot_name
],
'date' => time(),
'chat' => [
'id' => $bot_id,
'type' => 'private',
],
'text' => ''
]
];
$update = array_merge($update_template, $update);
$this->enableAdmin($bot_id); // Give bot access to admin commands $this->enableAdmin($bot_id); // Give bot access to admin commands
$this->getCommandsList(); // Load full commands list $this->getCommandsList(); // Load full commands list
foreach ($commands as $command => $parameter) { foreach ($commands as $command) {
if (is_numeric($command)) { // if array/key is not associative $command will be integer and $parameter will be the actual command! $this->update = new Update(
$command = $parameter; [
} 'update_id' => 0,
'message' => [
$temp_update = $update; 'message_id' => 0,
'from' => [
if ($parameter && $parameter != $command) { 'id' => $bot_id,
$temp_update['message']['text'] = $parameter; 'first_name' => $bot_name,
} 'username' => $bot_username
],
$this->update = new Update($temp_update); // this prevents commands throwing exceptions about missing Update objects 'date' => time(),
'chat' => [
'id' => $bot_id,
'type' => 'private',
],
'text' => $command
]
]
);
$this->executeCommand($command); $this->executeCommand($this->update->getMessage()->getCommand());
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment