Skip to content

Commit 3228a65

Browse files
authored
Merge pull request #457 from jacklul/misc
Misc additions/changes
2 parents 036bac2 + b3a7d3e commit 3228a65

15 files changed

+127
-61
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: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use Exception;
1414
use Longman\TelegramBot\Entities\InlineQuery\InlineEntity;
15+
use Longman\TelegramBot\TelegramLog;
1516
use ReflectionObject;
1617

1718
/**
@@ -21,22 +22,22 @@
2122
*
2223
* @link https://core.telegram.org/bots/api#available-types
2324
*
24-
* @method array getRawData() Get the raw data passed to this entity
25-
* @method string getBotName() Return the bot name passed to this entity
25+
* @method array getRawData() Get the raw data passed to this entity
26+
* @method string getBotUsername() Return the bot name passed to this entity
2627
*/
2728
abstract class Entity
2829
{
2930
/**
3031
* Entity constructor.
3132
*
32-
* @todo Get rid of the $bot_name, it shouldn't be here!
33+
* @todo Get rid of the $bot_username, it shouldn't be here!
3334
*
3435
* @param array $data
35-
* @param string $bot_name
36+
* @param string $bot_username
3637
*
3738
* @throws \Longman\TelegramBot\Exception\TelegramException
3839
*/
39-
public function __construct($data, $bot_name = '')
40+
public function __construct($data, $bot_username = '')
4041
{
4142
//Make sure we're not raw_data inception-ing
4243
if (array_key_exists('raw_data', $data)) {
@@ -47,7 +48,7 @@ public function __construct($data, $bot_name = '')
4748
$data['raw_data'] = $data;
4849
}
4950

50-
$data['bot_name'] = $bot_name;
51+
$data['bot_username'] = $bot_username;
5152
$this->assignMemberVariables($data);
5253
$this->validate();
5354
}
@@ -142,7 +143,7 @@ public function __call($method, $args)
142143
$sub_entities = $this->subEntities();
143144

144145
if (isset($sub_entities[$property_name])) {
145-
return new $sub_entities[$property_name]($property, $this->getProperty('bot_name'));
146+
return new $sub_entities[$property_name]($property, $this->getProperty('bot_username'));
146147
}
147148

148149
return $property;
@@ -241,4 +242,17 @@ public function tryMention($escape_markdown = false)
241242

242243
return ($is_username ? '@' : '') . $name;
243244
}
245+
246+
/**
247+
* Get Bot name
248+
*
249+
* @todo: Left for backwards compatibility, remove in the future
250+
*
251+
* @return string
252+
*/
253+
public function getBotName()
254+
{
255+
TelegramLog::debug('Usage of deprecated method getBotName() detected, please use getBotUsername() instead!');
256+
return $this->getBotUsername();
257+
}
244258
}

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
/**

0 commit comments

Comments
 (0)