Fix and improve Admin commands.

parent 68874bb7
...@@ -35,7 +35,7 @@ class ChatsCommand extends AdminCommand ...@@ -35,7 +35,7 @@ class ChatsCommand extends AdminCommand
/** /**
* @var string * @var string
*/ */
protected $version = '1.0.2'; protected $version = '1.1.0';
/** /**
* @var bool * @var bool
...@@ -46,6 +46,7 @@ class ChatsCommand extends AdminCommand ...@@ -46,6 +46,7 @@ class ChatsCommand extends AdminCommand
* Command execute method * Command execute method
* *
* @return mixed * @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function execute() public function execute()
{ {
...@@ -61,7 +62,7 @@ class ChatsCommand extends AdminCommand ...@@ -61,7 +62,7 @@ class ChatsCommand extends AdminCommand
null, //'yyyy-mm-dd hh:mm:ss' date range from null, //'yyyy-mm-dd hh:mm:ss' date range from
null, //'yyyy-mm-dd hh:mm:ss' date range to null, //'yyyy-mm-dd hh:mm:ss' date range to
null, //Specific chat_id to select null, //Specific chat_id to select
($text === '' || $text == '*') ? null : $text //Text to search in user/group name ($text === '' || $text === '*') ? null : $text //Text to search in user/group name
); );
$user_chats = 0; $user_chats = 0;
...@@ -70,12 +71,13 @@ class ChatsCommand extends AdminCommand ...@@ -70,12 +71,13 @@ class ChatsCommand extends AdminCommand
if ($text === '') { if ($text === '') {
$text_back = ''; $text_back = '';
} elseif ($text == '*') { } elseif ($text === '*') {
$text_back = 'List of all bot chats:' . "\n"; $text_back = 'List of all bot chats:' . PHP_EOL;
} else { } else {
$text_back = 'Chat search results:' . "\n"; $text_back = 'Chat search results:' . PHP_EOL;
} }
if (is_array($results)) {
foreach ($results as $result) { foreach ($results as $result) {
//Initialize a chat object //Initialize a chat object
$result['id'] = $result['chat_id']; $result['id'] = $result['chat_id'];
...@@ -88,36 +90,37 @@ class ChatsCommand extends AdminCommand ...@@ -88,36 +90,37 @@ class ChatsCommand extends AdminCommand
} }
if ($chat->isPrivateChat()) { if ($chat->isPrivateChat()) {
if ($text != '') { if ($text !== '') {
$text_back .= '- P ' . $chat->tryMention() . ' [' . $whois . ']' . "\n"; $text_back .= '- P ' . $chat->tryMention() . ' [' . $whois . ']' . PHP_EOL;
} }
++$user_chats; ++$user_chats;
} elseif ($chat->isSuperGroup()) { } elseif ($chat->isSuperGroup()) {
if ($text != '') { if ($text !== '') {
$text_back .= '- S ' . $chat->getTitle() . ' [' . $whois . ']' . "\n"; $text_back .= '- S ' . $chat->getTitle() . ' [' . $whois . ']' . PHP_EOL;
} }
++$super_group_chats; ++$super_group_chats;
} elseif ($chat->isGroupChat()) { } elseif ($chat->isGroupChat()) {
if ($text != '') { if ($text !== '') {
$text_back .= '- G ' . $chat->getTitle() . ' [' . $whois . ']' . "\n"; $text_back .= '- G ' . $chat->getTitle() . ' [' . $whois . ']' . PHP_EOL;
} }
++$group_chats; ++$group_chats;
} }
} }
}
if (($user_chats + $group_chats + $super_group_chats) === 0) { if (($user_chats + $group_chats + $super_group_chats) === 0) {
$text_back = 'No chats found..'; $text_back = 'No chats found..';
} else { } else {
$text_back .= "\n" . 'Private Chats: ' . $user_chats; $text_back .= PHP_EOL . 'Private Chats: ' . $user_chats;
$text_back .= "\n" . 'Group: ' . $group_chats; $text_back .= PHP_EOL . 'Groups: ' . $group_chats;
$text_back .= "\n" . 'Super Group: ' . $super_group_chats; $text_back .= PHP_EOL . 'Super Groups: ' . $super_group_chats;
$text_back .= "\n" . 'Total: ' . ($user_chats + $group_chats + $super_group_chats); $text_back .= PHP_EOL . 'Total: ' . ($user_chats + $group_chats + $super_group_chats);
if ($text === '') { if ($text === '') {
$text_back .= "\n\n" . 'List all chats: /' . $this->name . ' *' . "\n" . 'Search for chats: /' . $this->name . ' <search string>'; $text_back .= PHP_EOL . PHP_EOL . 'List all chats: /' . $this->name . ' *' . PHP_EOL . 'Search for chats: /' . $this->name . ' <search string>';
} }
} }
......
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
namespace Longman\TelegramBot\Commands\AdminCommands; namespace Longman\TelegramBot\Commands\AdminCommands;
use Longman\TelegramBot\Commands\AdminCommand; use Longman\TelegramBot\Commands\AdminCommand;
use Longman\TelegramBot\Entities\Message;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Request; use Longman\TelegramBot\Request;
/** /**
...@@ -36,7 +38,7 @@ class SendtoallCommand extends AdminCommand ...@@ -36,7 +38,7 @@ class SendtoallCommand extends AdminCommand
/** /**
* @var string * @var string
*/ */
protected $version = '1.2.1'; protected $version = '1.3.0';
/** /**
* @var bool * @var bool
...@@ -47,6 +49,7 @@ class SendtoallCommand extends AdminCommand ...@@ -47,6 +49,7 @@ class SendtoallCommand extends AdminCommand
* Execute command * Execute command
* *
* @return boolean * @return boolean
* @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function execute() public function execute()
{ {
...@@ -68,18 +71,21 @@ class SendtoallCommand extends AdminCommand ...@@ -68,18 +71,21 @@ class SendtoallCommand extends AdminCommand
null //'yyyy-mm-dd hh:mm:ss' date range to null //'yyyy-mm-dd hh:mm:ss' date range to
); );
$tot = 0; $total = 0;
$fail = 0; $failed = 0;
$text = 'Message sent to:' . "\n"; $text = 'Message sent to:' . "\n";
/** @var ServerResponse $result */
foreach ($results as $result) { foreach ($results as $result) {
$status = ''; $name = '';
$type = ''; $type = '';
if ($result->isOk()) { if ($result->isOk()) {
$status = '✔️'; $status = '✔️';
$ServerResponse = $result->getResult(); /** @var Message $message */
$chat = $ServerResponse->getChat(); $message = $result->getResult();
$chat = $message->getChat();
if ($chat->isPrivateChat()) { if ($chat->isPrivateChat()) {
$name = $chat->getFirstName(); $name = $chat->getFirstName();
$type = 'user'; $type = 'user';
...@@ -89,15 +95,15 @@ class SendtoallCommand extends AdminCommand ...@@ -89,15 +95,15 @@ class SendtoallCommand extends AdminCommand
} }
} else { } else {
$status = '✖️'; $status = '✖️';
++$fail; ++$failed;
} }
++$tot; ++$total;
$text .= $tot . ') ' . $status . ' ' . $type . ' ' . $name . "\n"; $text .= $total . ') ' . $status . ' ' . $type . ' ' . $name . "\n";
} }
$text .= 'Delivered: ' . ($tot - $fail) . '/' . $tot . "\n"; $text .= 'Delivered: ' . ($total - $failed) . '/' . $total . "\n";
if ($tot === 0) { if ($total === 0) {
$text = 'No users or chats found..'; $text = 'No users or chats found..';
} }
} }
......
...@@ -38,7 +38,7 @@ class SendtochannelCommand extends AdminCommand ...@@ -38,7 +38,7 @@ class SendtochannelCommand extends AdminCommand
/** /**
* @var string * @var string
*/ */
protected $version = '0.1.4'; protected $version = '0.2.0';
/** /**
* @var bool * @var bool
...@@ -56,36 +56,43 @@ class SendtochannelCommand extends AdminCommand ...@@ -56,36 +56,43 @@ class SendtochannelCommand extends AdminCommand
* Command execute method * Command execute method
* *
* @return \Longman\TelegramBot\Entities\ServerResponse|mixed * @return \Longman\TelegramBot\Entities\ServerResponse|mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function execute() public function execute()
{ {
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$user_id = $message->getFrom()->getId(); $user_id = $message->getFrom()->getId();
$type = $message->getType(); $type = $message->getType();
// 'Cast' the command type into message this protect the machine state // 'Cast' the command type into message to protect the machine state
// if the commmad is recolled when the conversation is already started // if the commmad is recalled when the conversation is already started
$type = ($type == 'command') ? 'Message' : $type; $type = ($type === 'command') ? 'Message' : $type;
$text = trim($message->getText(true)); $text = trim($message->getText(true));
$text_yes_or_no = ($text === 'Yes' || $text === 'No');
$data = []; $data = [
$data['chat_id'] = $chat_id; 'chat_id' => $chat_id,
];
// Conversation // Conversation
$this->conversation = new Conversation($user_id, $chat_id, $this->getName()); $this->conversation = new Conversation($user_id, $chat_id, $this->getName());
$notes = &$this->conversation->notes;
$channels = (array)$this->getConfig('your_channel'); $channels = (array)$this->getConfig('your_channel');
if (!isset($this->conversation->notes['state'])) { if (isset($notes['state'])) {
$state = (count($channels) == 0) ? -1 : 0; $state = $notes['state'];
$this->conversation->notes['last_message_id'] = $message->getMessageId();
} else { } else {
$state = $this->conversation->notes['state']; $state = (count($channels) === 0) ? -1 : 0;
$notes['last_message_id'] = $message->getMessageId();
} }
switch ($state) { switch ($state) {
case -1: case -1:
// getConfig has not been configured asking for channel to administer // getConfig has not been configured asking for channel to administer
if ($type != 'Message' || $text === '') { if ($type !== 'Message' || $text === '') {
$this->conversation->notes['state'] = -1; $notes['state'] = -1;
$this->conversation->update(); $this->conversation->update();
$data['text'] = 'Insert the channel name: (@yourchannel)'; $data['text'] = 'Insert the channel name: (@yourchannel)';
...@@ -94,8 +101,8 @@ class SendtochannelCommand extends AdminCommand ...@@ -94,8 +101,8 @@ class SendtochannelCommand extends AdminCommand
break; break;
} }
$this->conversation->notes['channel'] = $text; $notes['channel'] = $text;
$this->conversation->notes['last_message_id'] = $message->getMessageId(); $notes['last_message_id'] = $message->getMessageId();
// Jump to state 1 // Jump to state 1
goto insert; goto insert;
...@@ -103,38 +110,35 @@ class SendtochannelCommand extends AdminCommand ...@@ -103,38 +110,35 @@ class SendtochannelCommand extends AdminCommand
default: default:
case 0: case 0:
// getConfig has been configured choose channel // getConfig has been configured choose channel
if ($type != 'Message' || !in_array($text, $channels)) { if ($type !== 'Message' || !in_array($text, $channels, true)) {
$this->conversation->notes['state'] = 0; $notes['state'] = 0;
$this->conversation->update(); $this->conversation->update();
$keyboard = []; $keyboard = [];
foreach ($channels as $channel) { foreach ($channels as $channel) {
$keyboard[] = [$channel]; $keyboard[] = [$channel];
} }
$reply_keyboard_markup = new ReplyKeyboardMarkup( $data['reply_markup'] = new ReplyKeyboardMarkup(
[ [
'keyboard' => $keyboard, 'keyboard' => $keyboard,
'resize_keyboard' => true, 'resize_keyboard' => true,
'one_time_keyboard' => true, 'one_time_keyboard' => true,
'selective' => true 'selective' => true,
] ]
); );
$data['reply_markup'] = $reply_keyboard_markup;
$data['text'] = 'Select a channel';
if ($type != 'Message' || !in_array($text, $channels)) {
$data['text'] = 'Select a channel from the keyboard:'; $data['text'] = 'Select a channel from the keyboard:';
}
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
break; break;
} }
$this->conversation->notes['channel'] = $text; $notes['channel'] = $text;
$this->conversation->notes['last_message_id'] = $message->getMessageId(); $notes['last_message_id'] = $message->getMessageId();
// no break // no break
case 1: case 1:
insert: insert:
if ($this->conversation->notes['last_message_id'] == $message->getMessageId() || ($type == 'Message' && $text === '')) { if (($type === 'Message' && $text === '') || $notes['last_message_id'] === $message->getMessageId()) {
$this->conversation->notes['state'] = 1; $notes['state'] = 1;
$this->conversation->update(); $this->conversation->update();
$data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); $data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]);
...@@ -142,45 +146,40 @@ class SendtochannelCommand extends AdminCommand ...@@ -142,45 +146,40 @@ class SendtochannelCommand extends AdminCommand
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
break; break;
} }
$this->conversation->notes['last_message_id'] = $message->getMessageId(); $notes['last_message_id'] = $message->getMessageId();
$this->conversation->notes['message'] = $message->reflect(); $notes['message'] = $message->reflect();
$this->conversation->notes['message_type'] = $type; $notes['message_type'] = $type;
// no break // no break
case 2: case 2:
if ($this->conversation->notes['last_message_id'] == $message->getMessageId() || !($text == 'Yes' || $text == 'No')) { if (!$text_yes_or_no || $notes['last_message_id'] === $message->getMessageId()) {
$this->conversation->notes['state'] = 2; $notes['state'] = 2;
$this->conversation->update(); $this->conversation->update();
// Execute this just with object that allow caption // Execute this just with object that allow caption
if ($this->conversation->notes['message_type'] == 'Video' || $this->conversation->notes['message_type'] == 'Photo') { if ($notes['message_type'] === 'Video' || $notes['message_type'] === 'Photo') {
$keyboard = [['Yes', 'No']]; $data['reply_markup'] = new ReplyKeyboardMarkup(
$reply_keyboard_markup = new ReplyKeyboardMarkup(
[ [
'keyboard' => $keyboard, 'keyboard' => [['Yes', 'No']],
'resize_keyboard' => true, 'resize_keyboard' => true,
'one_time_keyboard' => true, 'one_time_keyboard' => true,
'selective' => true 'selective' => true,
] ]
); );
$data['reply_markup'] = $reply_keyboard_markup;
$data['text'] = 'Would you insert caption?'; $data['text'] = 'Would you like to insert a caption?';
if ($this->conversation->notes['last_message_id'] != $message->getMessageId() && !($text == 'Yes' || $text == 'No')) { if (!$text_yes_or_no && $notes['last_message_id'] !== $message->getMessageId()) {
$data['text'] = 'Would you insert a caption?' . "\n" . 'Type Yes or No'; $data['text'] .= PHP_EOL . 'Type Yes or No';
} }
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
break; break;
} }
} }
$this->conversation->notes['set_caption'] = false; $notes['set_caption'] = ($text === 'Yes');
if ($text == 'Yes') { $notes['last_message_id'] = $message->getMessageId();
$this->conversation->notes['set_caption'] = true;
}
$this->conversation->notes['last_message_id'] = $message->getMessageId();
// no break // no break
case 3: case 3:
if (($this->conversation->notes['last_message_id'] == $message->getMessageId() || $type != 'Message') && $this->conversation->notes['set_caption']) { if ($notes['set_caption'] && ($notes['last_message_id'] === $message->getMessageId() || $type !== 'Message')) {
$this->conversation->notes['state'] = 3; $notes['state'] = 3;
$this->conversation->update(); $this->conversation->update();
$data['text'] = 'Insert caption:'; $data['text'] = 'Insert caption:';
...@@ -188,56 +187,52 @@ class SendtochannelCommand extends AdminCommand ...@@ -188,56 +187,52 @@ class SendtochannelCommand extends AdminCommand
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
break; break;
} }
$this->conversation->notes['last_message_id'] = $message->getMessageId(); $notes['last_message_id'] = $message->getMessageId();
$this->conversation->notes['caption'] = $text; $notes['caption'] = $text;
// no break // no break
case 4: case 4:
if ($this->conversation->notes['last_message_id'] == $message->getMessageId() || !($text == 'Yes' || $text == 'No')) { if (!$text_yes_or_no || $notes['last_message_id'] === $message->getMessageId()) {
$this->conversation->notes['state'] = 4; $notes['state'] = 4;
$this->conversation->update(); $this->conversation->update();
$data['text'] = 'Message will look like this:'; $data['text'] = 'Message will look like this:';
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
if ($this->conversation->notes['message_type'] != 'command') { if ($notes['message_type'] !== 'command') {
if ($this->conversation->notes['set_caption']) { if ($notes['set_caption']) {
$data['caption'] = $this->conversation->notes['caption']; $data['caption'] = $notes['caption'];
} }
$result = $this->sendBack(new Message($this->conversation->notes['message'], 'thisbot'), $data); $this->sendBack(new Message($notes['message'], $this->telegram->getBotName()), $data);
$data['text'] = 'Would you post it?'; $data['reply_markup'] = new ReplyKeyboardMarkup(
if ($this->conversation->notes['last_message_id'] != $message->getMessageId() && !($text == 'Yes' || $text == 'No')) {
$data['text'] = 'Would you post it?' . "\n" . 'Press Yes or No';
}
$keyboard = [['Yes', 'No']];
$reply_keyboard_markup = new ReplyKeyboardMarkup(
[ [
'keyboard' => $keyboard, 'keyboard' => [['Yes', 'No']],
'resize_keyboard' => true, 'resize_keyboard' => true,
'one_time_keyboard' => true, 'one_time_keyboard' => true,
'selective' => true 'selective' => true,
] ]
); );
$data['reply_markup'] = $reply_keyboard_markup;
$data['text'] = 'Would you like to post it?';
if (!$text_yes_or_no && $notes['last_message_id'] !== $message->getMessageId()) {
$data['text'] .= PHP_EOL . 'Type Yes or No';
}
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
} }
break; break;
} }
$this->conversation->notes['post_message'] = false; $notes['post_message'] = ($text === 'Yes');
if ($text == 'Yes') { $notes['last_message_id'] = $message->getMessageId();
$this->conversation->notes['post_message'] = true;
}
$this->conversation->notes['last_message_id'] = $message->getMessageId();
// no break // no break
case 5: case 5:
$data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); $data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]);
if ($this->conversation->notes['post_message']) { if ($notes['post_message']) {
$data['text'] = $this->publish( $data['text'] = $this->publish(
new Message($this->conversation->notes['message'], 'anystring'), new Message($notes['message'], $this->telegram->getBotName()),
$this->conversation->notes['channel'], $notes['channel'],
$this->conversation->notes['caption'] $notes['caption']
); );
} else { } else {
$data['text'] = 'Abort by user, message not sent..'; $data['text'] = 'Abort by user, message not sent..';
...@@ -246,31 +241,59 @@ class SendtochannelCommand extends AdminCommand ...@@ -246,31 +241,59 @@ class SendtochannelCommand extends AdminCommand
$this->conversation->stop(); $this->conversation->stop();
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
} }
return $result; return $result;
} }
/** /**
* Execute without db * SendBack
* *
* @return mixed * Received a message, the bot can send a copy of it to another chat/channel.
* You don't have to care about the type of the message, the function detect it and use the proper
* REQUEST:: function to send it.
* $data include all the var that you need to send the message to the proper chat
*
* @todo This method will be moved to a higher level maybe in AdminCommand or Command
* @todo Looking for a more significant name
*
* @param \Longman\TelegramBot\Entities\Message $message
* @param array $data
*
* @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function executeNoDb() protected function sendBack(Message $message, array $data)
{ {
$message = $this->getMessage(); $type = $message->getType();
$text = trim($message->getText(true)); $type = ($type === 'command') ? 'Message' : $type;
$chat_id = $message->getChat()->getId(); if ($type === 'Message') {
$data['text'] = $message->getText(true);
$data = []; } elseif ($type === 'Audio') {
$data['chat_id'] = $chat_id; $data['audio'] = $message->getAudio()->getFileId();
$data['duration'] = $message->getAudio()->getDuration();
if ($text === '') { $data['performer'] = $message->getAudio()->getPerformer();
$data['text'] = 'Usage: /sendtochannel <text>'; $data['title'] = $message->getAudio()->getTitle();
} else { } elseif ($type === 'Document') {
$channels = (array)$this->getConfig('your_channel'); $data['document'] = $message->getDocument()->getFileId();
$first_channel = $channels[0]; } elseif ($type === 'Photo') {
$data['text'] = $this->publish(new Message($message->reflect(), 'anystring'), $first_channel); $data['photo'] = $message->getPhoto()[0]->getFileId();
} elseif ($type === 'Sticker') {
$data['sticker'] = $message->getSticker()->getFileId();
} elseif ($type === 'Video') {
$data['video'] = $message->getVideo()->getFileId();
} elseif ($type === 'Voice') {
$data['voice'] = $message->getVoice()->getFileId();
} elseif ($type === 'Location') {
$data['latitude'] = $message->getLocation()->getLatitude();
$data['longitude'] = $message->getLocation()->getLongitude();
} }
return Request::sendMessage($data); $callback_path = 'Longman\TelegramBot\Request';
$callback_function = 'send' . $type;
if (!method_exists($callback_path, $callback_function)) {
throw new TelegramException('Methods: ' . $callback_function . ' not found in class Request.');
}
return call_user_func_array($callback_path . '::' . $callback_function, [$data]);
} }
/** /**
...@@ -281,6 +304,7 @@ class SendtochannelCommand extends AdminCommand ...@@ -281,6 +304,7 @@ class SendtochannelCommand extends AdminCommand
* @param string|null $caption * @param string|null $caption
* *
* @return string * @return string
* @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
protected function publish(Message $message, $channel, $caption = null) protected function publish(Message $message, $channel, $caption = null)
{ {
...@@ -292,61 +316,40 @@ class SendtochannelCommand extends AdminCommand ...@@ -292,61 +316,40 @@ class SendtochannelCommand extends AdminCommand
if ($this->sendBack($message, $data)->isOk()) { if ($this->sendBack($message, $data)->isOk()) {
$response = 'Message sent successfully to: ' . $channel; $response = 'Message sent successfully to: ' . $channel;
} else { } else {
$response = 'Message not sent to: ' . $channel . "\n" . $response = 'Message not sent to: ' . $channel . PHP_EOL .
'- Does the channel exist?' . "\n" . '- Does the channel exist?' . PHP_EOL .
'- Is the bot an admin of the channel?'; '- Is the bot an admin of the channel?';
} }
return $response; return $response;
} }
/** /**
* SendBack * Execute without db
*
* Received a message, the bot can send a copy of it to another chat/channel.
* You don't have to care about the type of the message, the function detect it and use the proper
* REQUEST:: function to send it.
* $data include all the var that you need to send the message to the proper chat
* *
* @todo This method will be moved at an higher level maybe in AdminCommand or Command * @todo Why send just to the first found channel?
* @todo Looking for a more significative name
* *
* @param \Longman\TelegramBot\Entities\Message $message * @return mixed
* @param array $data
*
* @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException * @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
protected function sendBack(Message $message, array $data) public function executeNoDb()
{ {
$type = $message->getType(); $message = $this->getMessage();
$type = ($type == 'command') ? 'Message' : $type; $chat_id = $message->getChat()->getId();
if ($type == 'Message') { $text = trim($message->getText(true));
$data['text'] = $message->getText(true);
} elseif ($type == 'Audio') { $data = [
$data['audio'] = $message->getAudio()->getFileId(); 'chat_id' => $chat_id,
$data['duration'] = $message->getAudio()->getDuration(); 'text' => 'Usage: ' . $this->usage,
$data['performer'] = $message->getAudio()->getPerformer(); ];
$data['title'] = $message->getAudio()->getTitle();
} elseif ($type == 'Document') { if ($text !== '') {
$data['document'] = $message->getDocument()->getFileId(); $channels = (array)$this->getConfig('your_channel');
} elseif ($type == 'Photo') { $first_channel = $channels[0];
$data['photo'] = $message->getPhoto()[0]->getFileId(); $data['text'] = $this->publish(new Message($message->reflect(), $this->telegram->getBotName()),
} elseif ($type == 'Sticker') { $first_channel);
$data['sticker'] = $message->getSticker()->getFileId();
} elseif ($type == 'Video') {
$data['video'] = $message->getVideo()->getFileId();
} elseif ($type == 'Voice') {
$data['voice'] = $message->getVoice()->getFileId();
} elseif ($type == 'Location') {
$data['latitude'] = $message->getLocation()->getLatitude();
$data['longitude'] = $message->getLocation()->getLongitude();
}
$callback_path = 'Longman\TelegramBot\Request';
$callback_function = 'send' . $type;
if (!method_exists($callback_path, $callback_function)) {
throw new TelegramException('Methods: ' . $callback_function . ' not found in class Request.');
} }
return call_user_func_array($callback_path . '::' . $callback_function, [$data]); return Request::sendMessage($data);
} }
} }
...@@ -15,6 +15,8 @@ namespace Longman\TelegramBot\Commands\AdminCommands; ...@@ -15,6 +15,8 @@ namespace Longman\TelegramBot\Commands\AdminCommands;
use Longman\TelegramBot\Commands\AdminCommand; use Longman\TelegramBot\Commands\AdminCommand;
use Longman\TelegramBot\DB; use Longman\TelegramBot\DB;
use Longman\TelegramBot\Entities\Chat; use Longman\TelegramBot\Entities\Chat;
use Longman\TelegramBot\Entities\PhotoSize;
use Longman\TelegramBot\Entities\UserProfilePhotos;
use Longman\TelegramBot\Request; use Longman\TelegramBot\Request;
/** /**
...@@ -40,7 +42,7 @@ class WhoisCommand extends AdminCommand ...@@ -40,7 +42,7 @@ class WhoisCommand extends AdminCommand
/** /**
* @var string * @var string
*/ */
protected $version = '1.1.0'; protected $version = '1.2.0';
/** /**
* @var bool * @var bool
...@@ -51,6 +53,7 @@ class WhoisCommand extends AdminCommand ...@@ -51,6 +53,7 @@ class WhoisCommand extends AdminCommand
* Command execute method * Command execute method
* *
* @return mixed * @return mixed
* @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function execute() public function execute()
{ {
...@@ -71,7 +74,7 @@ class WhoisCommand extends AdminCommand ...@@ -71,7 +74,7 @@ class WhoisCommand extends AdminCommand
$text = substr($command, 5); $text = substr($command, 5);
//We need that '-' now, bring it back //We need that '-' now, bring it back
if ((substr($text, 0, 1) == 'g')) { if (strpos($text, 'g') === 0) {
$text = str_replace('g', '-', $text); $text = str_replace('g', '-', $text);
} }
} }
...@@ -80,9 +83,13 @@ class WhoisCommand extends AdminCommand ...@@ -80,9 +83,13 @@ class WhoisCommand extends AdminCommand
$text = 'Provide the id to lookup: /whois <id>'; $text = 'Provide the id to lookup: /whois <id>';
} else { } else {
$user_id = $text; $user_id = $text;
$chat = null;
$created_at = null;
$updated_at = null;
$result = null;
if (is_numeric($text)) { if (is_numeric($text)) {
$result = DB::selectChats( $results = DB::selectChats(
true, //Select groups (group chat) true, //Select groups (group chat)
true, //Select supergroups (super group chat) true, //Select supergroups (super group chat)
true, //Select users (single chat) true, //Select users (single chat)
...@@ -91,7 +98,9 @@ class WhoisCommand extends AdminCommand ...@@ -91,7 +98,9 @@ class WhoisCommand extends AdminCommand
$user_id //Specific chat_id to select $user_id //Specific chat_id to select
); );
$result = $result[0]; if (!empty($results)) {
$result = reset($results);
}
} else { } else {
$results = DB::selectChats( $results = DB::selectChats(
true, //Select groups (group chat) true, //Select groups (group chat)
...@@ -103,8 +112,8 @@ class WhoisCommand extends AdminCommand ...@@ -103,8 +112,8 @@ class WhoisCommand extends AdminCommand
$text //Text to search in user/group name $text //Text to search in user/group name
); );
if (is_array($results) && count($results) == 1) { if (is_array($results) && count($results) === 1) {
$result = $results[0]; $result = reset($results);
} }
} }
...@@ -118,36 +127,38 @@ class WhoisCommand extends AdminCommand ...@@ -118,36 +127,38 @@ class WhoisCommand extends AdminCommand
$old_id = $result['old_id']; $old_id = $result['old_id'];
} }
if ($chat != null) { if ($chat !== null) {
if ($chat->isPrivateChat()) { if ($chat->isPrivateChat()) {
$text = 'User ID: ' . $user_id . "\n"; $text = 'User ID: ' . $user_id . PHP_EOL;
$text .= 'Name: ' . $chat->getFirstName() . ' ' . $chat->getLastName() . "\n"; $text .= 'Name: ' . $chat->getFirstName() . ' ' . $chat->getLastName() . PHP_EOL;
if ($chat->getUsername() != '') { $username = $chat->getUsername();
$text .= 'Username: @' . $chat->getUsername() . "\n"; if ($username !== null && $username !== '') {
$text .= 'Username: @' . $username . PHP_EOL;
} }
$text .= 'First time seen: ' . $created_at . "\n"; $text .= 'First time seen: ' . $created_at . PHP_EOL;
$text .= 'Last activity: ' . $updated_at . "\n"; $text .= 'Last activity: ' . $updated_at . PHP_EOL;
//Code from Whoami command //Code from Whoami command
$limit = 10; $limit = 10;
$offset = null; $offset = null;
$ServerResponse = Request::getUserProfilePhotos([ $ServerResponse = Request::getUserProfilePhotos(
[
'user_id' => $user_id, 'user_id' => $user_id,
'limit' => $limit, 'limit' => $limit,
'offset' => $offset, 'offset' => $offset,
]); ]
);
if ($ServerResponse->isOk()) { if ($ServerResponse->isOk()) {
/** @var UserProfilePhotos $UserProfilePhoto */
$UserProfilePhoto = $ServerResponse->getResult(); $UserProfilePhoto = $ServerResponse->getResult();
$totalcount = $UserProfilePhoto->getTotalCount();
} else {
$totalcount = 0;
}
if ($totalcount > 0) { if ($UserProfilePhoto->getTotalCount() > 0) {
$photos = $UserProfilePhoto->getPhotos(); $photos = $UserProfilePhoto->getPhotos();
/** @var PhotoSize $photo */
$photo = $photos[0][2]; $photo = $photos[0][2];
$file_id = $photo->getFileId(); $file_id = $photo->getFileId();
...@@ -156,12 +167,13 @@ class WhoisCommand extends AdminCommand ...@@ -156,12 +167,13 @@ class WhoisCommand extends AdminCommand
return Request::sendPhoto($data); return Request::sendPhoto($data);
} }
}
} elseif ($chat->isGroupChat()) { } elseif ($chat->isGroupChat()) {
$text = 'Chat ID: ' . $user_id . (!empty($old_id) ? ' (previously: ' . $old_id . ')' : '') . "\n"; $text = 'Chat ID: ' . $user_id . (!empty($old_id) ? ' (previously: ' . $old_id . ')' : '') . PHP_EOL;
$text .= 'Type: ' . ucfirst($chat->getType()) . "\n"; $text .= 'Type: ' . ucfirst($chat->getType()) . PHP_EOL;
$text .= 'Title: ' . $chat->getTitle() . "\n"; $text .= 'Title: ' . $chat->getTitle() . PHP_EOL;
$text .= 'First time added to group: ' . $created_at . "\n"; $text .= 'First time added to group: ' . $created_at . PHP_EOL;
$text .= 'Last activity: ' . $updated_at . "\n"; $text .= 'Last activity: ' . $updated_at . PHP_EOL;
} }
} elseif (is_array($results) && count($results) > 1) { } elseif (is_array($results) && count($results) > 1) {
$text = 'Multiple chats matched!'; $text = 'Multiple chats matched!';
...@@ -171,6 +183,7 @@ class WhoisCommand extends AdminCommand ...@@ -171,6 +183,7 @@ class WhoisCommand extends AdminCommand
} }
$data['text'] = $text; $data['text'] = $text;
return Request::sendMessage($data); return Request::sendMessage($data);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment