/** * Command execute method * * @return mixed * @throws \Longman\TelegramBot\Exception\TelegramException */ public function execute() { $message = $this->getMessage(); $chat_id = $message->getChat()->getId(); $text = trim($message->getText(true)); $results = DB::selectChats(true, true, true, null, null, null, $text === '' || $text === '*' ? null : $text); $user_chats = 0; $group_chats = 0; $super_group_chats = 0; if ($text === '') { $text_back = ''; } elseif ($text === '*') { $text_back = 'List of all bot chats:' . PHP_EOL; } else { $text_back = 'Chat search results:' . PHP_EOL; } if (is_array($results)) { foreach ($results as $result) { //Initialize a chat object $result['id'] = $result['chat_id']; $chat = new Chat($result); $whois = $chat->getId(); if ($this->telegram->getCommandObject('whois')) { // We can't use '-' in command because part of it will become unclickable $whois = '/whois' . str_replace('-', 'g', $chat->getId()); } if ($chat->isPrivateChat()) { if ($text !== '') { $text_back .= '- P ' . $chat->tryMention() . ' [' . $whois . ']' . PHP_EOL; } ++$user_chats; } elseif ($chat->isSuperGroup()) { if ($text !== '') { $text_back .= '- S ' . $chat->getTitle() . ' [' . $whois . ']' . PHP_EOL; } ++$super_group_chats; } elseif ($chat->isGroupChat()) { if ($text !== '') { $text_back .= '- G ' . $chat->getTitle() . ' [' . $whois . ']' . PHP_EOL; } ++$group_chats; } } } if ($user_chats + $group_chats + $super_group_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); if ($text === '') { $text_back .= PHP_EOL . PHP_EOL . 'List all chats: /' . $this->name . ' *' . PHP_EOL . 'Search for chats: /' . $this->name . ' <search string>'; } } $data = ['chat_id' => $chat_id, 'text' => $text_back]; return Request::sendMessage($data); }
/** * Insert chat * * @param Entities\Chat $chat * @param string $date * @param int $migrate_to_chat_id * * @return bool If the insert was successful * @throws TelegramException */ public static function insertChat(Chat $chat, $date, $migrate_to_chat_id = null) { if (!self::isDbConnected()) { return false; } $chat_id = $chat->getId(); $chat_title = $chat->getTitle(); $type = $chat->getType(); try { $sth2 = self::$pdo->prepare('INSERT INTO `' . TB_CHAT . '` ( `id`, `type`, `title`, `created_at` ,`updated_at`, `old_id` ) VALUES ( :id, :type, :title, :date, :date, :oldid ) ON DUPLICATE KEY UPDATE `type`=:type, `title`=:title, `updated_at`=:date '); if ($migrate_to_chat_id) { $type = 'supergroup'; $sth2->bindParam(':id', $migrate_to_chat_id, \PDO::PARAM_INT); $sth2->bindParam(':oldid', $chat_id, \PDO::PARAM_INT); } else { $sth2->bindParam(':id', $chat_id, \PDO::PARAM_INT); $sth2->bindParam(':oldid', $migrate_to_chat_id, \PDO::PARAM_INT); } $sth2->bindParam(':type', $type, \PDO::PARAM_INT); $sth2->bindParam(':title', $chat_title, \PDO::PARAM_STR, 255); $sth2->bindParam(':date', $date, \PDO::PARAM_STR); return $sth2->execute(); } catch (\PDOException $e) { throw new TelegramException($e->getMessage()); } }