Unverified Commit c5a5923d authored by Armando Lüscher's avatar Armando Lüscher Committed by GitHub

Merge pull request #810 from noplanman/improve_admin_send_commands

Improve admin send commands
parents db530e2a e6aca7a3
...@@ -13,6 +13,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c ...@@ -13,6 +13,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
- Updated Travis to use Trusty containers (for HHVM) and add PHP 7.2 to the tests. - Updated Travis to use Trusty containers (for HHVM) and add PHP 7.2 to the tests.
- Add debug log entry instead of throwing an exception for duplicate updates. - Add debug log entry instead of throwing an exception for duplicate updates.
- `Telegram::handleGetUpdates()` can now work without a database connection (not enabled by default). - `Telegram::handleGetUpdates()` can now work without a database connection (not enabled by default).
- Improved `/sendtochannel` and `/sendtoall` commands, using new message helpers.
### Deprecated ### Deprecated
### Removed ### Removed
### Fixed ### Fixed
......
...@@ -38,7 +38,7 @@ class SendtoallCommand extends AdminCommand ...@@ -38,7 +38,7 @@ class SendtoallCommand extends AdminCommand
/** /**
* @var string * @var string
*/ */
protected $version = '1.4.0'; protected $version = '1.5.0';
/** /**
* @var bool * @var bool
...@@ -53,67 +53,59 @@ class SendtoallCommand extends AdminCommand ...@@ -53,67 +53,59 @@ class SendtoallCommand extends AdminCommand
*/ */
public function execute() public function execute()
{ {
$message = $this->getMessage(); $text = $this->getMessage()->getText(true);
$chat_id = $message->getChat()->getId();
$text = $message->getText(true);
if ($text === '') { if ($text === '') {
$text = 'Write the message to send: /sendtoall <message>'; return $this->replyToChat('Usage: ' . $this->getUsage());
} else { }
$results = Request::sendToActiveChats(
'sendMessage', //callback function to execute (see Request.php methods) /** @var ServerResponse[] $results */
['text' => $text], //Param to evaluate the request $results = Request::sendToActiveChats(
[ 'sendMessage', //callback function to execute (see Request.php methods)
'groups' => true, ['text' => $text], //Param to evaluate the request
'supergroups' => true, [
'channels' => false, 'groups' => true,
'users' => true, 'supergroups' => true,
] 'channels' => false,
); 'users' => true,
]
$total = 0; );
$failed = 0;
if (empty($results)) {
$text = 'Message sent to:' . PHP_EOL; return $this->replyToChat('No users or chats found.');
}
/** @var ServerResponse $result */
foreach ($results as $result) { $total = 0;
$name = ''; $failed = 0;
$type = '';
if ($result->isOk()) { $text = 'Message sent to:' . PHP_EOL;
$status = '✔️';
foreach ($results as $result) {
/** @var Message $message */ $name = '';
$message = $result->getResult(); $type = '';
$chat = $message->getChat(); if ($result->isOk()) {
if ($chat->isPrivateChat()) { $status = '✔️';
$name = $chat->getFirstName();
$type = 'user'; /** @var Message $message */
} else { $message = $result->getResult();
$name = $chat->getTitle(); $chat = $message->getChat();
$type = 'chat'; if ($chat->isPrivateChat()) {
} $name = $chat->getFirstName();
$type = 'user';
} else { } else {
$status = '✖️'; $name = $chat->getTitle();
++$failed; $type = 'chat';
} }
++$total; } else {
$status = '✖️';
$text .= $total . ') ' . $status . ' ' . $type . ' ' . $name . PHP_EOL; ++$failed;
} }
$text .= 'Delivered: ' . ($total - $failed) . '/' . $total . PHP_EOL; ++$total;
if ($total === 0) { $text .= $total . ') ' . $status . ' ' . $type . ' ' . $name . PHP_EOL;
$text = 'No users or chats found..';
}
} }
$text .= 'Delivered: ' . ($total - $failed) . '/' . $total . PHP_EOL;
$data = [ return $this->replyToChat($text);
'chat_id' => $chat_id,
'text' => $text,
];
return Request::sendMessage($data);
} }
} }
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