/** * 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); }
public function preExecute() { if (!$this->need_mysql | $this->need_mysql & $this->telegram->isDbEnabled() & DB::isDbConnected()) { return $this->execute(); } return $this->executeNoDB(); }
/** * 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); }
public function execute() { $update = $this->getUpdate(); $message = $this->getMessage(); $chat_id = $message->getChat()->getId(); $message_id = $message->getMessageId(); $text = $message->getText(true); if (empty($text)) { $text = 'Write te message to sent: /sendall <message>'; } else { $results = DB::sendToActiveChats('sendMessage', array('text' => $text), true, true, null, null); $tot = 0; $fail = 0; $text = "Message sended to:\n"; foreach ($results as $result) { $status = ''; $type = ''; print_r($result); if ($result->isOk()) { $status = '✔️'; $ServerResponse = $result->getResult(); $chat = $ServerResponse->getChat(); if ($chat->isSingleChat()) { $name = $chat->getFirstName(); $type = 'user'; } else { $name = $chat->getTitle(); $type = 'chat'; } } else { $status = '✖️'; ++$fail; } ++$tot; $text .= $tot . ') ' . $status . ' ' . $type . ' ' . $name . "\n"; } $text .= "Delivered: " . ($tot - $fail) . '/' . $tot . "\n"; } if ($tot == 0) { $text = "No users or chats found.."; } $data = array(); $data['chat_id'] = $chat_id; //$data['reply_to_message_id'] = $message_id; $data['text'] = $text; $result = Request::sendMessage($data); return $result; }
public function execute() { $update = $this->getUpdate(); $message = $this->getMessage(); $chat_id = $message->getChat()->getId(); $message_id = $message->getMessageId(); $text = $message->getText(true); $results = DB::selectChats(true, true, true, null, null); $user_chats = 0; $group_chats = 0; $super_group_chats = 0; $text = "List of bot chats:\n"; foreach ($results as $result) { //initialize a chat object $result['id'] = $result['chat_id']; $chat = new Chat($result); if ($chat->isPrivateChat()) { $text .= '- P ' . $chat->tryMention() . "\n"; ++$user_chats; } elseif ($chat->isGroupChat()) { $text .= '- G ' . $chat->getTitle() . "\n"; ++$group_chats; } elseif ($chat->isSuperGroup()) { $text .= '- S ' . $chat->getTitle() . "\n"; ++$super_group_chats; } } if ($group_chats + $user_chats + $super_group_chats == 0) { $text = "No chats found.."; } else { $text .= "\nPrivate Chats: " . $user_chats; $text .= "\nGroup: " . $group_chats; $text .= "\nSuper Group: " . $super_group_chats; $text .= "\nTot: " . ($group_chats + $user_chats + $super_group_chats); } $data = []; $data['chat_id'] = $chat_id; $data['text'] = $text; $result = Request::sendMessage($data); return $result->isOk(); }
public function execute() { $update = $this->getUpdate(); $message = $this->getMessage(); $chat_id = $message->getChat()->getId(); $message_id = $message->getMessageId(); $text = $message->getText(true); $results = DB::selectChats(true, true, null, null); $user_chats = 0; $group_chats = 0; $text = "List of bot chats:\n"; foreach ($results as $result) { //I want initialize a chat object //id, title, first_name, last_name $result['id'] = $result['chat_id']; $chat = new Chat($result); if ($chat->isPrivateChat()) { //$text .= '- U '.$chat->getFirstName()."\n"; $text .= '- U ' . $this->tryMentionChat($chat) . "\n"; ++$user_chats; } else { $text .= '- G ' . $chat->getTitle() . "\n"; ++$group_chats; } } if ($group_chats + $user_chats == 0) { $text = "No chats found.."; } else { $text .= "\nUser Chats: " . $user_chats; $text .= "\nGroup Chats: " . $group_chats; $text .= "\nTot: " . ($group_chats + $user_chats); } $data = []; $data['chat_id'] = $chat_id; //$data['reply_to_message_id'] = $message_id; $data['text'] = $text; //$data['parse_mode'] = 'Markdown'; $result = Request::sendMessage($data); return $result; }
/** * Send Message in all the active chat * * * @return bool */ public static function sendToActiveChats($callback_function, array $data, $send_chats = true, $send_users = true, $date_from = null, $date_to = null) { $callback_path = __NAMESPACE__ . '\\Request'; if (!method_exists($callback_path, $callback_function)) { throw new TelegramException('Methods: ' . $callback_function . ' not found in class Request.'); } $chats = DB::selectChats($send_chats, $send_users, $date_from, $date_to); $results = []; foreach ($chats as $row) { $data['chat_id'] = $row['chat_id']; $results[] = call_user_func_array($callback_path . '::' . $callback_function, array($data)); } return $results; }
/** * Process bot Update request * * @param Entities\Update $update * * @return Entities\ServerResponse */ public function processUpdate(Update $update) { $this->update = $update; //If all else fails, it's a generic message. $command = 'genericmessage'; $update_type = $this->update->getUpdateType(); if (in_array($update_type, ['inline_query', 'chosen_inline_result', 'callback_query', 'edited_message'])) { $command = $this->getCommandFromType($update_type); } elseif ($update_type === 'message') { $message = $this->update->getMessage(); //Load admin commands if ($this->isAdmin()) { $this->addCommandsPath(BASE_COMMANDS_PATH . '/AdminCommands', false); } $this->addCommandsPath(BASE_COMMANDS_PATH . '/UserCommands', false); $type = $message->getType(); if ($type === 'command') { $command = $message->getCommand(); } elseif (in_array($type, ['channel_chat_created', 'delete_chat_photo', 'group_chat_created', 'left_chat_member', 'migrate_from_chat_id', 'migrate_to_chat_id', 'new_chat_member', 'new_chat_photo', 'new_chat_title', 'supergroup_chat_created'])) { $command = $this->getCommandFromType($type); } } //Make sure we have an up-to-date command list //This is necessary to "require" all the necessary command files! $this->getCommandsList(); DB::insertRequest($this->update); return $this->executeCommand($command); }
/** * Process Handle bot request * * @return \Longman\TelegramBot\Telegram */ public function processUpdate(update $update) { //Load admin Commands if ($this->admin_enabled) { $message = $update->getMessage(); //$from = $message->getFrom(); //$user_id = $from->getId(); //Admin command avaiable only in single chat with the bot $chat = $message->getChat(); $user_id = $chat->getId(); if (in_array($user_id, $this->admins_list)) { $this->addCommandsPath(BASE_PATH . '/Admin'); } } DB::insertRequest($update); // check type $message = $update->getMessage(); $type = $message->getType(); switch ($type) { default: case 'text': return $this->executeCommand('Genericmessage', $update); break; case 'command': // execute command $command = $message->getCommand(); return $this->executeCommand($command, $update); break; case 'new_chat_participant': // trigger new participant return $this->executeCommand('Newchatparticipant', $update); break; case 'left_chat_participant': // trigger left chat participant return $this->executeCommand('Leftchatparticipant', $update); break; case 'new_chat_title': // trigger new_chat_title return $this->executeCommand('Newchattitle', $update); break; case 'delete_chat_photo': // trigger delete_chat_photo return $this->executeCommand('Deletechatphoto', $update); break; case 'group_chat_created': // trigger group_chat_created return $this->executeCommand('Groupchatcreated', $update); break; } }
/** * Process Handle bot request * * @return \Longman\TelegramBot\Telegram */ public function processUpdate(update $update) { $update_type = $update->getUpdateType(); if ($update_type == 'message') { //Load admin Commands if ($this->admin_enabled) { $message = $update->getMessage(); //Admin command avaiable in any chats //$from = $message->getFrom(); //$user_id = $from->getId(); //Admin command avaiable only in single chat with the bot $chat = $message->getChat(); $user_id = $chat->getId(); if (in_array($user_id, $this->admins_list)) { $this->addCommandsPath(BASE_PATH . '/Admin'); } } // check type $message = $update->getMessage(); $type = $message->getType(); switch ($type) { default: case 'Message': case 'Photo': case 'Audio': case 'Document': case 'Sticker': case 'Video': case 'Voice': case 'Location': $command = 'Genericmessage'; break; case 'command': // execute command $command = $message->getCommand(); break; case 'new_chat_participant': // trigger new participant $command = 'Newchatparticipant'; break; case 'left_chat_participant': // trigger left chat participant $command = 'Leftchatparticipant'; break; case 'new_chat_title': // trigger new_chat_title $command = 'Newchattitle'; break; case 'delete_chat_photo': // trigger delete_chat_photo $command = 'Deletechatphoto'; break; case 'group_chat_created': // trigger group_chat_created $command = 'Groupchatcreated'; break; case 'supergroup_chat_created': // trigger super_group_chat_created $command = 'Supergroupchatcreated'; break; case 'channel_chat_created': // trigger channel_chat_created $command = 'Channelchatcreated'; break; } } elseif ($update_type == 'inline_query') { $command = 'Inlinequery'; } elseif ($update_type == 'chosen_inline_result') { $command = 'Choseninlineresult'; } DB::insertRequest($update); $result = $this->executeCommand($command, $update); return $result; }
/** * Start a fake conversation for the passed command and return the randomly generated ids. * * @return array */ public static function startFakeConversation() { if (!DB::isDbConnected()) { return false; } //Just get some random values. $message_id = mt_rand(); $user_id = mt_rand(); $chat_id = mt_rand(); //Make sure we have a valid user and chat available. $message = self::getFakeMessageObject(['message_id' => $message_id], ['id' => $user_id], ['id' => $chat_id]); DB::insertMessageRequest($message); DB::insertUser($message->getFrom(), null, $message->getChat()); return compact('message_id', 'user_id', 'chat_id'); }