Skip to content

Commit

Permalink
Rename botname to bot_username, like in core 0.42.0.
Browse files Browse the repository at this point in the history
Fix coding style.
Add changelog.
  • Loading branch information
noplanman committed Apr 10, 2017
1 parent 3fa47d8 commit f0afb26
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 55 deletions.
50 changes: 50 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Changelog
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- Changelog.
### Changed
- (!) Rename vital parameter `botname` to `bot_username` everywhere.
### Fixed
- Some code style issues.

## [0.42.0] - 2017-04-10
### Changed
- Move to PHP Telegram Bot organisation.
- Mirror version with core library.
- Update repository links.
### Fixed
- Readme formatting.

## [0.4.0] - 2017-02-26
### Added
- Latest Telegram Bot limiter functionality.
### Fixed
- Travis tests, using MariaDB instead of MySQL.

## [0.3.1] - 2017-01-04
### Fixed
- Make CLI usable again after setting up Telegram API IP address limitations.

## [0.3.0] - 2016-12-25
### Added
- Latest changes from PHP Telegram API bot.
### Security
- Request validation to secure the script to allow only Telegram API IPs of executing the webhook handle.

## [0.2.1] - 2016-10-16
### Added
- Interval between updates can be set via parameter.

## [0.2] - 2016-09-16
### Changed
- Force PHP7.

## [0.1.1] - 2016-08-20
### Fixed
- Tiny conditional fix to correct the output.

## [0.1] - 2016-08-20
### Added
- First minor version that contains the basic functionality.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ require __DIR__ . '/vendor/autoload.php';
try {
$bot = new BotManager([
// Vitals!
'api_key' => '12345:my_api_key',
'botname' => 'my_own_bot',
'secret' => 'super_secret',
'api_key' => '12345:my_api_key',
'bot_username' => 'my_own_bot',
'secret' => 'super_secret',

// Extras.
'webhook' => 'https://example.com/manager.php',
'webhook' => 'https://example.com/manager.php',
]);
$bot->run();
} catch (\Exception $e) {
Expand All @@ -131,7 +131,7 @@ try {

The vital parameters are:
- Bot API key
- Bot name
- Bot username
- A secret

What secret you ask? Well, this is a user-defined key that is required to execute any of the library features.
Expand Down
4 changes: 2 additions & 2 deletions src/Action.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php declare(strict_types=1);
/**
* This file is part of the TelegramBotManager package.
*
Expand Down Expand Up @@ -52,7 +52,7 @@ public function __construct($action = 'handle')
*/
public function isAction($actions): bool
{
return in_array($this->action, (array)$actions, true);
return in_array($this->action, (array) $actions, true);
}

/**
Expand Down
17 changes: 9 additions & 8 deletions src/BotManager.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php declare(strict_types=1);
/**
* This file is part of the TelegramBotManager package.
*
Expand Down Expand Up @@ -60,6 +60,7 @@ class BotManager
* @param array $params
*
* @throws \InvalidArgumentException
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function __construct(array $params)
{
Expand All @@ -69,7 +70,7 @@ public function __construct(array $params)
// Set up a new Telegram instance.
$this->telegram = new Telegram(
$this->params->getBotParam('api_key'),
$this->params->getBotParam('botname')
$this->params->getBotParam('bot_username')
);
}

Expand Down Expand Up @@ -324,7 +325,7 @@ public function getLoopTime(): int
return 604800; // Default to 7 days.
}

return max(0, (int)$loop_time);
return max(0, (int) $loop_time);
}

/**
Expand All @@ -341,7 +342,7 @@ public function getLoopInterval(): int
}

// Minimum interval is 1 second.
return max(1, (int)$interval_time);
return max(1, (int) $interval_time);
}

/**
Expand Down Expand Up @@ -382,7 +383,7 @@ public function handleGetUpdates(): self

$response = $this->telegram->handleGetUpdates();
if ($response->isOk()) {
$results = array_filter((array)$response->getResult());
$results = array_filter((array) $response->getResult());

$output .= sprintf('Updates processed: %d' . PHP_EOL, count($results));

Expand Down Expand Up @@ -468,9 +469,9 @@ public function isValidRequest(): bool
}
}

$lower_dec = (float)sprintf('%u', ip2long(self::TELEGRAM_IP_LOWER));
$upper_dec = (float)sprintf('%u', ip2long(self::TELEGRAM_IP_UPPER));
$ip_dec = (float)sprintf('%u', ip2long($ip));
$lower_dec = (float) sprintf('%u', ip2long(self::TELEGRAM_IP_LOWER));
$upper_dec = (float) sprintf('%u', ip2long(self::TELEGRAM_IP_UPPER));
$ip_dec = (float) sprintf('%u', ip2long($ip));

return $ip_dec >= $lower_dec && $ip_dec <= $upper_dec;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Params.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php declare(strict_types=1);
/**
* This file is part of the TelegramBotManager package.
*
Expand Down Expand Up @@ -27,7 +27,7 @@ class Params
*/
private static $valid_vital_bot_params = [
'api_key',
'botname',
'bot_username',
'secret',
];

Expand Down Expand Up @@ -68,7 +68,7 @@ class Params
* Params constructor.
*
* api_key (string) Telegram Bot API key
* botname (string) Telegram Bot name
* bot_username (string) Telegram Bot username
* secret (string) Secret string to validate calls
* validate_request (bool) Only allow webhook access from valid Telegram API IPs
* webhook (string) URI of the webhook
Expand Down
2 changes: 1 addition & 1 deletion tests/TelegramBotManager/Tests/ActionTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php declare(strict_types=1);
/**
* This file is part of the TelegramBotManager package.
*
Expand Down
48 changes: 24 additions & 24 deletions tests/TelegramBotManager/Tests/BotManagerTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php declare(strict_types=1);
/**
* This file is part of the TelegramBotManager package.
*
Expand Down Expand Up @@ -32,10 +32,10 @@ class BotManagerTest extends \PHPUnit\Framework\TestCase
public static function setUpBeforeClass()
{
self::$live_params = [
'api_key' => getenv('API_KEY'),
'botname' => getenv('BOTNAME'),
'secret' => 'super-secret',
'mysql' => [
'api_key' => getenv('API_KEY'),
'bot_username' => getenv('BOT_USERNAME'),
'secret' => 'super-secret',
'mysql' => [
'host' => PHPUNIT_DB_HOST,
'database' => PHPUNIT_DB_NAME,
'user' => PHPUNIT_DB_USER,
Expand Down Expand Up @@ -77,7 +77,7 @@ public function testNoVitalsFail()
*/
public function testSomeVitalsFail()
{
new BotManager(['api_key' => '12345:api_key', 'botname' => 'testbot']);
new BotManager(['api_key' => '12345:api_key', 'bot_username' => 'testbot']);
}

public function testVitalsSuccess()
Expand All @@ -92,7 +92,7 @@ public function testValidTelegramObject()
$telegram = $bot->getTelegram();

self::assertInstanceOf(Telegram::class, $telegram);
self::assertSame(ParamsTest::$demo_vital_params['botname'], $telegram->getBotName());
self::assertSame(ParamsTest::$demo_vital_params['bot_username'], $telegram->getBotUsername());
self::assertSame(ParamsTest::$demo_vital_params['api_key'], $telegram->getApiKey());
}

Expand Down Expand Up @@ -158,31 +158,31 @@ public function testValidateAndSetWebhookSuccess()
$botManager,
'telegram',
$this->getMockBuilder(Telegram::class)
->disableOriginalConstructor()
->setMethods(['setWebhook', 'deleteWebhook', 'getDescription'])
->getMock()
->disableOriginalConstructor()
->setMethods(['setWebhook', 'deleteWebhook', 'getDescription'])
->getMock()
);

$telegram = $botManager->getTelegram();

/** @var \PHPUnit_Framework_MockObject_MockObject $telegram */
$telegram->expects(static::any())
->method('setWebhook')
->with('https://web/hook.php?a=handle&s=secret_12345')
->will(static::returnSelf());
->method('setWebhook')
->with('https://web/hook.php?a=handle&s=secret_12345')
->will(static::returnSelf());
$telegram->expects(static::any())
->method('deleteWebhook')
->will(static::returnSelf());
->method('deleteWebhook')
->will(static::returnSelf());
$telegram->expects(static::any())
->method('getDescription')
->will(static::onConsecutiveCalls(
'Webhook was set', // set
'Webhook is already set',
'Webhook was deleted', // reset
'Webhook was set',
'Webhook was deleted', //unset
'Webhook is already deleted'
));
->method('getDescription')
->will(static::onConsecutiveCalls(
'Webhook was set', // set
'Webhook is already set',
'Webhook was deleted', // reset
'Webhook was set',
'Webhook was deleted', //unset
'Webhook is already deleted'
));

TestHelpers::setObjectProperty($botManager->getAction(), 'action', 'set');
$output = $botManager->validateAndSetWebhook()->getOutput();
Expand Down
20 changes: 10 additions & 10 deletions tests/TelegramBotManager/Tests/ParamsTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php declare(strict_types=1);
/**
* This file is part of the TelegramBotManager package.
*
Expand All @@ -18,9 +18,9 @@ class ParamsTest extends \PHPUnit\Framework\TestCase
* @var array Demo vital parameters.
*/
public static $demo_vital_params = [
'api_key' => '12345:api_key',
'botname' => 'test_bot',
'secret' => 'secret_12345',
'api_key' => '12345:api_key',
'bot_username' => 'test_bot',
'secret' => 'secret_12345',
];

/**
Expand Down Expand Up @@ -70,16 +70,16 @@ public function testConstruct()
public function testConstructWithoutApiKey()
{
new Params([
'botname' => 'test_bot',
'secret' => 'secret_12345',
'bot_username' => 'test_bot',
'secret' => 'secret_12345',
]);
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Some vital info is missing: botname
* @expectedExceptionMessage Some vital info is missing: bot_username
*/
public function testConstructWithoutBotname()
public function testConstructWithoutBotUsername()
{
new Params([
'api_key' => '12345:api_key',
Expand All @@ -94,8 +94,8 @@ public function testConstructWithoutBotname()
public function testConstructWithoutSecret()
{
new Params([
'api_key' => '12345:api_key',
'botname' => 'test_bot',
'api_key' => '12345:api_key',
'bot_username' => 'test_bot',
]);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/TelegramBotManager/Tests/TestHelpers.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php declare(strict_types=1);
/**
* This file is part of the TelegramBotManager package.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php declare(strict_types=1);
/**
* This file is part of the TelegramBotManager package.
*
Expand Down

0 comments on commit f0afb26

Please sign in to comment.