Longman\TelegramBot\DB::selectChats PHP Method

selectChats() public static method

Select Group and/or single Chats
public static selectChats ( boolean $select_groups = true, boolean $select_super_groups = true, boolean $select_users = true, string $date_from = null, string $date_to = null, integer $chat_id = null, string $text = null ) : array | boolean
$select_groups boolean
$select_super_groups boolean
$select_users boolean
$date_from string
$date_to string
$chat_id integer
$text string
return array | boolean (Selected chats or false if invalid arguments)
    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)
    {
        if (!self::isDbConnected()) {
            return false;
        }
        if (!$select_groups && !$select_users && !$select_super_groups) {
            return false;
        }
        try {
            $query = '
                SELECT * ,
                ' . TB_CHAT . '.`id` AS `chat_id`,
                ' . TB_CHAT . '.`created_at` AS `chat_created_at`,
                ' . TB_CHAT . '.`updated_at` AS `chat_updated_at`
            ';
            if ($select_users) {
                $query .= '
                    , ' . TB_USER . '.`id` AS `user_id`
                    FROM `' . TB_CHAT . '`
                    LEFT JOIN `' . TB_USER . '`
                    ON ' . TB_CHAT . '.`id`=' . TB_USER . '.`id`
                ';
            } else {
                $query .= 'FROM `' . TB_CHAT . '`';
            }
            //Building parts of query
            $where = [];
            $tokens = [];
            if (!$select_groups || !$select_users || !$select_super_groups) {
                $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"');
                $where[] = '(' . implode(' OR ', $chat_or_user) . ')';
            }
            if (null !== $date_from) {
                $where[] = TB_CHAT . '.`updated_at` >= :date_from';
                $tokens[':date_from'] = $date_from;
            }
            if (null !== $date_to) {
                $where[] = TB_CHAT . '.`updated_at` <= :date_to';
                $tokens[':date_to'] = $date_to;
            }
            if (null !== $chat_id) {
                $where[] = TB_CHAT . '.`id` = :chat_id';
                $tokens[':chat_id'] = $chat_id;
            }
            if (null !== $text) {
                if ($select_users) {
                    $where[] = '(
                        LOWER(' . TB_CHAT . '.`title`) LIKE :text
                        OR LOWER(' . TB_USER . '.`first_name`) LIKE :text
                        OR LOWER(' . TB_USER . '.`last_name`) LIKE :text
                        OR LOWER(' . TB_USER . '.`username`) LIKE :text
                    )';
                } else {
                    $where[] = 'LOWER(' . TB_CHAT . '.`title`) LIKE :text';
                }
                $tokens[':text'] = '%' . strtolower($text) . '%';
            }
            if (!empty($where)) {
                $query .= ' WHERE ' . implode(' AND ', $where);
            }
            $query .= ' ORDER BY ' . TB_CHAT . '.`updated_at` ASC';
            $sth = self::$pdo->prepare($query);
            $sth->execute($tokens);
            return $sth->fetchAll(PDO::FETCH_ASSOC);
        } catch (PDOException $e) {
            throw new TelegramException($e->getMessage());
        }
    }

Usage Example

 /**
  * Command execute method
  *
  * @return mixed
  * @throws \Longman\TelegramBot\Exception\TelegramException
  */
 public function execute()
 {
     $message = $this->getMessage();
     $chat_id = $message->getChat()->getId();
     $command = $message->getCommand();
     $text = trim($message->getText(true));
     $data = ['chat_id' => $chat_id];
     //No point in replying to messages in private chats
     if (!$message->getChat()->isPrivateChat()) {
         $data['reply_to_message_id'] = $message->getMessageId();
     }
     if ($command !== 'whois') {
         $text = substr($command, 5);
         //We need that '-' now, bring it back
         if (strpos($text, 'g') === 0) {
             $text = str_replace('g', '-', $text);
         }
     }
     if ($text === '') {
         $text = 'Provide the id to lookup: /whois <id>';
     } else {
         $user_id = $text;
         $chat = null;
         $created_at = null;
         $updated_at = null;
         $result = null;
         if (is_numeric($text)) {
             $results = DB::selectChats(true, true, true, null, null, $user_id);
             if (!empty($results)) {
                 $result = reset($results);
             }
         } else {
             $results = DB::selectChats(true, true, true, null, null, null, $text);
             if (is_array($results) && count($results) === 1) {
                 $result = reset($results);
             }
         }
         if (is_array($result)) {
             $result['id'] = $result['chat_id'];
             $chat = new Chat($result);
             $user_id = $result['id'];
             $created_at = $result['chat_created_at'];
             $updated_at = $result['chat_updated_at'];
             $old_id = $result['old_id'];
         }
         if ($chat !== null) {
             if ($chat->isPrivateChat()) {
                 $text = 'User ID: ' . $user_id . PHP_EOL;
                 $text .= 'Name: ' . $chat->getFirstName() . ' ' . $chat->getLastName() . PHP_EOL;
                 $username = $chat->getUsername();
                 if ($username !== null && $username !== '') {
                     $text .= 'Username: @' . $username . PHP_EOL;
                 }
                 $text .= 'First time seen: ' . $created_at . PHP_EOL;
                 $text .= 'Last activity: ' . $updated_at . PHP_EOL;
                 //Code from Whoami command
                 $limit = 10;
                 $offset = null;
                 $response = Request::getUserProfilePhotos(['user_id' => $user_id, 'limit' => $limit, 'offset' => $offset]);
                 if ($response->isOk()) {
                     /** @var UserProfilePhotos $user_profile_photos */
                     $user_profile_photos = $response->getResult();
                     if ($user_profile_photos->getTotalCount() > 0) {
                         $photos = $user_profile_photos->getPhotos();
                         /** @var PhotoSize $photo */
                         $photo = $photos[0][2];
                         $file_id = $photo->getFileId();
                         $data['photo'] = $file_id;
                         $data['caption'] = $text;
                         return Request::sendPhoto($data);
                     }
                 }
             } elseif ($chat->isGroupChat()) {
                 $text = 'Chat ID: ' . $user_id . (!empty($old_id) ? ' (previously: ' . $old_id . ')' : '') . PHP_EOL;
                 $text .= 'Type: ' . ucfirst($chat->getType()) . PHP_EOL;
                 $text .= 'Title: ' . $chat->getTitle() . PHP_EOL;
                 $text .= 'First time added to group: ' . $created_at . PHP_EOL;
                 $text .= 'Last activity: ' . $updated_at . PHP_EOL;
             }
         } elseif (is_array($results) && count($results) > 1) {
             $text = 'Multiple chats matched!';
         } else {
             $text = 'Chat not found!';
         }
     }
     $data['text'] = $text;
     return Request::sendMessage($data);
 }
All Usage Examples Of Longman\TelegramBot\DB::selectChats