Commit 195910c5 authored by Armando Lüscher's avatar Armando Lüscher Committed by GitHub

Merge pull request #524 from jacklul/selectchats_channels

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