Commit 765a9c40 authored by Armando Lüscher's avatar Armando Lüscher

Initial code cleanup of `Telegram.php`.

No functional changes, only slight modifications of documentation and code layout.
This commit is mainly to discuss with the project members if this style is ok and can be further refined.
parent dfa7ee4f
...@@ -23,7 +23,6 @@ use Longman\TelegramBot\Exception\TelegramException; ...@@ -23,7 +23,6 @@ use Longman\TelegramBot\Exception\TelegramException;
*/ */
class Telegram class Telegram
{ {
/** /**
* Version * Version
* *
...@@ -67,7 +66,7 @@ class Telegram ...@@ -67,7 +66,7 @@ class Telegram
protected $update; protected $update;
/** /**
* Log Requests * Log requests
* *
* @var bool * @var bool
*/ */
...@@ -81,14 +80,14 @@ class Telegram ...@@ -81,14 +80,14 @@ class Telegram
protected $log_path; protected $log_path;
/** /**
* Upload Path * Upload path
* *
* @var string * @var string
*/ */
protected $upload_path; protected $upload_path;
/** /**
* Dowload Path * Download path
* *
* @var string * @var string
*/ */
...@@ -102,7 +101,7 @@ class Telegram ...@@ -102,7 +101,7 @@ class Telegram
protected $log_verbosity; protected $log_verbosity;
/** /**
* MySQL Integration * MySQL integration
* *
* @var boolean * @var boolean
*/ */
...@@ -127,14 +126,15 @@ class Telegram ...@@ -127,14 +126,15 @@ class Telegram
* *
* @var array * @var array
*/ */
protected $message_types = [
protected $message_types =['Message', 'Photo', 'Audio', 'Document', 'Sticker', 'Video', 'Message', 'Photo', 'Audio', 'Document', 'Sticker', 'Video',
'Voice', 'Location', 'command', 'new_chat_participant', 'Voice', 'Location', 'command', 'new_chat_participant',
'left_chat_participant', 'new_chat_title', 'delete_chat_photo', 'left_chat_participant', 'new_chat_title', 'delete_chat_photo',
'group_chat_created', 'supergroup_chat_created', 'channel_chat_created', 'group_chat_created', 'supergroup_chat_created', 'channel_chat_created',
]; ];
/** /**
* Admins List * Admins list
* *
* @var array * @var array
*/ */
...@@ -145,15 +145,14 @@ class Telegram ...@@ -145,15 +145,14 @@ class Telegram
* *
* @var boolean * @var boolean
*/ */
protected $admin_enabled = false; protected $admin_enabled = false;
/** /**
* Constructor * Constructor
* *
* @param string $api_key * @param string $api_key
* @param string $bot_name
*/ */
public function __construct($api_key, $bot_name) public function __construct($api_key, $bot_name)
{ {
if (empty($api_key)) { if (empty($api_key)) {
...@@ -167,21 +166,21 @@ class Telegram ...@@ -167,21 +166,21 @@ class Telegram
$this->api_key = $api_key; $this->api_key = $api_key;
$this->bot_name = $bot_name; $this->bot_name = $bot_name;
//Set default download and upload dir //Set default download and upload dir
$this->setDownloadPath(BASE_PATH . "/../Download"); $this->setDownloadPath(BASE_PATH . '/../Download');
$this->setUploadPath(BASE_PATH . "/../Upload"); $this->setUploadPath(BASE_PATH . '/../Upload');
Request::initialize($this); Request::initialize($this);
} }
/** /**
* Initialize * Initialize
* *
* @param array credential, string table_prefix * @param array $credential
* @param string $table_prefix
*/ */
public function enableMySQL(array $credential, $table_prefix = null) public function enableMySQL(array $credential, $table_prefix = null)
{ {
$this->pdo = DB::initialize($credential, $this, $table_prefix); $this->pdo = DB::initialize($credential, $this, $table_prefix);
$this->mysql_enabled = true; $this->mysql_enabled = true;
} }
...@@ -192,8 +191,8 @@ class Telegram ...@@ -192,8 +191,8 @@ class Telegram
*/ */
public function getCommandsList() public function getCommandsList()
{ {
$commands = []; $commands = [];
try { try {
$files = new \DirectoryIterator(BASE_PATH . '/Commands'); $files = new \DirectoryIterator(BASE_PATH . '/Commands');
} catch (\Exception $e) { } catch (\Exception $e) {
...@@ -230,6 +229,7 @@ class Telegram ...@@ -230,6 +229,7 @@ class Telegram
} }
} }
} }
return $commands; return $commands;
} }
...@@ -243,7 +243,7 @@ class Telegram ...@@ -243,7 +243,7 @@ class Telegram
public function setLogRequests($log_requests) public function setLogRequests($log_requests)
{ {
$this->log_requests = $log_requests; $this->log_requests = $log_requests;
//set default log verbosity //Set default log verbosity
$this->log_verbosity = 1; $this->log_verbosity = 1;
return $this; return $this;
} }
...@@ -274,8 +274,6 @@ class Telegram ...@@ -274,8 +274,6 @@ class Telegram
/** /**
* Get log path * Get log path
* *
* @param string $log_path
*
* @return string * @return string
*/ */
public function getLogPath() public function getLogPath()
...@@ -283,7 +281,6 @@ class Telegram ...@@ -283,7 +281,6 @@ class Telegram
return $this->log_path; return $this->log_path;
} }
/** /**
* Set log Verbosity * Set log Verbosity
* *
...@@ -303,7 +300,6 @@ class Telegram ...@@ -303,7 +300,6 @@ class Telegram
/** /**
* Get log verbosity * Get log verbosity
* *
*
* @return int * @return int
*/ */
public function getLogVerbosity() public function getLogVerbosity()
...@@ -337,9 +333,8 @@ class Telegram ...@@ -337,9 +333,8 @@ class Telegram
/** /**
* Handle getUpdates method * Handle getUpdates method
* *
* * @todo Complete DocBlock
*/ */
public function handleGetUpdates($limit = null, $timeout = null) public function handleGetUpdates($limit = null, $timeout = null)
{ {
//DB Query //DB Query
...@@ -358,7 +353,6 @@ class Telegram ...@@ -358,7 +353,6 @@ class Telegram
'timeout' => $timeout 'timeout' => $timeout
]); ]);
if ($ServerResponse->isOk()) { if ($ServerResponse->isOk()) {
$results = ''; $results = '';
$n_update = count($ServerResponse->getResult()); $n_update = count($ServerResponse->getResult());
...@@ -377,7 +371,6 @@ class Telegram ...@@ -377,7 +371,6 @@ class Telegram
*/ */
public function handle() public function handle()
{ {
$this->input = Request::getInput(); $this->input = Request::getInput();
if (empty($this->input)) { if (empty($this->input)) {
throw new TelegramException('Input is empty!'); throw new TelegramException('Input is empty!');
...@@ -396,32 +389,31 @@ class Telegram ...@@ -396,32 +389,31 @@ class Telegram
* *
* @return \Longman\TelegramBot\Telegram * @return \Longman\TelegramBot\Telegram
*/ */
public function processUpdate(Update $update)
public function processUpdate(update $update)
{ {
$update_type = $update->getUpdateType(); $update_type = $update->getUpdateType();
if ($update_type == 'message') { if ($update_type == 'message') {
//Load admin Commands //Load admin Commands
if ($this->admin_enabled) { if ($this->admin_enabled) {
$message = $update->getMessage(); $message = $update->getMessage();
//Admin command avaiable in any chats //Admin command avaiable in any chats
//$from = $message->getFrom(); //$from = $message->getFrom();
//$user_id = $from->getId(); //$user_id = $from->getId();
//Admin command avaiable only in single chat with the bot //Admin command avaiable only in single chat with the bot
$chat = $message->getChat(); $chat = $message->getChat();
$user_id = $chat->getId(); $user_id = $chat->getId();
if (in_array($user_id, $this->admins_list)) { if (in_array($user_id, $this->admins_list)) {
$this->addCommandsPath(BASE_PATH.'/Admin'); $this->addCommandsPath(BASE_PATH . '/Admin');
} }
} }
// check type // check type
$message = $update->getMessage(); $message = $update->getMessage();
$type = $message->getType(); $type = $message->getType();
switch ($type) { switch ($type) {
default: default:
case 'Message': case 'Message':
...@@ -481,6 +473,8 @@ class Telegram ...@@ -481,6 +473,8 @@ class Telegram
/** /**
* Execute /command * Execute /command
* *
* @todo Complete DocBlock
*
* @return mixed * @return mixed
*/ */
public function executeCommand($command, Update $update) public function executeCommand($command, Update $update)
...@@ -504,6 +498,8 @@ class Telegram ...@@ -504,6 +498,8 @@ class Telegram
/** /**
* Get command class * Get command class
* *
* @todo Complete DocBlock
*
* @return object * @return object
*/ */
public function getCommandClass($command, Update $update = null) public function getCommandClass($command, Update $update = null)
...@@ -545,7 +541,9 @@ class Telegram ...@@ -545,7 +541,9 @@ class Telegram
return false; return false;
} }
/**
* @todo Complete DocBlock
*/
protected function sanitizeCommand($string, $capitalizeFirstCharacter = false) protected function sanitizeCommand($string, $capitalizeFirstCharacter = false)
{ {
$str = str_replace(' ', '', ucwords(str_replace('_', ' ', $string))); $str = str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
...@@ -555,7 +553,7 @@ class Telegram ...@@ -555,7 +553,7 @@ class Telegram
/** /**
* Enable Admin Account * Enable Admin Account
* *
* @param array list of admins * @param array $admins_list List of admins
* *
* @return string * @return string
*/ */
...@@ -565,7 +563,7 @@ class Telegram ...@@ -565,7 +563,7 @@ class Telegram
if ($admin > 0) { if ($admin > 0) {
$this->admins_list[] = $admin; $this->admins_list[] = $admin;
} else { } else {
throw new TelegramException('Invalid value "'.$admin.'" for admin!'); throw new TelegramException('Invalid value "' . $admin . '" for admin!');
} }
} }
...@@ -575,7 +573,7 @@ class Telegram ...@@ -575,7 +573,7 @@ class Telegram
} }
/** /**
* check id user require the db connection * Check if user required the db connection
* *
* @return bool * @return bool
*/ */
...@@ -588,11 +586,12 @@ class Telegram ...@@ -588,11 +586,12 @@ class Telegram
} }
} }
/** /**
* Add custom commands path * Add custom commands path
* *
* @return object * @param string $folder Custom commands path
*
* @return \Longman\TelegramBot\Telegram
*/ */
public function addCommandsPath($folder) public function addCommandsPath($folder)
{ {
...@@ -603,11 +602,12 @@ class Telegram ...@@ -603,11 +602,12 @@ class Telegram
return $this; return $this;
} }
/** /**
* Set custom upload path * Set custom upload path
* *
* @return object * @param string $folder Custom upload path
*
* @return \Longman\TelegramBot\Telegram
*/ */
public function setUploadPath($folder) public function setUploadPath($folder)
{ {
...@@ -626,9 +626,11 @@ class Telegram ...@@ -626,9 +626,11 @@ class Telegram
} }
/** /**
* Set custom Download path * Set custom download path
* *
* @return object * @param string $folder Custom download path
*
* @return \Longman\TelegramBot\Telegram
*/ */
public function setDownloadPath($folder) public function setDownloadPath($folder)
{ {
...@@ -637,7 +639,7 @@ class Telegram ...@@ -637,7 +639,7 @@ class Telegram
} }
/** /**
* Get custom Download path * Get custom download path
* *
* @return string * @return string
*/ */
...@@ -649,7 +651,9 @@ class Telegram ...@@ -649,7 +651,9 @@ class Telegram
/** /**
* Set command config * Set command config
* *
* @return object * @todo Complete DocBlock
*
* @return \Longman\TelegramBot\Telegram
*/ */
public function setCommandConfig($command, array $array) public function setCommandConfig($command, array $array)
{ {
...@@ -667,9 +671,8 @@ class Telegram ...@@ -667,9 +671,8 @@ class Telegram
return isset($this->commands_config[$command]) ? $this->commands_config[$command] : array(); return isset($this->commands_config[$command]) ? $this->commands_config[$command] : array();
} }
/** /**
* Get API KEY * Get API key
* *
* @return string * @return string
*/ */
...@@ -679,7 +682,7 @@ class Telegram ...@@ -679,7 +682,7 @@ class Telegram
} }
/** /**
* Get BOT NAME * Get Bot name
* *
* @return string * @return string
*/ */
...@@ -701,6 +704,8 @@ class Telegram ...@@ -701,6 +704,8 @@ class Telegram
/** /**
* Set Webhook for bot * Set Webhook for bot
* *
* @todo Complete DocBlock
*
* @return string * @return string
*/ */
public function setWebHook($url, $path_certificate = null) public function setWebHook($url, $path_certificate = null)
...@@ -713,7 +718,7 @@ class Telegram ...@@ -713,7 +718,7 @@ class Telegram
if (!$result->isOk()) { if (!$result->isOk()) {
throw new TelegramException( throw new TelegramException(
'Webhook was not set! Error: '.$result->getErrorCode().' '. $result->getDescription() 'Webhook was not set! Error: ' . $result->getErrorCode() . ' ' . $result->getDescription()
); );
} }
...@@ -731,7 +736,7 @@ class Telegram ...@@ -731,7 +736,7 @@ class Telegram
if (!$result->isOk()) { if (!$result->isOk()) {
throw new TelegramException( throw new TelegramException(
'Webhook was not unset! Error: '.$result->getErrorCode().' '. $result->getDescription() 'Webhook was not unset! Error: ' . $result->getErrorCode() . ' ' . $result->getDescription()
); );
} }
......
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