Skip to content

Commit

Permalink
Merge pull request #634 from defstudio/patch-1
Browse files Browse the repository at this point in the history
[feat] add settings to registerWebhook method
  • Loading branch information
fabio-ivona authored Aug 27, 2024
2 parents c5f0b0c + 1e666da commit 4405003
Show file tree
Hide file tree
Showing 17 changed files with 294 additions and 25 deletions.
17 changes: 14 additions & 3 deletions config/telegraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,30 @@
* If enabled, unknown webhook commands are
* reported as exception in application logs
*/
'report_unknown_commands' => true,
'report_unknown_commands' => env('TELEGRAPH_REPORT_UNKNOWN_COMMANDS', true),

/**
* secret token to be sent in a X-Telegram-Bot-Api-Secret-Token header
* to verify the authenticity of the webhook
*/
'secret' => env('TELEGRAPH_WEBHOOK_SECRET'),

/**
* maximum allowed simultaneous connections to the webhook (defaults to 40)
*/
'max_connections' => env('TELEGRAPH_WEBHOOK_MAX_CONNECTIONS', 40),

/*
* If enabled, Telegraph dumps received
* webhook messages to logs
*/
'debug' => false,
'debug' => env('TELEGRAPH_WEBHOOK_DEBUG', false),
],

/*
* Sets HTTP request timeout when interacting with Telegram servers
*/
'http_timeout' => 30,
'http_timeout' => env('TELEGRAPH_HTTP_TIMEOUT', 30),

'security' => [
/*
Expand Down
6 changes: 6 additions & 0 deletions docs/13.api/1.bots.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ register a webhook for the active bot
Telegraph::registerWebhook()->send();
```

you can use the method parameters to customize the webhook settings:

- `dropPendingUpdates`: drops pending updates from telegram
- `maxConnections`: maximum allowed simultaneous connections to the webhook (defaults to 40)
- `secretToken`: secret token to be sent in a `X-Telegram-Bot-Api-Secret-Token` header to verify the authenticity of the webhook

## `unregisterWebhook()`

unregister a webhook for the active bot
Expand Down
6 changes: 6 additions & 0 deletions docs/14.models/1.telegraph-bot.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ register a webhook url
$telegraphBot->registerWebhook()->send();
```

you can use the method parameters to customize the webhook settings:

- `dropPendingUpdates`: drops pending updates from telegram
- `maxConnections`: maximum allowed simultaneous connections to the webhook (defaults to 40)
- `secretToken`: secret token to be sent in a `X-Telegram-Bot-Api-Secret-Token` header to verify the authenticity of the webhook

### `unregisterWebhook()`

unregister a webhook url
Expand Down
8 changes: 8 additions & 0 deletions docs/15.webhooks/2.registering-webhooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ You can register a webhook calling the `telegraph:set-webhook` artisan command:
php artisan telegraph:set-webhook
```

some options are available in order to customize the webhook settings, refer to [bots documentation](models/telegraph-bot#registerWebhook) for more info:

```shell
php artisan telegraph:set-webhook --drop-pending-updates --max-connections=100 --secret=super_secret_token
```

### programmatically

if you are implementing a custom bot management logic, you can register a webhok using the `TelegraphBot` model:
Expand All @@ -23,6 +29,8 @@ if you are implementing a custom bot management logic, you can register a webhok
$telegraphBot->registerWebhook()->send();
```

some options are available in order to customize the webhook settings, refer to [bots documentation](models/telegraph-bot#registerWebhook) for more info:

> [!WARNING]
> Manual updates polling is not available if a webhook is set up for the bot. Webhook should be remove first using its [unregisterWebhook](webhooks/deleting-webhooks) method
Expand Down
15 changes: 12 additions & 3 deletions src/Commands/CreateNewBotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@

use DefStudio\Telegraph\Models\TelegraphBot;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;

class CreateNewBotCommand extends Command
{
public $signature = 'telegraph:new-bot';
public $signature = 'telegraph:new-bot
{--drop-pending-updates : drops pending updates from telegram}
{--max-connections=40 : maximum allowed simultaneous connections to the webhook (defaults to 40)}
{--secret= : secret token to be sent in a X-Telegram-Bot-Api-Secret-Token header to verify the authenticity of the webhook}
';

public $description = 'Create a new TelegraphBot';

Expand Down Expand Up @@ -48,9 +53,13 @@ public function handle(): int
}



if ($this->confirm(__('telegraph::commands.new_bot.ask_to_setup_webhook'))) {
$bot->registerWebhook()->send();
Artisan::call('telegraph:set-webhook', [
'bot' => $bot->id,
'--drop-pending-updates' => $this->option('drop-pending-updates'),
'--max-connections' => $this->option('max-connections'),
'--secret' => $this->option('secret'),
]);
}

$this->info(__('telegraph::commands.new_bot.bot_created', ['bot_name' => $bot->name]));
Expand Down
15 changes: 12 additions & 3 deletions src/Commands/SetTelegramWebhookCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
class SetTelegramWebhookCommand extends Command
{
public $signature = 'telegraph:set-webhook
{bot? : the ID of the bot (if the system contain a single bot, it can be left empty)}';
{bot? : the ID of the bot (if the system contain a single bot, it can be left empty)}
{--drop-pending-updates : drops pending updates from telegram}
{--max-connections=40 : maximum allowed simultaneous connections to the webhook (defaults to 40)}
{--secret= : secret token to be sent in a X-Telegram-Bot-Api-Secret-Token header to verify the authenticity of the webhook}
';

public $description = 'Set webhook url in telegram bot configuration';

Expand All @@ -29,13 +33,18 @@ public function handle(): int
return self::FAILURE;
}

$telegraph = $bot->registerWebhook();
$dropPendingUpdates = $this->option('drop-pending-updates');
$maxConnections = $this->option('max-connections');
$secret = $this->option('secret');

/* @phpstan-ignore-next-line */
$telegraph = $bot->registerWebhook($dropPendingUpdates, $maxConnections, $secret);

$this->info(__('telegraph::commands.set_webhook.sending_setup_request', ['api_url' => $telegraph->getApiUrl()]));

$reponse = $telegraph->send();

$ok = (bool)$reponse->json('ok');
$ok = (bool) $reponse->json('ok');

if (!$ok) {
$this->error(__('telegraph::errors.failed_to_register_webhook'));
Expand Down
12 changes: 8 additions & 4 deletions src/Concerns/InteractsWithWebhooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,21 @@ private function getWebhookUrl(): string
return $url;
}

return $customWebhookUrl . route('telegraph.webhook', $this->getBot(), false);
return $customWebhookUrl.route('telegraph.webhook', $this->getBot(), false);
}

public function registerWebhook(): Telegraph
public function registerWebhook(bool $dropPendingUpdates = null, int $maxConnections = null, string $secretToken = null): Telegraph
{
$telegraph = clone $this;

$telegraph->endpoint = self::ENDPOINT_SET_WEBHOOK;
$telegraph->data = [
$telegraph->data = collect([
'url' => $this->getWebhookUrl(),
];
'drop_pending_updates' => $dropPendingUpdates,
'max_connections' => $maxConnections ?? config('telegraph.max_connections'),
'secret_token' => $secretToken ?? config('telegraph.secret_token'),
])->filter()
->toArray();

return $telegraph;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Facades/Telegraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@
* @method static void assertSentFiles(string $endpoint, array $files = null)
* @method static void assertSent(string $message, bool $exact = true)
* @method static void assertNothingSent()
* @method static void assertRegisteredWebhook()
* @method static void assertUnregisteredWebhook()
* @method static void assertRequestedWebhookDebugInfo()
* @method static void assertRegisteredWebhook(array $data = null, bool $exact = true)
* @method static void assertUnregisteredWebhook(array $data = null, bool $exact = true)
* @method static void assertRequestedWebhookDebugInfo(array $data = null, bool $exact = true)
* @method static void assertRepliedWebhook(string $message)
* @method static void assertRepliedWebhookIsAlert()
* @method static void assertStoredFile(string $fileId)
Expand Down
4 changes: 2 additions & 2 deletions src/Models/TelegraphBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ public function chats(): HasMany
return $this->hasMany(config('telegraph.models.chat'), 'telegraph_bot_id');
}

public function registerWebhook(): Telegraph
public function registerWebhook(bool $dropPendingUpdates = null, int $maxConnections = null, string $secretToken = null): Telegraph
{
return TelegraphFacade::bot($this)->registerWebhook();
return TelegraphFacade::bot($this)->registerWebhook($dropPendingUpdates, $maxConnections, $secretToken);
}

public function unregisterWebhook(bool $dropPendingUpdates = false): Telegraph
Expand Down
14 changes: 7 additions & 7 deletions src/Support/Testing/Fakes/TelegraphFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class TelegraphFake extends Telegraph
use FakesRequests;

/**
* @param array<string, array<mixed>> $replies
* @param array<string, array> $replies
*/
public function __construct(array $replies = [])
{
Expand Down Expand Up @@ -135,19 +135,19 @@ public function assertNothingSent(): void
Assert::assertEmpty(self::$sentMessages, sprintf("Failed to assert that no request were sent (sent %d requests so far)", count(self::$sentMessages)));
}

public function assertRegisteredWebhook(): void
public function assertRegisteredWebhook(array $data = null, bool $exact = true): void
{
$this->assertSentData(Telegraph::ENDPOINT_SET_WEBHOOK);
$this->assertSentData(Telegraph::ENDPOINT_SET_WEBHOOK, $data, $exact);
}

public function assertUnregisteredWebhook(): void
public function assertUnregisteredWebhook(array $data = null, bool $exact = true): void
{
$this->assertSentData(Telegraph::ENDPOINT_UNSET_WEBHOOK);
$this->assertSentData(Telegraph::ENDPOINT_UNSET_WEBHOOK, $data, $exact);
}

public function assertRequestedWebhookDebugInfo(): void
public function assertRequestedWebhookDebugInfo(array $data = null, bool $exact = true): void
{
$this->assertSentData(Telegraph::ENDPOINT_GET_WEBHOOK_DEBUG_INFO);
$this->assertSentData(Telegraph::ENDPOINT_GET_WEBHOOK_DEBUG_INFO, $data, $exact);
}

public function assertRepliedWebhook(string $message): void
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"url": "https:\/\/api.telegram.org\/bot3f3814e1-5836-3d77-904e-60f64b15df36\/setWebhook",
"payload": {
"url": "https:\/\/testbot.defstudio.dev\/telegraph\/3f3814e1-5836-3d77-904e-60f64b15df36\/webhook",
"drop_pending_updates": true
},
"files": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"url": "https:\/\/api.telegram.org\/bot3f3814e1-5836-3d77-904e-60f64b15df36\/setWebhook",
"payload": {
"url": "https:\/\/testbot.defstudio.dev\/telegraph\/3f3814e1-5836-3d77-904e-60f64b15df36\/webhook",
"max_connections": 42
},
"files": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"url": "https:\/\/api.telegram.org\/bot3f3814e1-5836-3d77-904e-60f64b15df36\/setWebhook",
"payload": {
"url": "https:\/\/testbot.defstudio.dev\/telegraph\/3f3814e1-5836-3d77-904e-60f64b15df36\/webhook",
"secret_token": "super-secret-token"
},
"files": []
}
84 changes: 84 additions & 0 deletions tests/Feature/Commands/CreateNewBotCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,87 @@

Facade::assertRegisteredWebhook();
});

it('can register the new bot webhook dropping pending updates', function () {
withfakeUrl();

Facade::fake([
Telegraph::ENDPOINT_SET_WEBHOOK => [
'ok' => true,
],
]);

artisan('telegraph:new-bot', ['--drop-pending-updates' => true])
->expectsOutput('You are about to create a new Telegram Bot')
->expectsQuestion("Please, enter the bot token", "123456789")
->expectsQuestion("Enter the bot name (optional)", "")
->expectsQuestion("Do you want to add a chat to this bot?", false)
->expectsQuestion("Do you want to setup a webhook for this bot?", true)
->assertExitCode(Command::SUCCESS);


expect(TelegraphBot::first())
->not->toBeNull()
->token->toBe('123456789')
->name->toBe('Bot #1');

Facade::assertRegisteredWebhook([
'drop_pending_updates' => true,
], false);
});

it('can register the new bot webhook settings its max connections', function () {
withfakeUrl();

Facade::fake([
Telegraph::ENDPOINT_SET_WEBHOOK => [
'ok' => true,
],
]);

artisan('telegraph:new-bot', ['--max-connections' => 99])
->expectsOutput('You are about to create a new Telegram Bot')
->expectsQuestion("Please, enter the bot token", "123456789")
->expectsQuestion("Enter the bot name (optional)", "")
->expectsQuestion("Do you want to add a chat to this bot?", false)
->expectsQuestion("Do you want to setup a webhook for this bot?", true)
->assertExitCode(Command::SUCCESS);


expect(TelegraphBot::first())
->not->toBeNull()
->token->toBe('123456789')
->name->toBe('Bot #1');

Facade::assertRegisteredWebhook([
'max_connections' => 99,
], false);
});

it('can register the new bot webhook settings its secret token', function () {
withfakeUrl();

Facade::fake([
Telegraph::ENDPOINT_SET_WEBHOOK => [
'ok' => true,
],
]);

artisan('telegraph:new-bot', ['--secret' => 'foo'])
->expectsOutput('You are about to create a new Telegram Bot')
->expectsQuestion("Please, enter the bot token", "123456789")
->expectsQuestion("Enter the bot name (optional)", "")
->expectsQuestion("Do you want to add a chat to this bot?", false)
->expectsQuestion("Do you want to setup a webhook for this bot?", true)
->assertExitCode(Command::SUCCESS);


expect(TelegraphBot::first())
->not->toBeNull()
->token->toBe('123456789')
->name->toBe('Bot #1');

Facade::assertRegisteredWebhook([
'secret_token' => 'foo',
], false);
});
Loading

0 comments on commit 4405003

Please sign in to comment.