Skip to content

Commit c5a5923

Browse files
authored
Merge pull request #810 from noplanman/improve_admin_send_commands
Improve admin send commands
2 parents db530e2 + e6aca7a commit c5a5923

File tree

3 files changed

+149
-135
lines changed

3 files changed

+149
-135
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
1313
- Updated Travis to use Trusty containers (for HHVM) and add PHP 7.2 to the tests.
1414
- Add debug log entry instead of throwing an exception for duplicate updates.
1515
- `Telegram::handleGetUpdates()` can now work without a database connection (not enabled by default).
16+
- Improved `/sendtochannel` and `/sendtoall` commands, using new message helpers.
1617
### Deprecated
1718
### Removed
1819
### Fixed

src/Commands/AdminCommands/SendtoallCommand.php

Lines changed: 47 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class SendtoallCommand extends AdminCommand
3838
/**
3939
* @var string
4040
*/
41-
protected $version = '1.4.0';
41+
protected $version = '1.5.0';
4242

4343
/**
4444
* @var bool
@@ -53,67 +53,59 @@ class SendtoallCommand extends AdminCommand
5353
*/
5454
public function execute()
5555
{
56-
$message = $this->getMessage();
57-
58-
$chat_id = $message->getChat()->getId();
59-
$text = $message->getText(true);
56+
$text = $this->getMessage()->getText(true);
6057

6158
if ($text === '') {
62-
$text = 'Write the message to send: /sendtoall <message>';
63-
} else {
64-
$results = Request::sendToActiveChats(
65-
'sendMessage', //callback function to execute (see Request.php methods)
66-
['text' => $text], //Param to evaluate the request
67-
[
68-
'groups' => true,
69-
'supergroups' => true,
70-
'channels' => false,
71-
'users' => true,
72-
]
73-
);
74-
75-
$total = 0;
76-
$failed = 0;
77-
78-
$text = 'Message sent to:' . PHP_EOL;
79-
80-
/** @var ServerResponse $result */
81-
foreach ($results as $result) {
82-
$name = '';
83-
$type = '';
84-
if ($result->isOk()) {
85-
$status = '✔️';
86-
87-
/** @var Message $message */
88-
$message = $result->getResult();
89-
$chat = $message->getChat();
90-
if ($chat->isPrivateChat()) {
91-
$name = $chat->getFirstName();
92-
$type = 'user';
93-
} else {
94-
$name = $chat->getTitle();
95-
$type = 'chat';
96-
}
59+
return $this->replyToChat('Usage: ' . $this->getUsage());
60+
}
61+
62+
/** @var ServerResponse[] $results */
63+
$results = Request::sendToActiveChats(
64+
'sendMessage', //callback function to execute (see Request.php methods)
65+
['text' => $text], //Param to evaluate the request
66+
[
67+
'groups' => true,
68+
'supergroups' => true,
69+
'channels' => false,
70+
'users' => true,
71+
]
72+
);
73+
74+
if (empty($results)) {
75+
return $this->replyToChat('No users or chats found.');
76+
}
77+
78+
$total = 0;
79+
$failed = 0;
80+
81+
$text = 'Message sent to:' . PHP_EOL;
82+
83+
foreach ($results as $result) {
84+
$name = '';
85+
$type = '';
86+
if ($result->isOk()) {
87+
$status = '✔️';
88+
89+
/** @var Message $message */
90+
$message = $result->getResult();
91+
$chat = $message->getChat();
92+
if ($chat->isPrivateChat()) {
93+
$name = $chat->getFirstName();
94+
$type = 'user';
9795
} else {
98-
$status = '✖️';
99-
++$failed;
96+
$name = $chat->getTitle();
97+
$type = 'chat';
10098
}
101-
++$total;
102-
103-
$text .= $total . ') ' . $status . ' ' . $type . ' ' . $name . PHP_EOL;
99+
} else {
100+
$status = '✖️';
101+
++$failed;
104102
}
105-
$text .= 'Delivered: ' . ($total - $failed) . '/' . $total . PHP_EOL;
103+
++$total;
106104

107-
if ($total === 0) {
108-
$text = 'No users or chats found..';
109-
}
105+
$text .= $total . ') ' . $status . ' ' . $type . ' ' . $name . PHP_EOL;
110106
}
107+
$text .= 'Delivered: ' . ($total - $failed) . '/' . $total . PHP_EOL;
111108

112-
$data = [
113-
'chat_id' => $chat_id,
114-
'text' => $text,
115-
];
116-
117-
return Request::sendMessage($data);
109+
return $this->replyToChat($text);
118110
}
119111
}

0 commit comments

Comments
 (0)