Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
42 changes: 24 additions & 18 deletions src/Commands/AdminCommands/ChatsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ChatsCommand extends AdminCommand
/**
* @var string
*/
protected $version = '1.1.0';
protected $version = '1.2.0';

/**
* @var bool
Expand All @@ -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()
Expand All @@ -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 = '';
Expand Down Expand Up @@ -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 . ' <search string>';
Expand Down
15 changes: 8 additions & 7 deletions src/Commands/AdminCommands/SendtoallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class SendtoallCommand extends AdminCommand
/**
* @var string
*/
protected $version = '1.3.0';
protected $version = '1.4.0';

/**
* @var bool
Expand All @@ -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()
Expand All @@ -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;
Expand Down
40 changes: 19 additions & 21 deletions src/Commands/AdminCommands/WhoisCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class WhoisCommand extends AdminCommand
/**
* @var string
*/
protected $version = '1.2.0';
protected $version = '1.3.0';

/**
* @var bool
Expand All @@ -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()
Expand Down Expand Up @@ -89,37 +89,35 @@ 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);
}
}

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'];
Expand Down
69 changes: 35 additions & 34 deletions src/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -979,44 +979,44 @@ 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;
}

try {
$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 . '`
Expand All @@ -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
Expand All @@ -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)) {
Expand Down
22 changes: 7 additions & 15 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down