diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c648edec..3d80e06a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c - Documents can be sent by providing its contents via Psr7 stream (as opposed to passing a file path). - Allow setting a custom Guzzle HTTP Client for requests (#511). ### Changed +- [:exclamation:][unreleased-bc-chats-params-array] `Request::sendToActiveChats` and `DB::selectChats` now accept parameters as an options array. ### Deprecated ### Removed - [:exclamation:][unreleased-bc-up-download-directory] Upload and download directories are not set any more by default and must be set manually. @@ -105,5 +106,6 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c ### Deprecated - Move `hideKeyboard` to `removeKeyboard`. +[unreleased-bc-chats-params-array]: https://github.com/php-telegram-bot/core/wiki/Breaking-backwards-compatibility#unreleased [unreleased-bc-up-download-directory]: https://github.com/php-telegram-bot/core/wiki/Breaking-backwards-compatibility#unreleased [0.44.0-bc-update-content-type]: https://github.com/php-telegram-bot/core/wiki/Breaking-backwards-compatibility#update-getupdatecontent diff --git a/src/Commands/AdminCommands/ChatsCommand.php b/src/Commands/AdminCommands/ChatsCommand.php index 26a6ae523..b5c02d1c7 100644 --- a/src/Commands/AdminCommands/ChatsCommand.php +++ b/src/Commands/AdminCommands/ChatsCommand.php @@ -35,7 +35,7 @@ class ChatsCommand extends AdminCommand /** * @var string */ - protected $version = '1.1.0'; + protected $version = '1.2.0'; /** * @var bool @@ -45,7 +45,7 @@ class ChatsCommand extends AdminCommand /** * Command execute method * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public function execute() @@ -55,19 +55,18 @@ public function execute() $chat_id = $message->getChat()->getId(); $text = trim($message->getText(true)); - $results = DB::selectChats( - true, //Select groups (group chat) - true, //Select supergroups (super group chat) - true, //Select users (single chat) - null, //'yyyy-mm-dd hh:mm:ss' date range from - null, //'yyyy-mm-dd hh:mm:ss' date range to - null, //Specific chat_id to select - ($text === '' || $text === '*') ? null : $text //Text to search in user/group name - ); + $results = DB::selectChats([ + 'groups' => true, + 'supergroups' => true, + 'channels' => true, + 'users' => true, + 'text' => ($text === '' || $text === '*') ? null : $text //Text to search in user/group name + ]); - $user_chats = 0; - $group_chats = 0; - $super_group_chats = 0; + $user_chats = 0; + $group_chats = 0; + $supergroup_chats = 0; + $channel_chats = 0; if ($text === '') { $text_back = ''; @@ -100,24 +99,31 @@ public function execute() $text_back .= '- S ' . $chat->getTitle() . ' [' . $whois . ']' . PHP_EOL; } - ++$super_group_chats; + ++$supergroup_chats; } elseif ($chat->isGroupChat()) { if ($text !== '') { $text_back .= '- G ' . $chat->getTitle() . ' [' . $whois . ']' . PHP_EOL; } ++$group_chats; + } elseif ($chat->isChannel()) { + if ($text !== '') { + $text_back .= '- C ' . $chat->getTitle() . ' [' . $whois . ']' . PHP_EOL; + } + + ++$channel_chats; } } } - if (($user_chats + $group_chats + $super_group_chats) === 0) { + if (($user_chats + $group_chats + $supergroup_chats) === 0) { $text_back = 'No chats found..'; } else { $text_back .= PHP_EOL . 'Private Chats: ' . $user_chats; $text_back .= PHP_EOL . 'Groups: ' . $group_chats; - $text_back .= PHP_EOL . 'Super Groups: ' . $super_group_chats; - $text_back .= PHP_EOL . 'Total: ' . ($user_chats + $group_chats + $super_group_chats); + $text_back .= PHP_EOL . 'Super Groups: ' . $supergroup_chats; + $text_back .= PHP_EOL . 'Channels: ' . $channel_chats; + $text_back .= PHP_EOL . 'Total: ' . ($user_chats + $group_chats + $supergroup_chats); if ($text === '') { $text_back .= PHP_EOL . PHP_EOL . 'List all chats: /' . $this->name . ' *' . PHP_EOL . 'Search for chats: /' . $this->name . ' '; diff --git a/src/Commands/AdminCommands/SendtoallCommand.php b/src/Commands/AdminCommands/SendtoallCommand.php index f0b1e0e39..7ea9bfcda 100644 --- a/src/Commands/AdminCommands/SendtoallCommand.php +++ b/src/Commands/AdminCommands/SendtoallCommand.php @@ -38,7 +38,7 @@ class SendtoallCommand extends AdminCommand /** * @var string */ - protected $version = '1.3.0'; + protected $version = '1.4.0'; /** * @var bool @@ -48,7 +48,7 @@ class SendtoallCommand extends AdminCommand /** * Execute command * - * @return boolean + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public function execute() @@ -64,11 +64,12 @@ public function execute() $results = Request::sendToActiveChats( 'sendMessage', //callback function to execute (see Request.php methods) ['text' => $text], //Param to evaluate the request - true, //Send to groups (group chat) - true, //Send to super groups chats (super group chat) - 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 + [ + 'groups' => true, + 'supergroups' => true, + 'channels' => false, + 'users' => true, + ] ); $total = 0; diff --git a/src/Commands/AdminCommands/WhoisCommand.php b/src/Commands/AdminCommands/WhoisCommand.php index 83b4ebcea..ad5e26155 100644 --- a/src/Commands/AdminCommands/WhoisCommand.php +++ b/src/Commands/AdminCommands/WhoisCommand.php @@ -42,7 +42,7 @@ class WhoisCommand extends AdminCommand /** * @var string */ - protected $version = '1.2.0'; + protected $version = '1.3.0'; /** * @var bool @@ -52,7 +52,7 @@ class WhoisCommand extends AdminCommand /** * Command execute method * - * @return mixed + * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public function execute() @@ -89,28 +89,25 @@ public function execute() $result = null; if (is_numeric($text)) { - $results = DB::selectChats( - true, //Select groups (group chat) - true, //Select supergroups (super group chat) - true, //Select users (single chat) - null, //'yyyy-mm-dd hh:mm:ss' date range from - null, //'yyyy-mm-dd hh:mm:ss' date range to - $user_id //Specific chat_id to select - ); + $results = DB::selectChats([ + 'groups' => true, + 'supergroups' => true, + 'channels' => true, + 'users' => true, + 'chat_id' => $user_id, //Specific chat_id to select + ]); if (!empty($results)) { $result = reset($results); } } else { - $results = DB::selectChats( - true, //Select groups (group chat) - true, //Select supergroups (super group chat) - true, //Select users (single chat) - null, //'yyyy-mm-dd hh:mm:ss' date range from - null, //'yyyy-mm-dd hh:mm:ss' date range to - null, //Specific chat_id to select - $text //Text to search in user/group name - ); + $results = DB::selectChats([ + 'groups' => true, + 'supergroups' => true, + 'channels' => true, + 'users' => true, + 'text' => $text //Text to search in user/group name + ]); if (is_array($results) && count($results) === 1) { $result = reset($results); @@ -118,8 +115,9 @@ public function execute() } if (is_array($result)) { - $result['id'] = $result['chat_id']; - $chat = new Chat($result); + $result['id'] = $result['chat_id']; + $result['username'] = $result['chat_username']; + $chat = new Chat($result); $user_id = $result['id']; $created_at = $result['chat_created_at']; diff --git a/src/DB.php b/src/DB.php index 8b3b248eb..e36154bf8 100644 --- a/src/DB.php +++ b/src/DB.php @@ -979,33 +979,32 @@ public static function insertEditedMessageRequest(Message $edited_message) } /** - * Select Group and/or single Chats + * Select Groups, Supergroups, Channels and/or single user Chats (also by ID or text) * - * @param bool $select_groups - * @param bool $select_super_groups - * @param bool $select_users - * @param string $date_from - * @param string $date_to - * @param int $chat_id - * @param string $text + * @param $select_chats_params * - * @return array|bool (Selected chats or false if invalid arguments) - * @throws \Longman\TelegramBot\Exception\TelegramException + * @return array|bool + * @throws TelegramException */ - public static function selectChats( - $select_groups = true, - $select_super_groups = true, - $select_users = true, - $date_from = null, - $date_to = null, - $chat_id = null, - $text = null - ) { + public static function selectChats($select_chats_params) + { if (!self::isDbConnected()) { return false; } - if (!$select_groups && !$select_users && !$select_super_groups) { + // Set defaults for omitted values. + $select = array_merge([ + 'groups' => true, + 'supergroups' => true, + 'channels' => true, + 'users' => true, + 'date_from' => null, + 'date_to' => null, + 'chat_id' => null, + 'text' => null, + ], $select_chats_params); + + if (!$select['groups'] && !$select['users'] && !$select['supergroups']) { return false; } @@ -1013,10 +1012,11 @@ public static function selectChats( $query = ' SELECT * , ' . TB_CHAT . '.`id` AS `chat_id`, + ' . TB_CHAT . '.`username` AS `chat_username`, ' . TB_CHAT . '.`created_at` AS `chat_created_at`, ' . TB_CHAT . '.`updated_at` AS `chat_updated_at` '; - if ($select_users) { + if ($select['users']) { $query .= ' , ' . TB_USER . '.`id` AS `user_id` FROM `' . TB_CHAT . '` @@ -1031,33 +1031,34 @@ public static function selectChats( $where = []; $tokens = []; - if (!$select_groups || !$select_users || !$select_super_groups) { + if (!$select['groups'] || !$select['users'] || !$select['supergroups']) { $chat_or_user = []; - $select_groups && $chat_or_user[] = TB_CHAT . '.`type` = "group"'; - $select_super_groups && $chat_or_user[] = TB_CHAT . '.`type` = "supergroup"'; - $select_users && $chat_or_user[] = TB_CHAT . '.`type` = "private"'; + $select['groups'] && $chat_or_user[] = TB_CHAT . '.`type` = "group"'; + $select['supergroups'] && $chat_or_user[] = TB_CHAT . '.`type` = "supergroup"'; + $select['channels'] && $chat_or_user[] = TB_CHAT . '.`type` = "channel"'; + $select['users'] && $chat_or_user[] = TB_CHAT . '.`type` = "private"'; $where[] = '(' . implode(' OR ', $chat_or_user) . ')'; } - if (null !== $date_from) { + if (null !== $select['date_from']) { $where[] = TB_CHAT . '.`updated_at` >= :date_from'; - $tokens[':date_from'] = $date_from; + $tokens[':date_from'] = $select['date_from']; } - if (null !== $date_to) { + if (null !== $select['date_to']) { $where[] = TB_CHAT . '.`updated_at` <= :date_to'; - $tokens[':date_to'] = $date_to; + $tokens[':date_to'] = $select['date_to']; } - if (null !== $chat_id) { + if (null !== $select['chat_id']) { $where[] = TB_CHAT . '.`id` = :chat_id'; - $tokens[':chat_id'] = $chat_id; + $tokens[':chat_id'] = $select['chat_id']; } - if (null !== $text) { - if ($select_users) { + if (null !== $select['text']) { + if ($select['users']) { $where[] = '( LOWER(' . TB_CHAT . '.`title`) LIKE :text OR LOWER(' . TB_USER . '.`first_name`) LIKE :text @@ -1067,7 +1068,7 @@ public static function selectChats( } else { $where[] = 'LOWER(' . TB_CHAT . '.`title`) LIKE :text'; } - $tokens[':text'] = '%' . strtolower($text) . '%'; + $tokens[':text'] = '%' . strtolower($select['text']) . '%'; } if (!empty($where)) { diff --git a/src/Request.php b/src/Request.php index 5eeb29af3..176488b7e 100644 --- a/src/Request.php +++ b/src/Request.php @@ -961,38 +961,30 @@ public static function emptyResponse() /** * Send message to all active chats * - * @param string $callback_function - * @param array $data - * @param boolean $send_groups - * @param boolean $send_super_groups - * @param boolean $send_users - * @param string $date_from - * @param string $date_to + * @param string $callback_function + * @param array $data + * @param array $select_chats_params * * @return array - * @throws \Longman\TelegramBot\Exception\TelegramException + * @throws TelegramException */ public static function sendToActiveChats( $callback_function, array $data, - $send_groups = true, - $send_super_groups = true, - $send_users = true, - $date_from = null, - $date_to = null + array $select_chats_params ) { $callback_path = __NAMESPACE__ . '\Request'; if (!method_exists($callback_path, $callback_function)) { throw new TelegramException('Method "' . $callback_function . '" not found in class Request.'); } - $chats = DB::selectChats($send_groups, $send_super_groups, $send_users, $date_from, $date_to); + $chats = DB::selectChats($select_chats_params); $results = []; if (is_array($chats)) { foreach ($chats as $row) { $data['chat_id'] = $row['chat_id']; - $results[] = call_user_func_array($callback_path . '::' . $callback_function, [$data]); + $results[] = call_user_func($callback_path . '::' . $callback_function, $data); } }