SendtoallCommand.php 2.92 KB
Newer Older
1
<?php
2
/**
3 4 5 6 7 8
 * 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.
9 10
 */

11
namespace Longman\TelegramBot\Commands\AdminCommands;
12

13
use Longman\TelegramBot\Commands\AdminCommand;
14
use Longman\TelegramBot\Request;
15

16 17 18
/**
 * Admin "/sendtoall" command
 */
19
class SendtoallCommand extends AdminCommand
20
{
21 22
    /**#@+
     * {@inheritdoc}
23
     */
24
    protected $name = 'sendtoall';
25
    protected $description = 'Send the message to all the user\'s bot';
26
    protected $usage = '/sendtoall <message to send>';
27
    protected $version = '1.2.1';
28
    protected $need_mysql = true;
29
    /**#@-*/
30

31 32 33 34 35 36 37
    /**
     * Execute command
     *
     * @todo Don't use empty, as a string of '0' is regarded to be empty
     *
     * @return boolean
     */
38 39 40 41 42
    public function execute()
    {
        $message = $this->getMessage();

        $chat_id = $message->getChat()->getId();
43
        $text = $message->getText(true);
44 45

        if (empty($text)) {
46
            $text = 'Write the message to send: /sendtoall <message>';
47
        } else {
MBoretto's avatar
MBoretto committed
48
            $results = Request::sendToActiveChats(
49
                'sendMessage', //callback function to execute (see Request.php methods)
50
                ['text' => $text], //Param to evaluate the request
MBoretto's avatar
MBoretto committed
51 52
                true, //Send to groups (group chat)
                true, //Send to super groups chats (super group chat)
53 54 55 56 57 58 59 60
                true, //Send to users (single chat)
                null, //'yyyy-mm-dd hh:mm:ss' date range from
                null  //'yyyy-mm-dd hh:mm:ss' date range to
            );

            $tot = 0;
            $fail = 0;

61
            $text = 'Message sent to:' . "\n";
62 63 64 65 66 67 68 69 70
            foreach ($results as $result) {
                $status = '';
                $type = '';
                print_r($result);
                if ($result->isOk()) {
                    $status = '✔️';

                    $ServerResponse = $result->getResult();
                    $chat = $ServerResponse->getChat();
71
                    if ($chat->isPrivateChat()) {
72 73 74 75 76 77 78 79 80 81 82 83
                        $name = $chat->getFirstName();
                        $type = 'user';
                    } else {
                        $name = $chat->getTitle();
                        $type = 'chat';
                    }
                } else {
                    $status = '✖️';
                    ++$fail;
                }
                ++$tot;

84
                $text .= $tot . ') ' . $status . ' ' . $type . ' ' . $name . "\n";
85
            }
86
            $text .= 'Delivered: ' . ($tot - $fail) . '/' . $tot . "\n";
87 88 89 90

            if ($tot === 0) {
                $text = 'No users or chats found..';
            }
91 92
        }

93 94 95 96
        $data = [
            'chat_id' => $chat_id,
            'text'    => $text,
        ];
97

98
        return Request::sendMessage($data);
99 100
    }
}