Fix sendtochannel command to use updated code and use new Keyboard entities.

parent 3120727f
...@@ -10,12 +10,11 @@ ...@@ -10,12 +10,11 @@
namespace Longman\TelegramBot\Commands\AdminCommands; namespace Longman\TelegramBot\Commands\AdminCommands;
use Longman\TelegramBot\Entities\Keyboard;
use Longman\TelegramBot\Request; use Longman\TelegramBot\Request;
use Longman\TelegramBot\Conversation; use Longman\TelegramBot\Conversation;
use Longman\TelegramBot\Commands\AdminCommand; use Longman\TelegramBot\Commands\AdminCommand;
use Longman\TelegramBot\Entities\Message; use Longman\TelegramBot\Entities\Message;
use Longman\TelegramBot\Entities\ReplyKeyboardHide;
use Longman\TelegramBot\Entities\ReplyKeyboardMarkup;
use Longman\TelegramBot\Exception\TelegramException; use Longman\TelegramBot\Exception\TelegramException;
class SendtochannelCommand extends AdminCommand class SendtochannelCommand extends AdminCommand
...@@ -67,7 +66,7 @@ class SendtochannelCommand extends AdminCommand ...@@ -67,7 +66,7 @@ class SendtochannelCommand extends AdminCommand
$type = $message->getType(); $type = $message->getType();
// 'Cast' the command type into message to protect the machine state // 'Cast' the command type into message to protect the machine state
// if the commmad is recalled when the conversation is already started // if the commmad is recalled when the conversation is already started
$type = ($type === 'command') ? 'Message' : $type; in_array($type, ['command', 'text'], true) && $type = 'message';
$text = trim($message->getText(true)); $text = trim($message->getText(true));
$text_yes_or_no = ($text === 'Yes' || $text === 'No'); $text_yes_or_no = ($text === 'Yes' || $text === 'No');
...@@ -78,7 +77,9 @@ class SendtochannelCommand extends AdminCommand ...@@ -78,7 +77,9 @@ class SendtochannelCommand extends AdminCommand
// 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; $notes = &$this->conversation->notes;
!is_array($notes) && $notes = [];
$channels = (array)$this->getConfig('your_channel'); $channels = (array)$this->getConfig('your_channel');
if (isset($notes['state'])) { if (isset($notes['state'])) {
...@@ -91,12 +92,12 @@ class SendtochannelCommand extends AdminCommand ...@@ -91,12 +92,12 @@ class SendtochannelCommand extends AdminCommand
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 === '') {
$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)';
$data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); $data['reply_markup'] = Keyboard::hide(['selective' => true]);
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
break; break;
...@@ -110,7 +111,7 @@ class SendtochannelCommand extends AdminCommand ...@@ -110,7 +111,7 @@ 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, true)) { if ($type !== 'message' || !in_array($text, $channels, true)) {
$notes['state'] = 0; $notes['state'] = 0;
$this->conversation->update(); $this->conversation->update();
...@@ -118,7 +119,7 @@ class SendtochannelCommand extends AdminCommand ...@@ -118,7 +119,7 @@ class SendtochannelCommand extends AdminCommand
foreach ($channels as $channel) { foreach ($channels as $channel) {
$keyboard[] = [$channel]; $keyboard[] = [$channel];
} }
$data['reply_markup'] = new ReplyKeyboardMarkup( $data['reply_markup'] = new Keyboard(
[ [
'keyboard' => $keyboard, 'keyboard' => $keyboard,
'resize_keyboard' => true, 'resize_keyboard' => true,
...@@ -137,11 +138,11 @@ class SendtochannelCommand extends AdminCommand ...@@ -137,11 +138,11 @@ class SendtochannelCommand extends AdminCommand
// no break // no break
case 1: case 1:
insert: insert:
if (($type === 'Message' && $text === '') || $notes['last_message_id'] === $message->getMessageId()) { if (($type === 'message' && $text === '') || $notes['last_message_id'] === $message->getMessageId()) {
$notes['state'] = 1; $notes['state'] = 1;
$this->conversation->update(); $this->conversation->update();
$data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); $data['reply_markup'] = Keyboard::hide(['selective' => true]);
$data['text'] = 'Insert the content you want to share: text, photo, audio...'; $data['text'] = 'Insert the content you want to share: text, photo, audio...';
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
break; break;
...@@ -156,8 +157,8 @@ class SendtochannelCommand extends AdminCommand ...@@ -156,8 +157,8 @@ class SendtochannelCommand extends AdminCommand
$this->conversation->update(); $this->conversation->update();
// Execute this just with object that allow caption // Execute this just with object that allow caption
if ($notes['message_type'] === 'Video' || $notes['message_type'] === 'Photo') { if (in_array($notes['message_type'], ['video', 'photo'], true)) {
$data['reply_markup'] = new ReplyKeyboardMarkup( $data['reply_markup'] = new Keyboard(
[ [
'keyboard' => [['Yes', 'No']], 'keyboard' => [['Yes', 'No']],
'resize_keyboard' => true, 'resize_keyboard' => true,
...@@ -178,12 +179,12 @@ class SendtochannelCommand extends AdminCommand ...@@ -178,12 +179,12 @@ class SendtochannelCommand extends AdminCommand
$notes['last_message_id'] = $message->getMessageId(); $notes['last_message_id'] = $message->getMessageId();
// no break // no break
case 3: case 3:
if ($notes['set_caption'] && ($notes['last_message_id'] === $message->getMessageId() || $type !== 'Message')) { if ($notes['set_caption'] && ($notes['last_message_id'] === $message->getMessageId() || $type !== 'message')) {
$notes['state'] = 3; $notes['state'] = 3;
$this->conversation->update(); $this->conversation->update();
$data['text'] = 'Insert caption:'; $data['text'] = 'Insert caption:';
$data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); $data['reply_markup'] = Keyboard::hide(['selective' => true]);
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
break; break;
} }
...@@ -204,7 +205,7 @@ class SendtochannelCommand extends AdminCommand ...@@ -204,7 +205,7 @@ class SendtochannelCommand extends AdminCommand
} }
$this->sendBack(new Message($notes['message'], $this->telegram->getBotName()), $data); $this->sendBack(new Message($notes['message'], $this->telegram->getBotName()), $data);
$data['reply_markup'] = new ReplyKeyboardMarkup( $data['reply_markup'] = new Keyboard(
[ [
'keyboard' => [['Yes', 'No']], 'keyboard' => [['Yes', 'No']],
'resize_keyboard' => true, 'resize_keyboard' => true,
...@@ -226,7 +227,7 @@ class SendtochannelCommand extends AdminCommand ...@@ -226,7 +227,7 @@ class SendtochannelCommand extends AdminCommand
$notes['last_message_id'] = $message->getMessageId(); $notes['last_message_id'] = $message->getMessageId();
// no break // no break
case 5: case 5:
$data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); $data['reply_markup'] = Keyboard::hide(['selective' => true]);
if ($notes['post_message']) { if ($notes['post_message']) {
$data['text'] = $this->publish( $data['text'] = $this->publish(
...@@ -265,35 +266,37 @@ class SendtochannelCommand extends AdminCommand ...@@ -265,35 +266,37 @@ class SendtochannelCommand extends AdminCommand
protected function sendBack(Message $message, array $data) protected function sendBack(Message $message, array $data)
{ {
$type = $message->getType(); $type = $message->getType();
$type = ($type === 'command') ? 'Message' : $type; in_array($type, ['command', 'text'], true) && $type = 'message';
if ($type === 'Message') {
if ($type === 'message') {
$data['text'] = $message->getText(true); $data['text'] = $message->getText(true);
} elseif ($type === 'Audio') { } elseif ($type === 'audio') {
$data['audio'] = $message->getAudio()->getFileId(); $data['audio'] = $message->getAudio()->getFileId();
$data['duration'] = $message->getAudio()->getDuration(); $data['duration'] = $message->getAudio()->getDuration();
$data['performer'] = $message->getAudio()->getPerformer(); $data['performer'] = $message->getAudio()->getPerformer();
$data['title'] = $message->getAudio()->getTitle(); $data['title'] = $message->getAudio()->getTitle();
} elseif ($type === 'Document') { } elseif ($type === 'document') {
$data['document'] = $message->getDocument()->getFileId(); $data['document'] = $message->getDocument()->getFileId();
} elseif ($type === 'Photo') { } elseif ($type === 'photo') {
$data['photo'] = $message->getPhoto()[0]->getFileId(); $data['photo'] = $message->getPhoto()[0]->getFileId();
} elseif ($type === 'Sticker') { } elseif ($type === 'sticker') {
$data['sticker'] = $message->getSticker()->getFileId(); $data['sticker'] = $message->getSticker()->getFileId();
} elseif ($type === 'Video') { } elseif ($type === 'video') {
$data['video'] = $message->getVideo()->getFileId(); $data['video'] = $message->getVideo()->getFileId();
} elseif ($type === 'Voice') { } elseif ($type === 'voice') {
$data['voice'] = $message->getVoice()->getFileId(); $data['voice'] = $message->getVoice()->getFileId();
} elseif ($type === 'Location') { } elseif ($type === 'location') {
$data['latitude'] = $message->getLocation()->getLatitude(); $data['latitude'] = $message->getLocation()->getLatitude();
$data['longitude'] = $message->getLocation()->getLongitude(); $data['longitude'] = $message->getLocation()->getLongitude();
} }
$callback_path = 'Longman\TelegramBot\Request'; $callback_path = 'Longman\TelegramBot\Request';
$callback_function = 'send' . $type; $callback_function = 'send' . ucfirst($type);
if (!method_exists($callback_path, $callback_function)) { if (!method_exists($callback_path, $callback_function)) {
throw new TelegramException('Methods: ' . $callback_function . ' not found in class Request.'); throw new TelegramException('Methods: ' . $callback_function . ' not found in class Request.');
} }
return call_user_func_array($callback_path . '::' . $callback_function, [$data]); return $callback_path::$callback_function($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