|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace phpbu\App\Log; |
|
6
|
|
|
|
|
7
|
|
|
use GuzzleHttp\Client; |
|
8
|
|
|
use phpbu\App\Exception; |
|
9
|
|
|
use phpbu\App\Event; |
|
10
|
|
|
use phpbu\App\Listener; |
|
11
|
|
|
use phpbu\App\Result; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Telegram |
|
15
|
|
|
* @package phpbu |
|
16
|
|
|
* @subpackage Log |
|
17
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
18
|
|
|
* @author Anatoly Skornyakov <[email protected]> |
|
19
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
|
20
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
21
|
|
|
* @link http://phpbu.de/ |
|
22
|
|
|
*/ |
|
23
|
|
|
class Telegram implements Listener, Logger |
|
24
|
|
|
{ |
|
25
|
|
|
private const URL = 'https://api.telegram.org/bot%d:%s/sendMessage'; |
|
26
|
|
|
|
|
27
|
|
|
private const CHAT_ID_OPTION_INDEX = 'chat_id'; |
|
28
|
|
|
|
|
29
|
|
|
private const BOT_ID_OPTION_INDEX = 'bot_id'; |
|
30
|
|
|
|
|
31
|
|
|
private const BOT_TOKEN_OPTION_INDEX = 'bot_token'; |
|
32
|
|
|
|
|
33
|
|
|
private const REQUIRED_OPTIONS = [ |
|
34
|
|
|
self::CHAT_ID_OPTION_INDEX, |
|
35
|
|
|
self::BOT_ID_OPTION_INDEX, |
|
36
|
|
|
self::BOT_TOKEN_OPTION_INDEX, |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var int |
|
41
|
|
|
*/ |
|
42
|
|
|
private $chatId; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var Client |
|
46
|
|
|
*/ |
|
47
|
|
|
private $client; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Setup the logger. |
|
51
|
|
|
* |
|
52
|
|
|
* @param array $options |
|
53
|
|
|
* |
|
54
|
|
|
* @throws Exception |
|
55
|
|
|
*/ |
|
56
|
5 |
|
public function setup(array $options) |
|
57
|
|
|
{ |
|
58
|
5 |
|
foreach (self::REQUIRED_OPTIONS as $index) { |
|
59
|
5 |
|
if (empty($options[$index])) { |
|
60
|
4 |
|
throw new Exception( |
|
61
|
4 |
|
sprintf( |
|
62
|
5 |
|
'Telegram Logger: Not set %s', |
|
63
|
|
|
$index |
|
64
|
|
|
) |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
$this->chatId = $options[self::CHAT_ID_OPTION_INDEX]; |
|
70
|
|
|
|
|
71
|
1 |
|
$this->client = new Client( |
|
72
|
|
|
[ |
|
73
|
1 |
|
'base_uri' => $this->generateApiUrl($options), |
|
74
|
|
|
] |
|
75
|
|
|
); |
|
76
|
1 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param array $options |
|
80
|
|
|
* |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
2 |
|
protected function generateApiUrl(array $options): string |
|
84
|
|
|
{ |
|
85
|
2 |
|
return sprintf( |
|
86
|
2 |
|
self::URL, |
|
87
|
2 |
|
$options[self::BOT_ID_OPTION_INDEX], |
|
88
|
2 |
|
$options[self::BOT_TOKEN_OPTION_INDEX] |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param string $message |
|
94
|
|
|
*/ |
|
95
|
|
|
private function sendMessage(string $message): void |
|
96
|
|
|
{ |
|
97
|
|
|
$this->client->get( |
|
98
|
|
|
'', |
|
99
|
|
|
[ |
|
100
|
|
|
'query' => [ |
|
101
|
|
|
'text' => $message, |
|
102
|
|
|
'chat_id' => $this->chatId, |
|
103
|
|
|
], |
|
104
|
|
|
] |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return array |
|
110
|
|
|
*/ |
|
111
|
1 |
|
public static function getSubscribedEvents(): array |
|
112
|
|
|
{ |
|
113
|
|
|
return [ |
|
114
|
1 |
|
Event\App\End::NAME => 'onPhpbuEnd', |
|
115
|
|
|
]; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param Event\App\End $event |
|
120
|
|
|
*/ |
|
121
|
|
|
public function onPhpbuEnd(Event\App\End $event): void |
|
122
|
|
|
{ |
|
123
|
|
|
$this->sendMessage( |
|
124
|
|
|
$this->generateMessage($event->getResult()) |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param Result $result |
|
130
|
|
|
* |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
2 |
|
protected function generateMessage(Result $result): string |
|
134
|
|
|
{ |
|
135
|
|
|
$messageLines = [ |
|
136
|
2 |
|
'Backup is done.' . ($result->allOk() ? '' : ' But with errors.'), |
|
137
|
2 |
|
'Backups:', |
|
138
|
|
|
]; |
|
139
|
|
|
|
|
140
|
|
|
/** @var \phpbu\App\Result\Backup $backup */ |
|
141
|
2 |
|
foreach ($result->getBackups() as $backup) { |
|
142
|
|
|
$messageLines[] = sprintf( |
|
143
|
|
|
'* %s %s', |
|
144
|
|
|
($backup->allOk() ? '✅' : '‼️'), |
|
145
|
|
|
$backup->getName() |
|
146
|
|
|
); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
2 |
|
return implode(PHP_EOL, $messageLines); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|