Skip to content

Commit d23a22c

Browse files
committed
Brand new "/webhook" command.
1 parent d67ec68 commit d23a22c

File tree

7 files changed

+104
-1
lines changed

7 files changed

+104
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
.env
88
.env.backup
99
.phpunit.result.cache
10+
.DS_Store
1011
Homestead.json
1112
Homestead.yaml
1213
npm-debug.log

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ If you want you can delete added token from the bot by using this command.
7171

7272
This command creates a new menu for managing servers.
7373

74+
### /webhook
75+
76+
If you don't want to provide your API token for managing your Laravel Forge account but want to receive deployment notifications the use this command.
77+
7478
### /showchatid
7579

7680
Use this command to get an ID of any chat.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace App\Integrations\Telegram\Commands\Irazasyed;
4+
5+
use Illuminate\Support\Facades\Cache;
6+
use Illuminate\Support\Facades\Storage;
7+
use Telegram\Bot\Exceptions\CouldNotUploadInputFile;
8+
use Telegram\Bot\FileUpload\InputFile;
9+
use Telegram\Bot\Objects\Message as MessageObject;
10+
11+
class WebhookCommand extends Command
12+
{
13+
private const IMAGE_CACHE_KEY = 'images-ids:deployment-webhooks-section.png';
14+
15+
/**
16+
* @var string Command name.
17+
*/
18+
protected $name = 'webhook';
19+
20+
/**
21+
* @var string Command description.
22+
*/
23+
protected $description = 'This command returns a webhook URL for receiving deployment notifications without providing an API token.';
24+
25+
/**
26+
* Handle the command.
27+
*
28+
* @return void
29+
*/
30+
public function handle()
31+
{
32+
$this->replyWithMessage([
33+
'text' => "It's the webhook URL for this chat:\n*{$this->user()->forgeWebhookUrl()}*",
34+
'parse_mode' => 'Markdown',
35+
]);
36+
37+
if (Cache::has(self::IMAGE_CACHE_KEY)) {
38+
$this->sendImageUsingFileId();
39+
} else {
40+
$this->sendImageUsingFile();
41+
}
42+
}
43+
44+
/**
45+
* Sends a screenshot of Forge's "Deployment Webhooks" section using file ID.
46+
* If Telegram will return an error then it will try to send the screenshot using file.
47+
*
48+
* @return void
49+
*/
50+
private function sendImageUsingFileId(): void
51+
{
52+
try {
53+
$this->sendImage(Cache::get(self::IMAGE_CACHE_KEY));
54+
} catch (CouldNotUploadInputFile $exception) {
55+
report($exception);
56+
$this->sendImageUsingFile();
57+
}
58+
}
59+
60+
/**
61+
* Sends a screenshot of Forge's "Deployment Webhooks" section using file.
62+
* Saves the file ID for using it in the future.
63+
*
64+
* @return void
65+
*/
66+
private function sendImageUsingFile(): void
67+
{
68+
$image = InputFile::create(Storage::disk()->path('telegram/deployment-webhooks-section.png'));
69+
$response = $this->sendImage($image);
70+
71+
Cache::forever(
72+
self::IMAGE_CACHE_KEY,
73+
collect($response->photo)
74+
->pluck('file_id', 'width')
75+
->sortKeysDesc()
76+
->first()
77+
);
78+
}
79+
80+
/**
81+
* Sends image.
82+
*
83+
* @param InputFile|string $image
84+
* @return MessageObject
85+
*/
86+
private function sendImage($image): MessageObject
87+
{
88+
return $this->replyWithPhoto([
89+
'photo' => $image,
90+
'caption' => "Copy that URL and paste it to this section in your Laravel Forge site details.",
91+
]);
92+
}
93+
}

app/Integrations/Telegram/Dialogs/AddTokenDialog.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ private function askForToken(): void
9393
$text = 'Let\'s add your first Laravel Forge API token. '.
9494
'Go to the [API](https://forge.laravel.com/user/profile#/api) section in your '.
9595
'Laravel Forge account settings and generate a new token. '.
96-
'Then just send it to me.';
96+
"Then just send it to me.\nIf you don't want to provide your token ".
97+
'and don\'t want to manage your Laravel Forge account using this bot, '.
98+
'but you want to receive deployment notifications then just use the /webhook command.';
9799
} else {
98100
$text = 'Send me your Laravel Forge API token.';
99101
}

app/Integrations/Telegram/IrazasyedTelegramBot.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use App\Integrations\Telegram\Commands\Irazasyed\MenuCommand;
1111
use App\Integrations\Telegram\Commands\Irazasyed\ShowChatIdCommand;
1212
use App\Integrations\Telegram\Commands\Irazasyed\StartCommand;
13+
use App\Integrations\Telegram\Commands\Irazasyed\WebhookCommand;
1314
use App\Integrations\Telegram\Entities\BotCommand;
1415
use App\Integrations\Telegram\Entities\CallbackQueryAnswer;
1516
use App\Integrations\Telegram\Entities\ChatAction;
@@ -55,6 +56,7 @@ public function __construct(string $botApiKey)
5556
AddTokenCommand::class,
5657
DeleteTokenCommand::class,
5758
MenuCommand::class,
59+
WebhookCommand::class,
5860
]);
5961

6062
if (config('services.telegram.bot.donate_command')) {

storage/app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*
22
!public/
3+
!telegram/
34
!.gitignore
13.3 KB
Loading

0 commit comments

Comments
 (0)