Skip to content

Commit

Permalink
Merge pull request #7 from noplanman/new_webhook_params
Browse files Browse the repository at this point in the history
Update to include latest changes from PHP Telegram API.
  • Loading branch information
noplanman authored Dec 25, 2016
2 parents 0312722 + 394893d commit ff31519
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 52 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,12 @@ Parameter | Description
--------- |------------
webhook | URL to the manager PHP file used for setting up the Webhook.
| *e.g.* `'https://example.com/manager.php'`
selfcrt | Path to a self-signed certificate (if necessary).
certificate | Path to a self-signed certificate (if necessary).
| *e.g.* `__DIR__ . '/server.crt'`
max_connections | Maximum allowed simultaneous HTTPS connections to the webhook
| *e.g.* `20`
allowed_updates | List the types of updates you want your bot to receive
| *e.g.* `['message', 'edited_channel_post', 'callback_query']`
logging | Path(s) where to the log files should be put. This is an array that can contain all 3 log file paths (`error`, `debug` and `update`).
| *e.g.* `['error' => __DIR__ . '/php-telegram-bot-error.log']`
admins | An array of user ids that have admin access to your bot.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
],
"require": {
"php": "^7.0",
"longman/telegram-bot": "^0.36"
"longman/telegram-bot": "^0.38"
},
"require-dev": {
"phpunit/phpunit": "^5.5",
Expand Down
76 changes: 38 additions & 38 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions src/BotManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,21 +172,27 @@ public function validateSecret(bool $force = false): self
public function validateAndSetWebhook(): self
{
$webhook = $this->params->getBotParam('webhook');
$selfcrt = $this->params->getBotParam('selfcrt');
if (empty($webhook) && $this->action->isAction(['set', 'reset'])) {
throw new \InvalidArgumentException('Invalid webhook');
}

if ($this->action->isAction(['unset', 'reset'])) {
$this->handleOutput($this->telegram->unsetWebHook()->getDescription() . PHP_EOL);
$this->handleOutput($this->telegram->deleteWebhook()->getDescription() . PHP_EOL);
// When resetting the webhook, sleep for a bit to prevent too many requests.
$this->action->isAction('reset') && sleep(1);
}

if ($this->action->isAction(['set', 'reset'])) {
$webhook_params = array_filter([
'certificate' => $this->params->getBotParam('certificate'),
'max_connections' => $this->params->getBotParam('max_connections'),
'allowed_updates' => $this->params->getBotParam('allowed_updates'),
]);

$this->handleOutput(
$this->telegram->setWebHook(
$this->telegram->setWebhook(
$webhook . '?a=handle&s=' . $this->params->getBotParam('secret'),
$selfcrt
$webhook_params
)->getDescription() . PHP_EOL
);
}
Expand Down
8 changes: 6 additions & 2 deletions src/Params.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ class Params
*/
private static $valid_extra_bot_params = [
'webhook',
'selfcrt',
'certificate',
'max_connections',
'allowed_updates',
'logging',
'admins',
'mysql',
Expand Down Expand Up @@ -65,7 +67,9 @@ class Params
* botname (string) Telegram Bot name
* secret (string) Secret string to validate calls
* webhook (string) URI of the webhook
* selfcrt (string) Path to the self-signed certificate
* certificate (string) Path to the self-signed certificate
* max_connections (int) Maximum allowed simultaneous HTTPS connections to the webhook
* allowed_updates (array) List the types of updates you want your bot to receive
* logging (array) Array of logger files to set.
* admins (array) List of admins to enable.
* mysql (array) MySQL credentials to use.
Expand Down
10 changes: 5 additions & 5 deletions tests/TelegramBotManager/Tests/BotManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,19 @@ public function testValidateAndSetWebhookSuccess()
'telegram',
$this->getMockBuilder(Telegram::class)
->disableOriginalConstructor()
->setMethods(['setWebHook', 'unsetWebHook', 'getDescription'])
->setMethods(['setWebhook', 'deleteWebhook', 'getDescription'])
->getMock()
);

$telegram = $botManager->getTelegram();

/** @var \PHPUnit_Framework_MockObject_MockObject $telegram */
$telegram->expects(static::any())
->method('setWebHook')
->method('setWebhook')
->with('https://web/hook.php?a=handle&s=secret_12345')
->will(static::returnSelf());
$telegram->expects(static::any())
->method('unsetWebHook')
->method('deleteWebhook')
->will(static::returnSelf());
$telegram->expects(static::any())
->method('getDescription')
Expand Down Expand Up @@ -229,7 +229,7 @@ public function testValidateAndSetWebhookSuccessLiveBot()
/**
* @group live
*/
public function testUnsetWebhookViaRunLiveBot()
public function testDeleteWebhookViaRunLiveBot()
{
$_GET = ['a' => 'unset'];
$botManager = new BotManager(array_merge(self::$live_params, [
Expand Down Expand Up @@ -386,7 +386,7 @@ public function testGetUpdatesLiveBot()
public function testGetUpdatesLoopLiveBot()
{
// Webhook must NOT be set for this to work!
$this->testUnsetWebhookViaRunLiveBot();
$this->testDeleteWebhookViaRunLiveBot();

// Looping for 5 seconds should be enough to get a result.
$_GET = ['l' => 5];
Expand Down
4 changes: 3 additions & 1 deletion tests/TelegramBotManager/Tests/ParamsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class ParamsTest extends \PHPUnit_Framework_TestCase
*/
public static $demo_extra_params = [
'webhook' => 'https://php.telegram.bot/manager.php',
'selfcrt' => __DIR__ . '/server.crt',
'certificate' => __DIR__ . '/server.crt',
'max_connections' => 20,
'allowed_updates' => ['message', 'edited_channel_post', 'callback_query'],
'admins' => [1],
'mysql' => [
'host' => '127.0.0.1',
Expand Down

0 comments on commit ff31519

Please sign in to comment.