-
-
Notifications
You must be signed in to change notification settings - Fork 956
Expand file tree
/
Copy pathSendtoallCommand.php
More file actions
113 lines (95 loc) · 2.82 KB
/
SendtoallCommand.php
File metadata and controls
113 lines (95 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PhpTelegramBot\Core\Commands\AdminCommands;
use PhpTelegramBot\Core\Commands\AdminCommand;
use PhpTelegramBot\Core\Entities\Message;
use PhpTelegramBot\Core\Entities\ServerResponse;
use PhpTelegramBot\Core\Exception\TelegramException;
use PhpTelegramBot\Core\Request;
/**
* Admin "/sendtoall" command
*/
class SendtoallCommand extends AdminCommand
{
/**
* @var string
*/
protected $name = 'sendtoall';
/**
* @var string
*/
protected $description = 'Send the message to all of the bot\'s users';
/**
* @var string
*/
protected $usage = '/sendtoall <message to send>';
/**
* @var string
*/
protected $version = '1.5.0';
/**
* @var bool
*/
protected $need_mysql = true;
/**
* Execute command
*
* @return ServerResponse
* @throws TelegramException
*/
public function execute()
{
$text = $this->getMessage()->getText(true);
if ($text === '') {
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 {
$name = $chat->getTitle();
$type = 'chat';
}
} else {
$status = '✖️';
++$failed;
}
++$total;
$text .= $total . ') ' . $status . ' ' . $type . ' ' . $name . PHP_EOL;
}
$text .= 'Delivered: ' . ($total - $failed) . '/' . $total . PHP_EOL;
return $this->replyToChat($text);
}
}