Skip to content

Commit ca48e79

Browse files
committed
Rename variables 'bot_name' and similar to 'bot_username' to prevent confusion
1 parent b2ec976 commit ca48e79

File tree

10 files changed

+56
-42
lines changed

10 files changed

+56
-42
lines changed

examples/getUpdatesCLI.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
// Add you bot's API key and name
1616
$API_KEY = 'your_bot_api_key';
17-
$BOT_NAME = 'username_bot';
17+
$BOT_USERNAME = 'username_bot';
1818

1919
// Define a path for your custom commands
2020
//$commands_path = __DIR__ . '/Commands/';
@@ -29,7 +29,7 @@
2929

3030
try {
3131
// Create Telegram API object
32-
$telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME);
32+
$telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_USERNAME);
3333

3434
// Error, Debug and Raw Update logging
3535
//Longman\TelegramBot\TelegramLog::initialize($your_external_monolog_instance);

examples/hook.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
// Add you bot's API key and name
1515
$API_KEY = 'your_bot_api_key';
16-
$BOT_NAME = 'username_bot';
16+
$BOT_USERNAME = 'username_bot';
1717

1818
// Define a path for your custom commands
1919
//$commands_path = __DIR__ . '/Commands/';
@@ -28,7 +28,7 @@
2828

2929
try {
3030
// Create Telegram API object
31-
$telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME);
31+
$telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_USERNAME);
3232

3333
// Error, Debug and Raw Update logging
3434
//Longman\TelegramBot\TelegramLog::initialize($your_external_monolog_instance);

examples/set.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
require __DIR__ . '/vendor/autoload.php';
44

55
$API_KEY = 'your_bot_api_key';
6-
$BOT_NAME = 'username_bot';
6+
$BOT_USERNAME = 'username_bot';
77
$hook_url = 'https://yourdomain/path/to/hook.php';
8+
89
try {
910
// Create Telegram API object
10-
$telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME);
11+
$telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_USERNAME);
1112

1213
// Set webhook
1314
$result = $telegram->setWebhook($hook_url);

examples/unset.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
require __DIR__ . '/vendor/autoload.php';
44

55
$API_KEY = 'your_bot_api_key';
6-
$BOT_NAME = 'username_bot';
6+
$BOT_USERNAME = 'username_bot';
7+
78
try {
89
// Create Telegram API object
9-
$telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME);
10+
$telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_USERNAME);
1011

1112
// Delete webhook
1213
$result = $telegram->deleteWebhook();

src/Entities/Entity.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ abstract class Entity
2929
/**
3030
* Entity constructor.
3131
*
32-
* @todo Get rid of the $bot_name, it shouldn't be here!
32+
* @todo Get rid of the $bot_username, it shouldn't be here!
3333
*
3434
* @param array $data
35-
* @param string $bot_name
35+
* @param string $bot_username
3636
*
3737
* @throws \Longman\TelegramBot\Exception\TelegramException
3838
*/
39-
public function __construct($data, $bot_name = '')
39+
public function __construct($data, $bot_username = '')
4040
{
4141
//Make sure we're not raw_data inception-ing
4242
if (array_key_exists('raw_data', $data)) {
@@ -47,7 +47,7 @@ public function __construct($data, $bot_name = '')
4747
$data['raw_data'] = $data;
4848
}
4949

50-
$data['bot_name'] = $bot_name;
50+
$data['bot_username'] = $bot_username;
5151
$this->assignMemberVariables($data);
5252
$this->validate();
5353
}
@@ -142,7 +142,7 @@ public function __call($method, $args)
142142
$sub_entities = $this->subEntities();
143143

144144
if (isset($sub_entities[$property_name])) {
145-
return new $sub_entities[$property_name]($property, $this->getProperty('bot_name'));
145+
return new $sub_entities[$property_name]($property, $this->getProperty('bot_username'));
146146
}
147147

148148
return $property;

src/Entities/Message.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ protected function subEntities()
7979
* Message constructor
8080
*
8181
* @param array $data
82-
* @param string $bot_name
82+
* @param string $bot_username
8383
*
8484
* @throws \Longman\TelegramBot\Exception\TelegramException
8585
*/
86-
public function __construct(array $data, $bot_name = '')
86+
public function __construct(array $data, $bot_username = '')
8787
{
8888
//Retro-compatibility
8989
if (isset($data['new_chat_participant'])) {
@@ -95,7 +95,7 @@ public function __construct(array $data, $bot_name = '')
9595
unset($data['left_chat_participant']);
9696
}
9797

98-
parent::__construct($data, $bot_name);
98+
parent::__construct($data, $bot_username);
9999
}
100100

101101
/**

src/Entities/ReplyToMessage.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ class ReplyToMessage extends Message
2121
* ReplyToMessage constructor.
2222
*
2323
* @param array $data
24-
* @param string $bot_name
24+
* @param string $bot_username
2525
*
2626
* @throws \Longman\TelegramBot\Exception\TelegramException
2727
*/
28-
public function __construct(array $data, $bot_name = '')
28+
public function __construct(array $data, $bot_username = '')
2929
{
3030
//As explained in the documentation
3131
//Reply to message can't contain other reply to message entities
3232
unset($data['reply_to_message']);
3333

34-
parent::__construct($data, $bot_name);
34+
parent::__construct($data, $bot_username);
3535
}
3636
}

src/Entities/ServerResponse.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ class ServerResponse extends Entity
2626
* ServerResponse constructor.
2727
*
2828
* @param array $data
29-
* @param string $bot_name
29+
* @param string $bot_username
3030
*
3131
* @throws \Longman\TelegramBot\Exception\TelegramException
3232
*/
33-
public function __construct(array $data, $bot_name)
33+
public function __construct(array $data, $bot_username)
3434
{
3535
// Make sure we don't double-save the raw_data
3636
unset($data['raw_data']);
@@ -41,13 +41,13 @@ public function __construct(array $data, $bot_name)
4141

4242
if ($is_ok && is_array($result)) {
4343
if ($this->isAssoc($result)) {
44-
$data['result'] = $this->createResultObject($result, $bot_name);
44+
$data['result'] = $this->createResultObject($result, $bot_username);
4545
} else {
46-
$data['result'] = $this->createResultObjects($result, $bot_name);
46+
$data['result'] = $this->createResultObjects($result, $bot_username);
4747
}
4848
}
4949

50-
parent::__construct($data, $bot_name);
50+
parent::__construct($data, $bot_username);
5151
}
5252

5353
/**
@@ -88,12 +88,12 @@ public function printError()
8888
* Create and return the object of the received result
8989
*
9090
* @param array $result
91-
* @param string $bot_name
91+
* @param string $bot_username
9292
*
9393
* @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
9494
* @throws \Longman\TelegramBot\Exception\TelegramException
9595
*/
96-
private function createResultObject($result, $bot_name)
96+
private function createResultObject($result, $bot_username)
9797
{
9898
// We don't need to save the raw_data of the response object!
9999
$result['raw_data'] = null;
@@ -115,19 +115,19 @@ private function createResultObject($result, $bot_name)
115115
}
116116

117117
//Response from sendMessage
118-
return new Message($result, $bot_name);
118+
return new Message($result, $bot_username);
119119
}
120120

121121
/**
122122
* Create and return the objects array of the received result
123123
*
124124
* @param array $result
125-
* @param string $bot_name
125+
* @param string $bot_username
126126
*
127127
* @return null|\Longman\TelegramBot\Entities\ChatMember[]|\Longman\TelegramBot\Entities\Update[]
128128
* @throws \Longman\TelegramBot\Exception\TelegramException
129129
*/
130-
private function createResultObjects($result, $bot_name)
130+
private function createResultObjects($result, $bot_username)
131131
{
132132
$results = [];
133133
if (isset($result[0]['user'])) {
@@ -144,7 +144,7 @@ private function createResultObjects($result, $bot_name)
144144
// We don't need to save the raw_data of the response object!
145145
$update['raw_data'] = null;
146146

147-
$results[] = new Update($update, $bot_name);
147+
$results[] = new Update($update, $bot_username);
148148
}
149149
}
150150

src/Request.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,12 @@ public static function send($action, array $data = [])
315315
{
316316
self::ensureValidAction($action);
317317

318-
$bot_name = self::$telegram->getBotName();
318+
$bot_username = self::$telegram->getBotUsername();
319319

320320
if (defined('PHPUNIT_TESTSUITE')) {
321321
$fake_response = self::generateGeneralFakeServerResponse($data);
322322

323-
return new ServerResponse($fake_response, $bot_name);
323+
return new ServerResponse($fake_response, $bot_username);
324324
}
325325

326326
self::ensureNonEmptyData($data);
@@ -333,7 +333,7 @@ public static function send($action, array $data = [])
333333
throw new TelegramException('Telegram returned an invalid response! Please review your bot name and API key.');
334334
}
335335

336-
return new ServerResponse($response, $bot_name);
336+
return new ServerResponse($response, $bot_username);
337337
}
338338

339339
/**

src/Telegram.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ class Telegram
4040
protected $api_key = '';
4141

4242
/**
43-
* Telegram Bot name
43+
* Telegram Bot username
4444
*
4545
* @var string
4646
*/
47-
protected $bot_name = '';
47+
protected $bot_username = '';
4848

4949
/**
5050
* Telegram Bot id
@@ -134,22 +134,22 @@ class Telegram
134134
* Telegram constructor.
135135
*
136136
* @param string $api_key
137-
* @param string $bot_name
137+
* @param string $bot_username
138138
*
139139
* @throws \Longman\TelegramBot\Exception\TelegramException
140140
*/
141-
public function __construct($api_key, $bot_name)
141+
public function __construct($api_key, $bot_username)
142142
{
143143
if (empty($api_key)) {
144144
throw new TelegramException('API KEY not defined!');
145145
}
146146

147-
if (empty($bot_name)) {
147+
if (empty($bot_username)) {
148148
throw new TelegramException('Bot Username not defined!');
149149
}
150150

151151
$this->api_key = $api_key;
152-
$this->bot_name = $bot_name;
152+
$this->bot_username = $bot_username;
153153

154154
preg_match("/([0-9]*)\:.*/", $this->api_key, $matches);
155155
$this->bot_id = $matches[1];
@@ -319,7 +319,7 @@ public function handleGetUpdates($limit = null, $timeout = null)
319319
'ok' => false,
320320
'description' => 'getUpdates needs MySQL connection!',
321321
],
322-
$this->bot_name
322+
$this->bot_username
323323
);
324324
}
325325

@@ -369,7 +369,7 @@ public function handle()
369369
throw new TelegramException('Invalid JSON!');
370370
}
371371

372-
if ($response = $this->processUpdate(new Update($post, $this->bot_name))) {
372+
if ($response = $this->processUpdate(new Update($post, $this->bot_username))) {
373373
return $response->isOk();
374374
}
375375

@@ -733,9 +733,21 @@ public function getApiKey()
733733
*
734734
* @return string
735735
*/
736+
public function getBotUsername()
737+
{
738+
return $this->bot_username;
739+
}
740+
741+
/**
742+
* Get Bot name
743+
* @todo: Left for backwards compatibility, remove in the future
744+
*
745+
* @return string
746+
*/
736747
public function getBotName()
737748
{
738-
return $this->bot_name;
749+
TelegramLog::debug('Usage of deprecated method getBotName() detected, please use getBotUsername() instead!');
750+
return $this->getBotUsername();
739751
}
740752

741753
/**

0 commit comments

Comments
 (0)