-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.php
108 lines (98 loc) · 3.47 KB
/
App.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
97
98
99
100
101
102
103
104
105
106
107
108
<?php
use Config\BotConfig;
use Config\DbConfig;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Telegram;
use MrMuminov\PhpI18n\I18n;
class App
{
public static DbConfig $database;
public static Telegram $telegram;
public static bool $development = false;
public static BotConfig $bot;
public static I18n $i18n;
public static array $params = [];
public function __construct(
DbConfig $database,
BotConfig $bot,
I18n $i18n,
array $params = [],
bool $development = false,
)
{
self::$database = $database;
self::$bot = $bot;
self::$development = $development;
self::$i18n = $i18n;
self::$params = $params;
}
public static function log(mixed $message): void
{
@fputs(@fopen(filename: 'php://stdout', mode: 'a+'), print_r($message, 1) . PHP_EOL);
}
public static function debug(mixed $message): void
{
@fputs(@fopen(filename: 'php://stdout', mode: 'a+'), var_export($message, 1) . PHP_EOL);
}
public static function init(): void
{
if (isset(self::$telegram)) {
return;
}
if (!file_exists(__DIR__ . '/config.php')) {
throw new Exception(
message: "config.php file not found." . PHP_EOL .
"Please, copy config.example.php to name config.php and try again."
);
}
$config = require __DIR__ . '/config.php';
(new App(
database: new DbConfig(
host: $config['database']['host'],
port: $config['database']['port'],
username: $config['database']['username'],
password: $config['database']['password'],
dbname: $config['database']['dbname'],
attributes: $config['database']['attributes'] ?? [],
),
bot: new BotConfig(
bot_api_token: $config['bot']['bot_api_token'],
bot_username: $config['bot']['bot_username'],
webhook_url: $config['bot']['webhook_url'],
),
i18n: new I18n([
'path' => $config['i18n']['path'],
'languages' => $config['i18n']['languages'],
'language' => $config['i18n']['language'],
]),
params: $config['params'] ?? [],
development: $config['development'] ?? false,
));
self::$telegram = new Telegram(
api_key: App::$bot->bot_api_token,
bot_username: App::$bot->bot_username,
);
}
public static function setWebHook(
string $url = null,
mixed $certificate = null,
string $ipAddress = null,
string $maxConnections = null,
string $allowedUpdates = null,
string $dropPendingUpdates = null,
string $secretToken = null,
): ServerResponse
{
$data = [];
if ($certificate) $data['certificate'] = $certificate;
if ($ipAddress) $data['ip_address'] = $ipAddress;
if ($maxConnections) $data['max_connections'] = $maxConnections;
if ($allowedUpdates) $data['allowed_updates'] = $allowedUpdates;
if ($dropPendingUpdates) $data['drop_pending_updates'] = $dropPendingUpdates;
if ($secretToken) $data['secret_token'] = $secretToken;
return self::$telegram->setWebhook(
url: $url ?? App::$bot->webhook_url,
data: $data,
);
}
}