-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathInstallCommand.php
96 lines (80 loc) · 2.94 KB
/
InstallCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
namespace Telegram\Bot\Laravel\Console;
use Illuminate\Console\Command;
use Illuminate\Console\ConfirmableTrait;
use Illuminate\Filesystem\Filesystem;
class InstallCommand extends Command
{
use ConfirmableTrait;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'telegram:install {--force : Force the operation to run when in production}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Install Telegram Bot SDK scaffolding';
/**
* Execute the console command.
*/
public function handle(): void
{
if (! $this->confirmToProceed()) {
return;
}
// Publish config
$this->callSilent('vendor:publish', ['--tag' => 'telegram-config', '--force' => true]);
$this->installScaffolding();
}
/**
* Install the Telegram Bot SDK Scaffolding into the application.
*/
protected function installScaffolding(): void
{
// Directories
(new Filesystem)->ensureDirectoryExists(app_path('Listeners'));
(new Filesystem)->ensureDirectoryExists(app_path('Telegram/Commands'));
// Scaffolding files.
copy(__DIR__.'/../../stubs/app/Listeners/ProcessInboundPhoto.php',
app_path('Listeners/ProcessInboundPhoto.php'));
copy(__DIR__.'/../../stubs/app/Telegram/Commands/Start.php', app_path('Telegram/Commands/Start.php'));
$this->replaceInFile(
"// 'start' => App\Telegram\Commands\Start::class",
"'start' => App\Telegram\Commands\Start::class",
config_path('telegram.php')
);
$this->replaceInFile(
"'message' => [],",
"'message' => [],\n\n\t\t\t\t'message.photo' => [
\t\t\App\Listeners\ProcessInboundPhoto::class,\n\t\t\t\t],\n",
config_path('telegram.php')
);
$this->setBotTokenVariableInEnvironmentFile();
$this->line('');
$this->info('Telegram Bot SDK scaffolding installed successfully.');
$this->comment('Please add your Telegram Bot API Token & Hostname for Webhook in your ".env" file.');
$this->comment('Once configured, run "php artisan telegram:webhook:setup"');
}
/**
* Set the bot token var in the environment file.
*/
protected function setBotTokenVariableInEnvironmentFile(): void
{
if (env('TELEGRAM_BOT_TOKEN') !== null) {
return;
}
(new Filesystem)->append(base_path('.env'), "\nTELEGRAM_BOT_TOKEN=\nTELEGRAM_WEBHOOK_DOMAIN=");
(new Filesystem)->append(base_path('.env.example'), "\nTELEGRAM_BOT_TOKEN=");
}
/**
* Replace a given string within a given file.
*/
protected function replaceInFile(string $search, string $replace, string $path): void
{
file_put_contents($path, str_replace($search, $replace, file_get_contents($path)));
}
}