Commit ca48e792 authored by Jack'lul's avatar Jack'lul

Rename variables 'bot_name' and similar to 'bot_username' to prevent confusion

parent b2ec9768
...@@ -14,7 +14,7 @@ require __DIR__ . '/vendor/autoload.php'; ...@@ -14,7 +14,7 @@ require __DIR__ . '/vendor/autoload.php';
// Add you bot's API key and name // Add you bot's API key and name
$API_KEY = 'your_bot_api_key'; $API_KEY = 'your_bot_api_key';
$BOT_NAME = 'username_bot'; $BOT_USERNAME = 'username_bot';
// Define a path for your custom commands // Define a path for your custom commands
//$commands_path = __DIR__ . '/Commands/'; //$commands_path = __DIR__ . '/Commands/';
...@@ -29,7 +29,7 @@ $mysql_credentials = [ ...@@ -29,7 +29,7 @@ $mysql_credentials = [
try { try {
// Create Telegram API object // Create Telegram API object
$telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME); $telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_USERNAME);
// Error, Debug and Raw Update logging // Error, Debug and Raw Update logging
//Longman\TelegramBot\TelegramLog::initialize($your_external_monolog_instance); //Longman\TelegramBot\TelegramLog::initialize($your_external_monolog_instance);
......
...@@ -13,7 +13,7 @@ require __DIR__ . '/vendor/autoload.php'; ...@@ -13,7 +13,7 @@ require __DIR__ . '/vendor/autoload.php';
// Add you bot's API key and name // Add you bot's API key and name
$API_KEY = 'your_bot_api_key'; $API_KEY = 'your_bot_api_key';
$BOT_NAME = 'username_bot'; $BOT_USERNAME = 'username_bot';
// Define a path for your custom commands // Define a path for your custom commands
//$commands_path = __DIR__ . '/Commands/'; //$commands_path = __DIR__ . '/Commands/';
...@@ -28,7 +28,7 @@ $BOT_NAME = 'username_bot'; ...@@ -28,7 +28,7 @@ $BOT_NAME = 'username_bot';
try { try {
// Create Telegram API object // Create Telegram API object
$telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME); $telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_USERNAME);
// Error, Debug and Raw Update logging // Error, Debug and Raw Update logging
//Longman\TelegramBot\TelegramLog::initialize($your_external_monolog_instance); //Longman\TelegramBot\TelegramLog::initialize($your_external_monolog_instance);
......
...@@ -3,11 +3,12 @@ ...@@ -3,11 +3,12 @@
require __DIR__ . '/vendor/autoload.php'; require __DIR__ . '/vendor/autoload.php';
$API_KEY = 'your_bot_api_key'; $API_KEY = 'your_bot_api_key';
$BOT_NAME = 'username_bot'; $BOT_USERNAME = 'username_bot';
$hook_url = 'https://yourdomain/path/to/hook.php'; $hook_url = 'https://yourdomain/path/to/hook.php';
try { try {
// Create Telegram API object // Create Telegram API object
$telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME); $telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_USERNAME);
// Set webhook // Set webhook
$result = $telegram->setWebhook($hook_url); $result = $telegram->setWebhook($hook_url);
......
...@@ -3,10 +3,11 @@ ...@@ -3,10 +3,11 @@
require __DIR__ . '/vendor/autoload.php'; require __DIR__ . '/vendor/autoload.php';
$API_KEY = 'your_bot_api_key'; $API_KEY = 'your_bot_api_key';
$BOT_NAME = 'username_bot'; $BOT_USERNAME = 'username_bot';
try { try {
// Create Telegram API object // Create Telegram API object
$telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME); $telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_USERNAME);
// Delete webhook // Delete webhook
$result = $telegram->deleteWebhook(); $result = $telegram->deleteWebhook();
......
...@@ -29,14 +29,14 @@ abstract class Entity ...@@ -29,14 +29,14 @@ abstract class Entity
/** /**
* Entity constructor. * Entity constructor.
* *
* @todo Get rid of the $bot_name, it shouldn't be here! * @todo Get rid of the $bot_username, it shouldn't be here!
* *
* @param array $data * @param array $data
* @param string $bot_name * @param string $bot_username
* *
* @throws \Longman\TelegramBot\Exception\TelegramException * @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function __construct($data, $bot_name = '') public function __construct($data, $bot_username = '')
{ {
//Make sure we're not raw_data inception-ing //Make sure we're not raw_data inception-ing
if (array_key_exists('raw_data', $data)) { if (array_key_exists('raw_data', $data)) {
...@@ -47,7 +47,7 @@ abstract class Entity ...@@ -47,7 +47,7 @@ abstract class Entity
$data['raw_data'] = $data; $data['raw_data'] = $data;
} }
$data['bot_name'] = $bot_name; $data['bot_username'] = $bot_username;
$this->assignMemberVariables($data); $this->assignMemberVariables($data);
$this->validate(); $this->validate();
} }
...@@ -142,7 +142,7 @@ abstract class Entity ...@@ -142,7 +142,7 @@ abstract class Entity
$sub_entities = $this->subEntities(); $sub_entities = $this->subEntities();
if (isset($sub_entities[$property_name])) { if (isset($sub_entities[$property_name])) {
return new $sub_entities[$property_name]($property, $this->getProperty('bot_name')); return new $sub_entities[$property_name]($property, $this->getProperty('bot_username'));
} }
return $property; return $property;
......
...@@ -79,11 +79,11 @@ class Message extends Entity ...@@ -79,11 +79,11 @@ class Message extends Entity
* Message constructor * Message constructor
* *
* @param array $data * @param array $data
* @param string $bot_name * @param string $bot_username
* *
* @throws \Longman\TelegramBot\Exception\TelegramException * @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function __construct(array $data, $bot_name = '') public function __construct(array $data, $bot_username = '')
{ {
//Retro-compatibility //Retro-compatibility
if (isset($data['new_chat_participant'])) { if (isset($data['new_chat_participant'])) {
...@@ -95,7 +95,7 @@ class Message extends Entity ...@@ -95,7 +95,7 @@ class Message extends Entity
unset($data['left_chat_participant']); unset($data['left_chat_participant']);
} }
parent::__construct($data, $bot_name); parent::__construct($data, $bot_username);
} }
/** /**
......
...@@ -21,16 +21,16 @@ class ReplyToMessage extends Message ...@@ -21,16 +21,16 @@ class ReplyToMessage extends Message
* ReplyToMessage constructor. * ReplyToMessage constructor.
* *
* @param array $data * @param array $data
* @param string $bot_name * @param string $bot_username
* *
* @throws \Longman\TelegramBot\Exception\TelegramException * @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function __construct(array $data, $bot_name = '') public function __construct(array $data, $bot_username = '')
{ {
//As explained in the documentation //As explained in the documentation
//Reply to message can't contain other reply to message entities //Reply to message can't contain other reply to message entities
unset($data['reply_to_message']); unset($data['reply_to_message']);
parent::__construct($data, $bot_name); parent::__construct($data, $bot_username);
} }
} }
...@@ -26,11 +26,11 @@ class ServerResponse extends Entity ...@@ -26,11 +26,11 @@ class ServerResponse extends Entity
* ServerResponse constructor. * ServerResponse constructor.
* *
* @param array $data * @param array $data
* @param string $bot_name * @param string $bot_username
* *
* @throws \Longman\TelegramBot\Exception\TelegramException * @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function __construct(array $data, $bot_name) public function __construct(array $data, $bot_username)
{ {
// Make sure we don't double-save the raw_data // Make sure we don't double-save the raw_data
unset($data['raw_data']); unset($data['raw_data']);
...@@ -41,13 +41,13 @@ class ServerResponse extends Entity ...@@ -41,13 +41,13 @@ class ServerResponse extends Entity
if ($is_ok && is_array($result)) { if ($is_ok && is_array($result)) {
if ($this->isAssoc($result)) { if ($this->isAssoc($result)) {
$data['result'] = $this->createResultObject($result, $bot_name); $data['result'] = $this->createResultObject($result, $bot_username);
} else { } else {
$data['result'] = $this->createResultObjects($result, $bot_name); $data['result'] = $this->createResultObjects($result, $bot_username);
} }
} }
parent::__construct($data, $bot_name); parent::__construct($data, $bot_username);
} }
/** /**
...@@ -88,12 +88,12 @@ class ServerResponse extends Entity ...@@ -88,12 +88,12 @@ class ServerResponse extends Entity
* Create and return the object of the received result * Create and return the object of the received result
* *
* @param array $result * @param array $result
* @param string $bot_name * @param string $bot_username
* *
* @return \Longman\TelegramBot\Entities\Chat|\Longman\TelegramBot\Entities\ChatMember|\Longman\TelegramBot\Entities\File|\Longman\TelegramBot\Entities\Message|\Longman\TelegramBot\Entities\User|\Longman\TelegramBot\Entities\UserProfilePhotos|\Longman\TelegramBot\Entities\WebhookInfo * @return \Longman\TelegramBot\Entities\Chat|\Longman\TelegramBot\Entities\ChatMember|\Longman\TelegramBot\Entities\File|\Longman\TelegramBot\Entities\Message|\Longman\TelegramBot\Entities\User|\Longman\TelegramBot\Entities\UserProfilePhotos|\Longman\TelegramBot\Entities\WebhookInfo
* @throws \Longman\TelegramBot\Exception\TelegramException * @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
private function createResultObject($result, $bot_name) private function createResultObject($result, $bot_username)
{ {
// We don't need to save the raw_data of the response object! // We don't need to save the raw_data of the response object!
$result['raw_data'] = null; $result['raw_data'] = null;
...@@ -115,19 +115,19 @@ class ServerResponse extends Entity ...@@ -115,19 +115,19 @@ class ServerResponse extends Entity
} }
//Response from sendMessage //Response from sendMessage
return new Message($result, $bot_name); return new Message($result, $bot_username);
} }
/** /**
* Create and return the objects array of the received result * Create and return the objects array of the received result
* *
* @param array $result * @param array $result
* @param string $bot_name * @param string $bot_username
* *
* @return null|\Longman\TelegramBot\Entities\ChatMember[]|\Longman\TelegramBot\Entities\Update[] * @return null|\Longman\TelegramBot\Entities\ChatMember[]|\Longman\TelegramBot\Entities\Update[]
* @throws \Longman\TelegramBot\Exception\TelegramException * @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
private function createResultObjects($result, $bot_name) private function createResultObjects($result, $bot_username)
{ {
$results = []; $results = [];
if (isset($result[0]['user'])) { if (isset($result[0]['user'])) {
...@@ -144,7 +144,7 @@ class ServerResponse extends Entity ...@@ -144,7 +144,7 @@ class ServerResponse extends Entity
// We don't need to save the raw_data of the response object! // We don't need to save the raw_data of the response object!
$update['raw_data'] = null; $update['raw_data'] = null;
$results[] = new Update($update, $bot_name); $results[] = new Update($update, $bot_username);
} }
} }
......
...@@ -315,12 +315,12 @@ class Request ...@@ -315,12 +315,12 @@ class Request
{ {
self::ensureValidAction($action); self::ensureValidAction($action);
$bot_name = self::$telegram->getBotName(); $bot_username = self::$telegram->getBotUsername();
if (defined('PHPUNIT_TESTSUITE')) { if (defined('PHPUNIT_TESTSUITE')) {
$fake_response = self::generateGeneralFakeServerResponse($data); $fake_response = self::generateGeneralFakeServerResponse($data);
return new ServerResponse($fake_response, $bot_name); return new ServerResponse($fake_response, $bot_username);
} }
self::ensureNonEmptyData($data); self::ensureNonEmptyData($data);
...@@ -333,7 +333,7 @@ class Request ...@@ -333,7 +333,7 @@ class Request
throw new TelegramException('Telegram returned an invalid response! Please review your bot name and API key.'); throw new TelegramException('Telegram returned an invalid response! Please review your bot name and API key.');
} }
return new ServerResponse($response, $bot_name); return new ServerResponse($response, $bot_username);
} }
/** /**
......
...@@ -40,11 +40,11 @@ class Telegram ...@@ -40,11 +40,11 @@ class Telegram
protected $api_key = ''; protected $api_key = '';
/** /**
* Telegram Bot name * Telegram Bot username
* *
* @var string * @var string
*/ */
protected $bot_name = ''; protected $bot_username = '';
/** /**
* Telegram Bot id * Telegram Bot id
...@@ -134,22 +134,22 @@ class Telegram ...@@ -134,22 +134,22 @@ class Telegram
* Telegram constructor. * Telegram constructor.
* *
* @param string $api_key * @param string $api_key
* @param string $bot_name * @param string $bot_username
* *
* @throws \Longman\TelegramBot\Exception\TelegramException * @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function __construct($api_key, $bot_name) public function __construct($api_key, $bot_username)
{ {
if (empty($api_key)) { if (empty($api_key)) {
throw new TelegramException('API KEY not defined!'); throw new TelegramException('API KEY not defined!');
} }
if (empty($bot_name)) { if (empty($bot_username)) {
throw new TelegramException('Bot Username not defined!'); throw new TelegramException('Bot Username not defined!');
} }
$this->api_key = $api_key; $this->api_key = $api_key;
$this->bot_name = $bot_name; $this->bot_username = $bot_username;
preg_match("/([0-9]*)\:.*/", $this->api_key, $matches); preg_match("/([0-9]*)\:.*/", $this->api_key, $matches);
$this->bot_id = $matches[1]; $this->bot_id = $matches[1];
...@@ -319,7 +319,7 @@ class Telegram ...@@ -319,7 +319,7 @@ class Telegram
'ok' => false, 'ok' => false,
'description' => 'getUpdates needs MySQL connection!', 'description' => 'getUpdates needs MySQL connection!',
], ],
$this->bot_name $this->bot_username
); );
} }
...@@ -369,7 +369,7 @@ class Telegram ...@@ -369,7 +369,7 @@ class Telegram
throw new TelegramException('Invalid JSON!'); throw new TelegramException('Invalid JSON!');
} }
if ($response = $this->processUpdate(new Update($post, $this->bot_name))) { if ($response = $this->processUpdate(new Update($post, $this->bot_username))) {
return $response->isOk(); return $response->isOk();
} }
...@@ -733,9 +733,21 @@ class Telegram ...@@ -733,9 +733,21 @@ class Telegram
* *
* @return string * @return string
*/ */
public function getBotUsername()
{
return $this->bot_username;
}
/**
* Get Bot name
* @todo: Left for backwards compatibility, remove in the future
*
* @return string
*/
public function getBotName() public function getBotName()
{ {
return $this->bot_name; TelegramLog::debug('Usage of deprecated method getBotName() detected, please use getBotUsername() instead!');
return $this->getBotUsername();
} }
/** /**
......
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