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
- 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.
- `Telegram::handleGetUpdates()` can now work without a database connection (not enabled by default).
- Improved `/sendtochannel` and `/sendtoall` commands, using new message helpers.
### Deprecated
### Removed
### Fixed
......
......@@ -38,7 +38,7 @@ class SendtoallCommand extends AdminCommand
/**
* @var string
*/
protected $version = '1.4.0';
protected $version = '1.5.0';
/**
* @var bool
......@@ -53,67 +53,59 @@ class SendtoallCommand extends AdminCommand
*/
public function execute()
{
$message = $this->getMessage();
$chat_id = $message->getChat()->getId();
$text = $message->getText(true);
$text = $this->getMessage()->getText(true);
if ($text === '') {
$text = 'Write the message to send: /sendtoall <message>';
} else {
$results = Request::sendToActiveChats(
'sendMessage', //callback function to execute (see Request.php methods)
['text' => $text], //Param to evaluate the request
[
'groups' => true,
'supergroups' => true,
'channels' => false,
'users' => true,
]
);
$total = 0;
$failed = 0;
$text = 'Message sent to:' . PHP_EOL;
/** @var ServerResponse $result */
foreach ($results as $result) {
$name = '';
$type = '';
if ($result->isOk()) {
$status = '✔️';
/** @var Message $message */
$message = $result->getResult();
$chat = $message->getChat();
if ($chat->isPrivateChat()) {
$name = $chat->getFirstName();
$type = 'user';
} else {
$name = $chat->getTitle();
$type = 'chat';
}
return $this->replyToChat('Usage: ' . $this->getUsage());
}
/** @var ServerResponse[] $results */
$results = Request::sendToActiveChats(
'sendMessage', //callback function to execute (see Request.php methods)
['text' => $text], //Param to evaluate the request
[
'groups' => true,
'supergroups' => true,
'channels' => false,
'users' => true,
]
);
if (empty($results)) {
return $this->replyToChat('No users or chats found.');
}
$total = 0;
$failed = 0;
$text = 'Message sent to:' . PHP_EOL;
foreach ($results as $result) {
$name = '';
$type = '';
if ($result->isOk()) {
$status = '✔️';
/** @var Message $message */
$message = $result->getResult();
$chat = $message->getChat();
if ($chat->isPrivateChat()) {
$name = $chat->getFirstName();
$type = 'user';
} else {
$status = '✖️';
++$failed;
$name = $chat->getTitle();
$type = 'chat';
}
++$total;
$text .= $total . ') ' . $status . ' ' . $type . ' ' . $name . PHP_EOL;
} else {
$status = '✖️';
++$failed;
}
$text .= 'Delivered: ' . ($total - $failed) . '/' . $total . PHP_EOL;
++$total;
if ($total === 0) {
$text = 'No users or chats found..';
}
$text .= $total . ') ' . $status . ' ' . $type . ' ' . $name . PHP_EOL;
}
$text .= 'Delivered: ' . ($total - $failed) . '/' . $total . PHP_EOL;
$data = [
'chat_id' => $chat_id,
'text' => $text,
];
return Request::sendMessage($data);
return $this->replyToChat($text);
}
}
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