|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * This file is part of the TelegramBot package. |
| 4 | + * |
| 5 | + * (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | +*/ |
| 10 | +namespace Longman\TelegramBot\Commands; |
| 11 | + |
| 12 | +use Longman\TelegramBot\Request; |
| 13 | +use Longman\TelegramBot\DB; |
| 14 | +use Longman\TelegramBot\Command; |
| 15 | +use Longman\TelegramBot\Entities\Update; |
| 16 | +use Longman\TelegramBot\Entities\Chat; |
| 17 | +use Longman\TelegramBot\Exception\TelegramException; |
| 18 | + |
| 19 | +class ChatsCommand extends Command |
| 20 | +{ |
| 21 | + protected $name = 'chats'; |
| 22 | + protected $description = 'List all chats stored by the bot'; |
| 23 | + protected $usage = '/chats '; |
| 24 | + protected $version = '1.0.0'; |
| 25 | + protected $enabled = true; |
| 26 | + protected $public = true; |
| 27 | + //need Mysql |
| 28 | + protected $need_mysql = true; |
| 29 | + |
| 30 | + public function executeNoDB() |
| 31 | + { |
| 32 | + //Database not setted or without connection |
| 33 | + //Preparing message |
| 34 | + $message = $this->getMessage(); |
| 35 | + $chat_id = $message->getChat()->getId(); |
| 36 | + $data = array(); |
| 37 | + $data['chat_id'] = $chat_id; |
| 38 | + $data['text'] = 'Sorry no database connection, unable to execute '.$this->name.' command.'; |
| 39 | + return Request::sendMessage($data); |
| 40 | + } |
| 41 | + |
| 42 | + public function execute() |
| 43 | + { |
| 44 | + $update = $this->getUpdate(); |
| 45 | + $message = $this->getMessage(); |
| 46 | + |
| 47 | + $chat_id = $message->getChat()->getId(); |
| 48 | + $message_id = $message->getMessageId(); |
| 49 | + $text = $message->getText(true); |
| 50 | + |
| 51 | + $results = DB::selectChats( |
| 52 | + true, //Send to chats (group chat) |
| 53 | + true, //Send to users (single chat) |
| 54 | + null, //'yyyy-mm-dd hh:mm:ss' date range from |
| 55 | + null //'yyyy-mm-dd hh:mm:ss' date range to |
| 56 | + ); |
| 57 | + |
| 58 | + $user_chats = 0; |
| 59 | + $group_chats = 0; |
| 60 | + $text = "List of bot chats:\n"; |
| 61 | + |
| 62 | + foreach ($results as $result) { |
| 63 | + //I want initialize a chat object |
| 64 | + //id, title, first_name, last_name |
| 65 | + $result['id'] = $result['chat_id']; |
| 66 | + |
| 67 | + $chat = new Chat($result); |
| 68 | + |
| 69 | + if ($chat->isSingleChat()) { |
| 70 | + //$text .= '- U '.$chat->getFirstName()."\n"; |
| 71 | + |
| 72 | + $text .= '- U '.$this->tryMentionChat($chat)."\n"; |
| 73 | + ++$user_chats; |
| 74 | + } else { |
| 75 | + $text .= '- G '.$chat->getTitle()."\n"; |
| 76 | + ++$group_chats; |
| 77 | + } |
| 78 | + |
| 79 | + } |
| 80 | + if (($group_chats + $user_chats) == 0) { |
| 81 | + $text = "No chats found.."; |
| 82 | + } else { |
| 83 | + $text .= "\nUser Chats: ".$user_chats; |
| 84 | + $text .= "\nGroup Chats: ".$group_chats; |
| 85 | + $text .= "\nTot: ".($group_chats + $user_chats); |
| 86 | + } |
| 87 | + |
| 88 | + $data = []; |
| 89 | + $data['chat_id'] = $chat_id; |
| 90 | + //$data['reply_to_message_id'] = $message_id; |
| 91 | + $data['text'] = $text; |
| 92 | + //$data['parse_mode'] = 'Markdown'; |
| 93 | + $result = Request::sendMessage($data); |
| 94 | + return $result; |
| 95 | + } |
| 96 | +} |
0 commit comments