Commit a93bf5b5 authored by MBoretto's avatar MBoretto

sent to channel works without configuration

parent 5adfab27
...@@ -26,7 +26,7 @@ class SendtochannelCommand extends AdminCommand ...@@ -26,7 +26,7 @@ class SendtochannelCommand extends AdminCommand
protected $name = 'sendtochannel'; protected $name = 'sendtochannel';
protected $description = 'Send message to a channel'; protected $description = 'Send message to a channel';
protected $usage = '/sendtochannel <message to send>'; protected $usage = '/sendtochannel <message to send>';
protected $version = '0.1.2'; protected $version = '0.1.3';
protected $need_mysql = true; protected $need_mysql = true;
/**#@-*/ /**#@-*/
...@@ -39,35 +39,51 @@ class SendtochannelCommand extends AdminCommand ...@@ -39,35 +39,51 @@ class SendtochannelCommand extends AdminCommand
$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 this protect the machine state
//if the commmad is recolled when the conversation is already started // if the commmad is recolled when the conversation is already started
if ($type == 'command') { $this->reply_to_message = isset($data['reply_to_message']) ? $data['reply_to_message'] : null;
$type = "Message"; $type = ($type == 'command') ? 'Message' : $type;
}
$text = trim($message->getText(true)); $text = trim($message->getText(true));
$data = []; $data = [];
$data['chat_id'] = $chat_id; $data['chat_id'] = $chat_id;
//tracking // Conversation
$conversation = new Conversation($user_id, $chat_id, $this->getName()); $conversation = new Conversation($user_id, $chat_id, $this->getName());
$conversation->start(); $conversation->start();
$session = $conversation->getData(); $session = $conversation->getData();
$channels = (array) $this->getConfig('your_channel');
if (!isset($session['state'])) { if (!isset($session['state'])) {
$state = '0'; $state = (count($channels) == 0) ? -1 : 0;
$session['last_message_id'] = $message->getMessageId(); $session['last_message_id'] = $message->getMessageId();
} else { } else {
$state = $session['state']; $state = $session['state'];
} }
switch ($state) {
case -1:
// getConfig has not been configured asking for channel to administer
if ($type != 'Message' || empty($text)) {
$channels = (array) $this->getConfig('your_channel'); $session['state'] = '-1';
//echo count($channels) . "\n"; $conversation->update($session);
switch ($state) { $data['text'] = 'Insert the channel name: (@yourchannel)';
$data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]);
$result = Request::sendMessage($data);
break;
}
$session['channel'] = $text;
$session['last_message_id'] = $message->getMessageId();
// Jump to state 1
goto insert;
// no break
default: default:
case 0: case 0:
if ($type != 'Message' || !in_array(trim($text), $channels)) { // getConfig has been configured choose channel
if ($type != 'Message' || !in_array($text, $channels)) {
$session['state'] = '0'; $session['state'] = '0';
$conversation->update($session); $conversation->update($session);
...@@ -85,7 +101,7 @@ class SendtochannelCommand extends AdminCommand ...@@ -85,7 +101,7 @@ class SendtochannelCommand extends AdminCommand
); );
$data['reply_markup'] = $reply_keyboard_markup; $data['reply_markup'] = $reply_keyboard_markup;
$data['text'] = 'Select a channel'; $data['text'] = 'Select a channel';
if ($type != 'Message' || !in_array(trim($text), $channels)) { 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);
...@@ -96,6 +112,7 @@ class SendtochannelCommand extends AdminCommand ...@@ -96,6 +112,7 @@ class SendtochannelCommand extends AdminCommand
// no break // no break
case 1: case 1:
insert:
if ($session['last_message_id'] == $message->getMessageId() || ($type == 'Message' && empty($text))) { if ($session['last_message_id'] == $message->getMessageId() || ($type == 'Message' && empty($text))) {
$session['state'] = 1; $session['state'] = 1;
$conversation->update($session); $conversation->update($session);
...@@ -115,6 +132,7 @@ class SendtochannelCommand extends AdminCommand ...@@ -115,6 +132,7 @@ class SendtochannelCommand extends AdminCommand
$session['state'] = 2; $session['state'] = 2;
$conversation->update($session); $conversation->update($session);
// Execute this just with object that allow caption
if ($session['message_type'] == 'Video' || $session['message_type'] == 'Photo') { if ($session['message_type'] == 'Video' || $session['message_type'] == 'Photo') {
$keyboard = [['Yes', 'No']]; $keyboard = [['Yes', 'No']];
$reply_keyboard_markup = new ReplyKeyboardMarkup( $reply_keyboard_markup = new ReplyKeyboardMarkup(
...@@ -221,7 +239,6 @@ class SendtochannelCommand extends AdminCommand ...@@ -221,7 +239,6 @@ class SendtochannelCommand extends AdminCommand
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
break; break;
} }
return $result; return $result;
} }
...@@ -271,7 +288,6 @@ class SendtochannelCommand extends AdminCommand ...@@ -271,7 +288,6 @@ class SendtochannelCommand extends AdminCommand
throw new TelegramException('Methods: '.$callback_function.' not found in class Request.'); throw new TelegramException('Methods: '.$callback_function.' not found in class Request.');
} }
$result = call_user_func_array($callback_path.'::'.$callback_function, array($data)); return call_user_func_array($callback_path.'::'.$callback_function, array($data));
return $result;
} }
} }
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