Commit 84cd8f50 authored by Avtandil Kikabidze's avatar Avtandil Kikabidze

Improved code and doc-blocks in admin and user commands

parent e986a12d
......@@ -10,9 +10,6 @@
namespace Longman\TelegramBot\Commands;
/**
* Abstract Admin Command Class
*/
abstract class AdminCommand extends Command
{
......
......@@ -15,23 +15,37 @@ use Longman\TelegramBot\DB;
use Longman\TelegramBot\Entities\Chat;
use Longman\TelegramBot\Request;
/**
* Admin "/chats" command
*/
class ChatsCommand extends AdminCommand
{
/**#@+
* {@inheritdoc}
/**
* @var string
*/
protected $name = 'chats';
/**
* @var string
*/
protected $description = 'List or search all chats stored by the bot';
/**
* @var string
*/
protected $usage = '/chats, /chats * or /chats <search string>';
/**
* @var string
*/
protected $version = '1.0.2';
/**
* @var bool
*/
protected $need_mysql = true;
/**#@-*/
/**
* {@inheritdoc}
* Command execute method
*
* @return mixed
*/
public function execute()
{
......@@ -69,7 +83,8 @@ class ChatsCommand extends AdminCommand
$whois = $chat->getId();
if ($this->telegram->getCommandObject('whois')) {
$whois = '/whois' . str_replace('-', 'g', $chat->getId()); //We can't use '-' in command because part of it will become unclickable
$whois = '/whois' . str_replace('-', 'g',
$chat->getId()); //We can't use '-' in command because part of it will become unclickable
}
if ($chat->isPrivateChat()) {
......@@ -102,7 +117,7 @@ class ChatsCommand extends AdminCommand
$text_back .= "\n" . 'Total: ' . ($user_chats + $group_chats + $super_group_chats);
if ($text === '') {
$text_back .= "\n\n" . 'List all chats: /' . $this->name .' *' . "\n" . 'Search for chats: /' . $this->name .' <search string>';
$text_back .= "\n\n" . 'List all chats: /' . $this->name . ' *' . "\n" . 'Search for chats: /' . $this->name . ' <search string>';
}
}
......
......@@ -18,15 +18,30 @@ use Longman\TelegramBot\Request;
*/
class SendtoallCommand extends AdminCommand
{
/**#@+
* {@inheritdoc}
/**
* @var string
*/
protected $name = 'sendtoall';
/**
* @var string
*/
protected $description = 'Send the message to all the user\'s bot';
/**
* @var string
*/
protected $usage = '/sendtoall <message to send>';
/**
* @var string
*/
protected $version = '1.2.1';
/**
* @var bool
*/
protected $need_mysql = true;
/**#@-*/
/**
* Execute command
......
......@@ -20,15 +20,30 @@ use Longman\TelegramBot\Exception\TelegramException;
class SendtochannelCommand extends AdminCommand
{
/**#@+
* {@inheritdoc}
/**
* @var string
*/
protected $name = 'sendtochannel';
/**
* @var string
*/
protected $description = 'Send message to a channel';
/**
* @var string
*/
protected $usage = '/sendtochannel <message to send>';
/**
* @var string
*/
protected $version = '0.1.4';
/**
* @var bool
*/
protected $need_mysql = true;
/**#@-*/
/**
* Conversation Object
......@@ -38,7 +53,9 @@ class SendtochannelCommand extends AdminCommand
protected $conversation;
/**
* {@inheritdoc}
* Command execute method
*
* @return \Longman\TelegramBot\Entities\ServerResponse|mixed
*/
public function execute()
{
......@@ -57,7 +74,7 @@ class SendtochannelCommand extends AdminCommand
// Conversation
$this->conversation = new Conversation($user_id, $chat_id, $this->getName());
$channels = (array) $this->getConfig('your_channel');
$channels = (array)$this->getConfig('your_channel');
if (!isset($this->conversation->notes['state'])) {
$state = (count($channels) == 0) ? -1 : 0;
$this->conversation->notes['last_message_id'] = $message->getMessageId();
......@@ -96,7 +113,7 @@ class SendtochannelCommand extends AdminCommand
}
$reply_keyboard_markup = new ReplyKeyboardMarkup(
[
'keyboard' => $keyboard ,
'keyboard' => $keyboard,
'resize_keyboard' => true,
'one_time_keyboard' => true,
'selective' => true
......@@ -139,7 +156,7 @@ class SendtochannelCommand extends AdminCommand
$keyboard = [['Yes', 'No']];
$reply_keyboard_markup = new ReplyKeyboardMarkup(
[
'keyboard' => $keyboard ,
'keyboard' => $keyboard,
'resize_keyboard' => true,
'one_time_keyboard' => true,
'selective' => true
......@@ -162,7 +179,7 @@ class SendtochannelCommand extends AdminCommand
$this->conversation->notes['last_message_id'] = $message->getMessageId();
// no break
case 3:
if (($this->conversation->notes['last_message_id'] == $message->getMessageId() || $type != 'Message' ) && $this->conversation->notes['set_caption']) {
if (($this->conversation->notes['last_message_id'] == $message->getMessageId() || $type != 'Message') && $this->conversation->notes['set_caption']) {
$this->conversation->notes['state'] = 3;
$this->conversation->update();
......@@ -195,7 +212,7 @@ class SendtochannelCommand extends AdminCommand
$keyboard = [['Yes', 'No']];
$reply_keyboard_markup = new ReplyKeyboardMarkup(
[
'keyboard' => $keyboard ,
'keyboard' => $keyboard,
'resize_keyboard' => true,
'one_time_keyboard' => true,
'selective' => true
......@@ -233,7 +250,9 @@ class SendtochannelCommand extends AdminCommand
}
/**
* {@inheritdoc}
* Execute without db
*
* @return mixed
*/
public function executeNoDb()
{
......@@ -247,7 +266,7 @@ class SendtochannelCommand extends AdminCommand
if ($text === '') {
$data['text'] = 'Usage: /sendtochannel <text>';
} else {
$channels = (array) $this->getConfig('your_channel');
$channels = (array)$this->getConfig('your_channel');
$first_channel = $channels[0];
$data['text'] = $this->publish(new Message($message->reflect(), 'anystring'), $first_channel);
}
......@@ -295,6 +314,7 @@ class SendtochannelCommand extends AdminCommand
* @param array $data
*
* @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
protected function sendBack(Message $message, array $data)
{
......@@ -323,7 +343,7 @@ class SendtochannelCommand extends AdminCommand
}
$callback_path = 'Longman\TelegramBot\Request';
$callback_function = 'send' . $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.');
}
......
......@@ -22,18 +22,35 @@ use Longman\TelegramBot\Request;
*/
class WhoisCommand extends AdminCommand
{
/**#@+
* {@inheritdoc}
/**
* @var string
*/
protected $name = 'whois';
/**
* @var string
*/
protected $description = 'Lookup user or group info';
/**
* @var string
*/
protected $usage = '/whois <id> or /whois <search string>';
/**
* @var string
*/
protected $version = '1.1.0';
/**
* @var bool
*/
protected $need_mysql = true;
/**#@-*/
/**
* {@inheritdoc}
* Command execute method
*
* @return mixed
*/
public function execute()
{
......@@ -43,7 +60,7 @@ class WhoisCommand extends AdminCommand
$command = $message->getCommand();
$text = trim($message->getText(true));
$data = [ 'chat_id' => $chat_id ];
$data = ['chat_id' => $chat_id];
//No point in replying to messages in private chats
if (!$message->getChat()->isPrivateChat()) {
......@@ -117,7 +134,7 @@ class WhoisCommand extends AdminCommand
$limit = 10;
$offset = null;
$ServerResponse = Request::getUserProfilePhotos([
'user_id' => $user_id ,
'user_id' => $user_id,
'limit' => $limit,
'offset' => $offset,
]);
......@@ -140,7 +157,7 @@ class WhoisCommand extends AdminCommand
return Request::sendPhoto($data);
}
} 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 . ')' : '') . "\n";
$text .= 'Type: ' . ucfirst($chat->getType()) . "\n";
$text .= 'Title: ' . $chat->getTitle() . "\n";
$text .= 'First time added to group: ' . $created_at . "\n";
......
......@@ -15,9 +15,6 @@ use Longman\TelegramBot\Request;
use Longman\TelegramBot\Telegram;
use Longman\TelegramBot\Entities\Update;
/**
* Abstract Command Class
*/
abstract class Command
{
/**
......
......@@ -12,9 +12,6 @@ namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
/**
* Abstract System Command Class
*/
abstract class SystemCommand extends Command
{
/**
......@@ -23,7 +20,7 @@ abstract class SystemCommand extends Command
* Although system commands should just work and return a successful ServerResponse,
* each system command can override this method to add custom functionality.
*
* @return Entities\ServerResponse
* @return \Longman\TelegramBot\Entities\ServerResponse
*/
public function execute()
{
......
......@@ -10,9 +10,6 @@
namespace Longman\TelegramBot\Commands;
/**
* Abstract User Command Class
*/
abstract class UserCommand extends Command
{
......
......@@ -24,18 +24,35 @@ use Longman\TelegramBot\Request;
*/
class CancelCommand extends UserCommand
{
/**#@+
* {@inheritdoc}
/**
* @var string
*/
protected $name = 'cancel';
/**
* @var string
*/
protected $description = 'Cancel the currently active conversation';
/**
* @var string
*/
protected $usage = '/cancel';
/**
* @var string
*/
protected $version = '0.1.1';
/**
* @var bool
*/
protected $need_mysql = true;
/**#@-*/
/**
* {@inheritdoc}
* Command execute method
*
* @return \Longman\TelegramBot\Entities\ServerResponse
*/
public function execute()
{
......@@ -56,7 +73,9 @@ class CancelCommand extends UserCommand
}
/**
* {@inheritdoc}
* Execute no db
*
* @return \Longman\TelegramBot\Entities\ServerResponse
*/
public function executeNoDb()
{
......@@ -72,10 +91,12 @@ class CancelCommand extends UserCommand
*/
private function hideKeyboard($text)
{
return Request::sendMessage([
return Request::sendMessage(
[
'reply_markup' => new ReplyKeyboardHide(['selective' => true]),
'chat_id' => $this->getMessage()->getChat()->getId(),
'text' => $text,
]);
]
);
}
}
......@@ -21,14 +21,25 @@ use Longman\TelegramBot\Request;
*/
class DateCommand extends UserCommand
{
/**#@+
* {@inheritdoc}
/**
* @var string
*/
protected $name = 'date';
/**
* @var string
*/
protected $description = 'Show date/time by location';
/**
* @var string
*/
protected $usage = '/date <location>';
/**
* @var string
*/
protected $version = '1.3.0';
/**#@-*/
/**
* Guzzle Client object
......@@ -64,6 +75,7 @@ class DateCommand extends UserCommand
* @param string $location
*
* @return array|boolean
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
private function getCoordinates($location)
{
......@@ -100,6 +112,7 @@ class DateCommand extends UserCommand
* @param string $lng
*
* @return array|boolean
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
private function getDate($lat, $lng)
{
......@@ -184,7 +197,9 @@ class DateCommand extends UserCommand
}
/**
* {@inheritdoc}
* Command execute method
*
* @return mixed
*/
public function execute()
{
......
......@@ -18,17 +18,30 @@ use Longman\TelegramBot\Request;
*/
class EchoCommand extends UserCommand
{
/**#@+
* {@inheritdoc}
/**
* @var string
*/
protected $name = 'echo';
/**
* @var string
*/
protected $description = 'Show text';
/**
* @var string
*/
protected $usage = '/echo <text>';
/**
* @var string
*/
protected $version = '1.0.1';
/**#@-*/
/**
* {@inheritdoc}
* Command execute method
*
* @return mixed
*/
public function execute()
{
......
......@@ -18,17 +18,30 @@ use Longman\TelegramBot\Request;
*/
class HelpCommand extends UserCommand
{
/**#@+
* {@inheritdoc}
/**
* @var string
*/
protected $name = 'help';
/**
* @var string
*/
protected $description = 'Show bot commands help';
/**
* @var string
*/
protected $usage = '/help or /help <command>';
/**
* @var string
*/
protected $version = '1.0.1';
/**#@-*/
/**
* {@inheritdoc}
* Command execute method
*
* @return mixed
*/
public function execute()
{
......
......@@ -18,17 +18,30 @@ use Longman\TelegramBot\Request;
*/
class SlapCommand extends UserCommand
{
/**#@+
* {@inheritdoc}
/**
* @var string
*/
protected $name = 'slap';
/**
* @var string
*/
protected $description = 'Slap someone with their username';
/**
* @var string
*/
protected $usage = '/slap <@user>';
/**
* @var string
*/
protected $version = '1.0.1';
/**#@-*/
/**
* {@inheritdoc}
* Command execute method
*
* @return mixed
*/
public function execute()
{
......
......@@ -22,15 +22,30 @@ use Longman\TelegramBot\Entities\ReplyKeyboardMarkup;
*/
class SurveyCommand extends UserCommand
{
/**#@+
* {@inheritdoc}
/**
* @var string
*/
protected $name = 'survey';
/**
* @var string
*/
protected $description = 'Survery for bot users';
/**
* @var string
*/
protected $usage = '/survey';
/**
* @var string
*/
protected $version = '0.2.0';
/**
* @var bool
*/
protected $need_mysql = true;
/**#@-*/
/**
* Conversation Object
......@@ -40,7 +55,9 @@ class SurveyCommand extends UserCommand
protected $conversation;
/**
* {@inheritdoc}
* Command execute method
*
* @return mixed
*/
public function execute()
{
......@@ -58,7 +75,7 @@ class SurveyCommand extends UserCommand
if ($chat->isGroupChat() || $chat->isSuperGroup()) {
//reply to message id is applied by default
//Force reply is applied by default to so can work with privacy on
$data['reply_markup'] = new ForceReply([ 'selective' => true]);
$data['reply_markup'] = new ForceReply(['selective' => true]);
}
$data['chat_id'] = $chat_id;
......@@ -124,10 +141,10 @@ class SurveyCommand extends UserCommand
$this->conversation->notes['state'] = 3;
$this->conversation->update();
$keyboard = [['M','F']];
$keyboard = [['M', 'F']];
$reply_keyboard_markup = new ReplyKeyboardMarkup(
[
'keyboard' => $keyboard ,
'keyboard' => $keyboard,
'resize_keyboard' => true,
'one_time_keyboard' => true,
'selective' => true
......@@ -149,14 +166,21 @@ class SurveyCommand extends UserCommand
if (is_null($message->getLocation())) {
$this->conversation->notes['state'] = 4;
$this->conversation->update();
$data['reply_markup'] = new ReplyKeyboardMarkup([
'keyboard' => [[
[ 'text' => 'Share Location', 'request_location' => true ],
]],
$data['reply_markup'] = new ReplyKeyboardMarkup(
[
'keyboard' => [
[
[
'text' => 'Share Location',
'request_location' => true
],
]
],
'resize_keyboard' => true,
'one_time_keyboard' => true,
'selective' => true,
]);
]
);
$data['text'] = 'Share your location:';
$result = Request::sendMessage($data);
break;
......@@ -185,14 +209,21 @@ class SurveyCommand extends UserCommand
$this->conversation->update();
$data['text'] = 'Share your contact information:';
$data['reply_markup'] = new ReplyKeyboardMarkup([
'keyboard' => [[
[ 'text' => 'Share Contact', 'request_contact' => true ],
]],
$data['reply_markup'] = new ReplyKeyboardMarkup(
[
'keyboard' => [
[
[
'text' => 'Share Contact',
'request_contact' => true
],
]
],
'resize_keyboard' => true,
'one_time_keyboard' => true,
'selective' => true,
]);
]
);
$result = Request::sendMessage($data);
break;
}
......@@ -204,7 +235,7 @@ class SurveyCommand extends UserCommand
$out_text = '/Survey result:' . "\n";
unset($this->conversation->notes['state']);
foreach ($this->conversation->notes as $k => $v) {
$out_text .= "\n" . ucfirst($k).': ' . $v;
$out_text .= "\n" . ucfirst($k) . ': ' . $v;
}
$data['photo'] = $this->conversation->notes['photo_id'];
......
......@@ -21,14 +21,25 @@ use Longman\TelegramBot\Request;
*/
class WeatherCommand extends UserCommand
{
/**#@+
* {@inheritdoc}
/**
* @var string
*/
protected $name = 'weather';
/**
* @var string
*/
protected $description = 'Show weather by location';
/**
* @var string
*/
protected $usage = '/weather <location>';
/**
* @var string
*/
protected $version = '1.1.0';
/**#@-*/
/**
* Base URI for OpenWeatherMap API
......@@ -43,6 +54,7 @@ class WeatherCommand extends UserCommand
* @param string $location
*
* @return string
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
private function getWeatherData($location)
{
......@@ -60,7 +72,7 @@ class WeatherCommand extends UserCommand
throw new TelegramException($e->getMessage());
}
return (string) $response->getBody();
return (string)$response->getBody();
}
/**
......@@ -103,7 +115,9 @@ class WeatherCommand extends UserCommand
}
/**
* {@inheritdoc}
* Command execute method
*
* @return mixed
*/
public function execute()
{
......
......@@ -21,18 +21,35 @@ use Longman\TelegramBot\Request;
*/
class WhoamiCommand extends UserCommand
{
/**#@+
* {@inheritdoc}
/**
* @var string
*/
protected $name = 'whoami';
/**
* @var string
*/
protected $description = 'Show your id, name and username';
/**
* @var string
*/
protected $usage = '/whoami';
/**
* @var string
*/
protected $version = '1.0.1';
/**
* @var bool
*/
protected $public = true;
/**#@-*/
/**
* {@inheritdoc}
* Command execute method
*
* @return mixed
*/
public function execute()
{
......@@ -55,7 +72,7 @@ class WhoamiCommand extends UserCommand
$limit = 10;
$offset = null;
$ServerResponse = Request::getUserProfilePhotos([
'user_id' => $user_id ,
'user_id' => $user_id,
'limit' => $limit,
'offset' => $offset,
]);
......
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