Skip to content

Commit f5c7ddc

Browse files
committed
WIP
- Bump Rector version. - Migrate PHPUnit XML. - Revise webhook path in config docs. - Refactor webhook system to use `secret_token` headers. - Drop bot token from webhook url. - Add facade single test
1 parent 36694da commit f5c7ddc

File tree

11 files changed

+48
-69
lines changed

11 files changed

+48
-69
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"irazasyed/docgen": "^0.2",
3535
"pestphp/pest": "^2.0",
3636
"php-parallel-lint/php-parallel-lint": "^1.3",
37-
"rector/rector": "^0.15.24"
37+
"rector/rector": "^0.16"
3838
},
3939
"suggest": {
4040
"irazasyed/larasupport": "Allows you to use any Laravel Package in Lumem"

config/telegram.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
* Controller: Responsible to listen to updates and acknowledge to Telegram.
8787
*
8888
* Default path: telegram
89-
* Webhook path: /telegram/{token}/{bot}
89+
* Webhook path: /telegram/{bot}/webhook
9090
*/
9191
'webhook' => [
9292
'domain' => env('TELEGRAM_WEBHOOK_DOMAIN', null),

phpunit.xml.dist

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" executionOrder="random" failOnWarning="true" failOnRisky="true" failOnEmptyTestSuite="true" beStrictAboutOutputDuringTests="true" cacheDirectory=".phpunit.cache" backupStaticProperties="false" requireCoverageMetadata="false" beStrictAboutCoverageMetadata="false">
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" executionOrder="random" failOnWarning="true" failOnRisky="true" failOnEmptyTestSuite="true" beStrictAboutOutputDuringTests="true" cacheDirectory=".phpunit.cache" backupStaticProperties="false" requireCoverageMetadata="false" beStrictAboutCoverageMetadata="false">
33
<testsuites>
44
<testsuite name="Telegram Bot SDK Laravel Test Suite">
55
<directory suffix=".php">tests</directory>
66
</testsuite>
77
</testsuites>
8-
<coverage>
8+
<coverage/>
9+
<source>
910
<include>
1011
<directory suffix=".php">src</directory>
1112
</include>
12-
</coverage>
13+
</source>
1314
</phpunit>

routes/telegram.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
Route::group(['middleware' => ValidateWebhook::class], function (): void {
88

9-
Route::post('/{token}/{bot}', config('telegram.webhook.controller'))->name('telegram.bot.webhook');
9+
Route::post('/{bot}/webhook', config('telegram.webhook.controller'))->name('telegram.bot.webhook');
1010

1111
// # Longpolling method (manual).
1212
// Route::get('/{token}/updates/{bot?}', function ($bot = 'default') {

src/Console/Webhook/WebhookSetupCommand.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Illuminate\Support\Str;
66
use Telegram\Bot\Bot;
7+
use Telegram\Bot\Helpers\Util;
8+
use Telegram\Bot\Laravel\Facades\Telegram;
79
use Telegram\Bot\Exceptions\TelegramSDKException;
810
use Telegram\Bot\Laravel\Console\ConsoleBaseCommand;
911
use Throwable;
@@ -49,13 +51,19 @@ protected function setupWebhook(Bot $bot): void
4951
// Bot webhook config.
5052
$config = $bot->config('webhook', []);
5153

54+
$secretToken = env('TELEGRAM_WEBHOOK_SECRET_TOKEN', $bot->config('token', ''));
55+
5256
// Global webhook config merged with bot config with the latter taking precedence.
53-
$params = collect($bot->config('global.webhook'))->except(['domain', 'path', 'controller', 'url'])
57+
$params = collect($bot->config('global.webhook'))
58+
->except(['domain', 'path', 'controller', 'url'])
5459
->merge($config)
55-
->put('url', $this->webhookUrl($bot))
56-
->all();
60+
->put('url', $this->webhookUrl($bot));
61+
62+
if(filled($secretToken)) {
63+
$params->put('secret_token', Util::secretToken($secretToken));
64+
}
5765

58-
if ($bot->setWebhook($params)) {
66+
if ($bot->setWebhook($params->all())) {
5967
$this->info('Success: Your webhook has been set!');
6068

6169
return;
@@ -71,7 +79,6 @@ protected function webhookUrl(Bot $bot): string
7179
}
7280

7381
return Str::replaceFirst('http:', 'https:', route('telegram.bot.webhook', [
74-
'token' => $bot->config('token'),
7582
'bot' => $bot->config('bot'),
7683
]));
7784
}

src/Http/Controllers/WebhookController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class WebhookController extends Controller
2323
*
2424
* @throws WebhookException|TelegramSDKException
2525
*/
26-
public function __invoke(BotManager $manager, string $token, string $bot)
26+
public function __invoke(BotManager $manager, string $bot)
2727
{
2828
App::terminating(static function () use ($manager, $bot): void {
2929
try {

src/Http/Middleware/ValidateWebhook.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Closure;
66
use Illuminate\Http\Request;
7+
use Telegram\Bot\Helpers\Util;
78
use Telegram\Bot\Exceptions\TelegramSDKException;
89
use Telegram\Bot\Laravel\Facades\Telegram;
910

@@ -19,7 +20,7 @@ class ValidateWebhook
1920
*/
2021
public function handle($request, Closure $next)
2122
{
22-
abort_unless($this->isTokenValid($request), 403);
23+
abort_unless($this->isSecretTokenValid($request), 403);
2324

2425
return $next($request);
2526
}
@@ -31,8 +32,11 @@ public function handle($request, Closure $next)
3132
*
3233
* @throws TelegramSDKException
3334
*/
34-
public function isTokenValid($request): bool
35+
public function isSecretTokenValid($request): bool
3536
{
36-
return Telegram::bot($request->route('bot'))->config('token') === $request->route('token');
37+
return Util::isSecretTokenValid(
38+
env('TELEGRAM_WEBHOOK_SECRET_TOKEN', Telegram::bot($request->route('bot'))->config('token', '')),
39+
$request->header('X-Telegram-Bot-Api-Secret-Token', '')
40+
);
3741
}
3842
}

tests/Facades/Telegram.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use Telegram\Bot\Objects\ResponseObject;
4+
use Telegram\Bot\Laravel\Facades\Telegram;
5+
6+
test('fake returns the given response', function () {
7+
Telegram::fake([
8+
ResponseObject::make([
9+
'id' => 123456789,
10+
'first_name' => 'Test',
11+
'username' => 'testbot',
12+
])
13+
]);
14+
15+
$response = Telegram::sendMessage([
16+
'chat_id' => 987654321,
17+
'text' => 'Hello World',
18+
]);
19+
20+
expect($response['id'])->toBe(123456789);
21+
});

tests/Feature/Example.php

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/Pest.php

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1 @@
11
<?php
2-
3-
/*
4-
|--------------------------------------------------------------------------
5-
| Test Case
6-
|--------------------------------------------------------------------------
7-
|
8-
| The closure you provide to your test functions is always bound to a specific PHPUnit test
9-
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
10-
| need to change it using the "uses()" function to bind a different classes or traits.
11-
|
12-
*/
13-
14-
// uses(Tests\TestCase::class)->in('Feature');
15-
16-
/*
17-
|--------------------------------------------------------------------------
18-
| Expectations
19-
|--------------------------------------------------------------------------
20-
|
21-
| When you're writing tests, you often need to check that values meet certain conditions. The
22-
| "expect()" function gives you access to a set of "expectations" methods that you can use
23-
| to assert different things. Of course, you may extend the Expectation API at any time.
24-
|
25-
*/
26-
27-
expect()->extend('toBeOne', function () {
28-
return $this->toBe(1);
29-
});
30-
31-
/*
32-
|--------------------------------------------------------------------------
33-
| Functions
34-
|--------------------------------------------------------------------------
35-
|
36-
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
37-
| project that you don't want to repeat in every file. Here you can also expose helpers as
38-
| global functions to help you to reduce the number of lines of code in your test files.
39-
|
40-
*/
41-
42-
function something()
43-
{
44-
// ..
45-
}

0 commit comments

Comments
 (0)