@@ -35,33 +35,33 @@ |
||
35 | 35 | class InlineKeyboardButton extends KeyboardButton |
36 | 36 | { |
37 | 37 | |
38 | - public function __construct($data) |
|
39 | - { |
|
38 | + public function __construct($data) |
|
39 | + { |
|
40 | 40 | |
41 | - parent::__construct($data); |
|
42 | - } |
|
41 | + parent::__construct($data); |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Creates instance of InlineKeyboardButton |
|
46 | - * |
|
47 | - * @param string $string |
|
48 | - * @return InlineKeyboardButton |
|
49 | - */ |
|
50 | - public static function make(string $string): InlineKeyboardButton |
|
51 | - { |
|
52 | - return new self($string); |
|
53 | - } |
|
44 | + /** |
|
45 | + * Creates instance of InlineKeyboardButton |
|
46 | + * |
|
47 | + * @param string $string |
|
48 | + * @return InlineKeyboardButton |
|
49 | + */ |
|
50 | + public static function make(string $string): InlineKeyboardButton |
|
51 | + { |
|
52 | + return new self($string); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * @param string $data |
|
57 | - * @return $this |
|
58 | - */ |
|
59 | - public function CallbackData(string $data): InlineKeyboardButton |
|
60 | - { |
|
61 | - $this->raw_data['callback_data'] = $data; |
|
55 | + /** |
|
56 | + * @param string $data |
|
57 | + * @return $this |
|
58 | + */ |
|
59 | + public function CallbackData(string $data): InlineKeyboardButton |
|
60 | + { |
|
61 | + $this->raw_data['callback_data'] = $data; |
|
62 | 62 | |
63 | - return $this; |
|
64 | - } |
|
63 | + return $this; |
|
64 | + } |
|
65 | 65 | |
66 | 66 | /** |
67 | 67 | * Check if the passed data array could be an InlineKeyboardButton. |
@@ -10,14 +10,14 @@ |
||
10 | 10 | class InlineKeyboard extends Keyboard |
11 | 11 | { |
12 | 12 | |
13 | - /** |
|
14 | - * Creates instance of Keyboard |
|
15 | - * |
|
16 | - * @return InlineKeyboard |
|
17 | - */ |
|
18 | - public static function make(): InlineKeyboard |
|
19 | - { |
|
20 | - return new self(); |
|
21 | - } |
|
13 | + /** |
|
14 | + * Creates instance of Keyboard |
|
15 | + * |
|
16 | + * @return InlineKeyboard |
|
17 | + */ |
|
18 | + public static function make(): InlineKeyboard |
|
19 | + { |
|
20 | + return new self(); |
|
21 | + } |
|
22 | 22 | |
23 | 23 | } |
@@ -34,83 +34,83 @@ |
||
34 | 34 | class KeyboardButton extends Entity |
35 | 35 | { |
36 | 36 | |
37 | - /** |
|
38 | - * @param array|string $data |
|
39 | - */ |
|
40 | - public function __construct($data) |
|
41 | - { |
|
42 | - if (is_string($data)) { |
|
43 | - $data = ['text' => $data]; |
|
44 | - } |
|
37 | + /** |
|
38 | + * @param array|string $data |
|
39 | + */ |
|
40 | + public function __construct($data) |
|
41 | + { |
|
42 | + if (is_string($data)) { |
|
43 | + $data = ['text' => $data]; |
|
44 | + } |
|
45 | 45 | |
46 | - parent::__construct($data); |
|
47 | - } |
|
46 | + parent::__construct($data); |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Creates instance of KeyboardButton |
|
51 | - * |
|
52 | - * @param string $string |
|
53 | - * @return KeyboardButton |
|
54 | - */ |
|
55 | - public static function make(string $string): KeyboardButton |
|
56 | - { |
|
57 | - return new self($string); |
|
58 | - } |
|
49 | + /** |
|
50 | + * Creates instance of KeyboardButton |
|
51 | + * |
|
52 | + * @param string $string |
|
53 | + * @return KeyboardButton |
|
54 | + */ |
|
55 | + public static function make(string $string): KeyboardButton |
|
56 | + { |
|
57 | + return new self($string); |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * @param string $url |
|
62 | - * @return $this |
|
63 | - */ |
|
64 | - public function WebApp(string $url): KeyboardButton |
|
65 | - { |
|
66 | - $this->raw_data['web_app'] = new WebAppInfo(['url' => $url]); |
|
60 | + /** |
|
61 | + * @param string $url |
|
62 | + * @return $this |
|
63 | + */ |
|
64 | + public function WebApp(string $url): KeyboardButton |
|
65 | + { |
|
66 | + $this->raw_data['web_app'] = new WebAppInfo(['url' => $url]); |
|
67 | 67 | |
68 | - return $this; |
|
69 | - } |
|
68 | + return $this; |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * @inheritDoc |
|
73 | - */ |
|
74 | - protected function subEntities(): array |
|
75 | - { |
|
76 | - return [ |
|
77 | - 'request_poll' => KeyboardButtonPollType::class, |
|
78 | - 'web_app' => WebAppInfo::class, |
|
79 | - ]; |
|
80 | - } |
|
71 | + /** |
|
72 | + * @inheritDoc |
|
73 | + */ |
|
74 | + protected function subEntities(): array |
|
75 | + { |
|
76 | + return [ |
|
77 | + 'request_poll' => KeyboardButtonPollType::class, |
|
78 | + 'web_app' => WebAppInfo::class, |
|
79 | + ]; |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * Check if the passed data array could be a KeyboardButton. |
|
84 | - * |
|
85 | - * @param array $data |
|
86 | - * |
|
87 | - * @return bool |
|
88 | - */ |
|
89 | - public static function couldBe(array $data): bool |
|
90 | - { |
|
91 | - return array_key_exists('text', $data); |
|
92 | - } |
|
82 | + /** |
|
83 | + * Check if the passed data array could be a KeyboardButton. |
|
84 | + * |
|
85 | + * @param array $data |
|
86 | + * |
|
87 | + * @return bool |
|
88 | + */ |
|
89 | + public static function couldBe(array $data): bool |
|
90 | + { |
|
91 | + return array_key_exists('text', $data); |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * {@inheritdoc} |
|
96 | - */ |
|
97 | - protected function validate(): void |
|
98 | - { |
|
99 | - if ($this->getProperty('text', '') === '') { |
|
100 | - throw new TelegramException('You must add some text to the button!'); |
|
101 | - } |
|
102 | - } |
|
94 | + /** |
|
95 | + * {@inheritdoc} |
|
96 | + */ |
|
97 | + protected function validate(): void |
|
98 | + { |
|
99 | + if ($this->getProperty('text', '') === '') { |
|
100 | + throw new TelegramException('You must add some text to the button!'); |
|
101 | + } |
|
102 | + } |
|
103 | 103 | |
104 | - /** |
|
105 | - * {@inheritdoc} |
|
106 | - */ |
|
107 | - public function __call(string $name, array $arguments): mixed |
|
108 | - { |
|
109 | - if (in_array($name, ['setRequestContact', 'setRequestLocation', 'setRequestPoll', 'setWebApp'], true)) { |
|
110 | - unset($this->request_contact, $this->request_location, $this->request_poll, $this->web_app); |
|
111 | - } |
|
104 | + /** |
|
105 | + * {@inheritdoc} |
|
106 | + */ |
|
107 | + public function __call(string $name, array $arguments): mixed |
|
108 | + { |
|
109 | + if (in_array($name, ['setRequestContact', 'setRequestLocation', 'setRequestPoll', 'setWebApp'], true)) { |
|
110 | + unset($this->request_contact, $this->request_location, $this->request_poll, $this->web_app); |
|
111 | + } |
|
112 | 112 | |
113 | - return parent::__call($name, $arguments); |
|
114 | - } |
|
113 | + return parent::__call($name, $arguments); |
|
114 | + } |
|
115 | 115 | |
116 | 116 | } |
@@ -19,13 +19,13 @@ |
||
19 | 19 | class WebAppInfo extends Entity |
20 | 20 | { |
21 | 21 | |
22 | - public function __construct($data) |
|
23 | - { |
|
24 | - if (is_string($data)) { |
|
25 | - $data = ['url' => $data]; |
|
26 | - } |
|
22 | + public function __construct($data) |
|
23 | + { |
|
24 | + if (is_string($data)) { |
|
25 | + $data = ['url' => $data]; |
|
26 | + } |
|
27 | 27 | |
28 | - parent::__construct($data); |
|
29 | - } |
|
28 | + parent::__construct($data); |
|
29 | + } |
|
30 | 30 | |
31 | 31 | } |
32 | 32 | \ No newline at end of file |
@@ -23,103 +23,103 @@ |
||
23 | 23 | class Keyboard extends Entity |
24 | 24 | { |
25 | 25 | |
26 | - public function __construct(array $data = []) |
|
27 | - { |
|
28 | - parent::__construct(array_merge($data, [ |
|
29 | - 'resize_keyboard' => false, |
|
30 | - 'one_time_keyboard' => false, |
|
31 | - 'input_field_placeholder' => null, |
|
32 | - 'selective' => false |
|
33 | - ])); |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * Create a new row in keyboard and add buttons. |
|
38 | - * |
|
39 | - * @param array<KeyboardButton> $row |
|
40 | - * @return Keyboard |
|
41 | - */ |
|
42 | - public function addRow(array $row): Keyboard |
|
43 | - { |
|
44 | - $keyboard_type = self::getType(); |
|
45 | - |
|
46 | - if (!isset($this->raw_data[$keyboard_type]) || !is_array($this->raw_data[$keyboard_type])) { |
|
47 | - $this->raw_data[$keyboard_type] = []; |
|
48 | - } |
|
49 | - |
|
50 | - $new_row = []; |
|
51 | - foreach ($row as $button) { |
|
52 | - $new_row[] = $button->getRawData(); |
|
53 | - } |
|
54 | - |
|
55 | - $this->raw_data[$keyboard_type][] = $new_row; |
|
56 | - |
|
57 | - return $this; |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Remove the current custom keyboard and display the default letter-keyboard. |
|
62 | - * |
|
63 | - * @link https://core.telegram.org/bots/api/#replykeyboardremove |
|
64 | - * |
|
65 | - * @param array $data |
|
66 | - * |
|
67 | - * @return Keyboard |
|
68 | - */ |
|
69 | - public static function remove(array $data = []): Keyboard |
|
70 | - { |
|
71 | - return new static(array_merge(['keyboard' => [], 'remove_keyboard' => true, 'selective' => false], $data)); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Display a reply interface to the user (act as if the user has selected the bot's message and tapped 'Reply'). |
|
76 | - * |
|
77 | - * @link https://core.telegram.org/bots/api#forcereply |
|
78 | - * |
|
79 | - * @param array $data |
|
80 | - * |
|
81 | - * @return Keyboard |
|
82 | - */ |
|
83 | - public static function forceReply(array $data = []): Keyboard |
|
84 | - { |
|
85 | - return new static(array_merge(['keyboard' => [], 'force_reply' => true, 'selective' => false], $data)); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Get keyboard button type |
|
90 | - * |
|
91 | - * @return string ["keyboard"|"inline_keyboard"] |
|
92 | - */ |
|
93 | - public function getType(): string |
|
94 | - { |
|
95 | - $reflection = new \ReflectionClass(static::class); |
|
96 | - |
|
97 | - $class_name = $reflection->getShortName(); |
|
98 | - |
|
99 | - return strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', $class_name), '_')); |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * Creates instance of Keyboard |
|
104 | - * |
|
105 | - * @return Keyboard |
|
106 | - */ |
|
107 | - public static function make(): Keyboard |
|
108 | - { |
|
109 | - return new self(); |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * @param array $rows |
|
114 | - * @return Keyboard |
|
115 | - */ |
|
116 | - public function setKeyboard(array $rows): Keyboard |
|
117 | - { |
|
118 | - foreach ($rows as $row) { |
|
119 | - $this->addRow($row); |
|
120 | - } |
|
121 | - |
|
122 | - return $this; |
|
123 | - } |
|
26 | + public function __construct(array $data = []) |
|
27 | + { |
|
28 | + parent::__construct(array_merge($data, [ |
|
29 | + 'resize_keyboard' => false, |
|
30 | + 'one_time_keyboard' => false, |
|
31 | + 'input_field_placeholder' => null, |
|
32 | + 'selective' => false |
|
33 | + ])); |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * Create a new row in keyboard and add buttons. |
|
38 | + * |
|
39 | + * @param array<KeyboardButton> $row |
|
40 | + * @return Keyboard |
|
41 | + */ |
|
42 | + public function addRow(array $row): Keyboard |
|
43 | + { |
|
44 | + $keyboard_type = self::getType(); |
|
45 | + |
|
46 | + if (!isset($this->raw_data[$keyboard_type]) || !is_array($this->raw_data[$keyboard_type])) { |
|
47 | + $this->raw_data[$keyboard_type] = []; |
|
48 | + } |
|
49 | + |
|
50 | + $new_row = []; |
|
51 | + foreach ($row as $button) { |
|
52 | + $new_row[] = $button->getRawData(); |
|
53 | + } |
|
54 | + |
|
55 | + $this->raw_data[$keyboard_type][] = $new_row; |
|
56 | + |
|
57 | + return $this; |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Remove the current custom keyboard and display the default letter-keyboard. |
|
62 | + * |
|
63 | + * @link https://core.telegram.org/bots/api/#replykeyboardremove |
|
64 | + * |
|
65 | + * @param array $data |
|
66 | + * |
|
67 | + * @return Keyboard |
|
68 | + */ |
|
69 | + public static function remove(array $data = []): Keyboard |
|
70 | + { |
|
71 | + return new static(array_merge(['keyboard' => [], 'remove_keyboard' => true, 'selective' => false], $data)); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Display a reply interface to the user (act as if the user has selected the bot's message and tapped 'Reply'). |
|
76 | + * |
|
77 | + * @link https://core.telegram.org/bots/api#forcereply |
|
78 | + * |
|
79 | + * @param array $data |
|
80 | + * |
|
81 | + * @return Keyboard |
|
82 | + */ |
|
83 | + public static function forceReply(array $data = []): Keyboard |
|
84 | + { |
|
85 | + return new static(array_merge(['keyboard' => [], 'force_reply' => true, 'selective' => false], $data)); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Get keyboard button type |
|
90 | + * |
|
91 | + * @return string ["keyboard"|"inline_keyboard"] |
|
92 | + */ |
|
93 | + public function getType(): string |
|
94 | + { |
|
95 | + $reflection = new \ReflectionClass(static::class); |
|
96 | + |
|
97 | + $class_name = $reflection->getShortName(); |
|
98 | + |
|
99 | + return strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', $class_name), '_')); |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * Creates instance of Keyboard |
|
104 | + * |
|
105 | + * @return Keyboard |
|
106 | + */ |
|
107 | + public static function make(): Keyboard |
|
108 | + { |
|
109 | + return new self(); |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * @param array $rows |
|
114 | + * @return Keyboard |
|
115 | + */ |
|
116 | + public function setKeyboard(array $rows): Keyboard |
|
117 | + { |
|
118 | + foreach ($rows as $row) { |
|
119 | + $this->addRow($row); |
|
120 | + } |
|
121 | + |
|
122 | + return $this; |
|
123 | + } |
|
124 | 124 | |
125 | 125 | } |
@@ -12,157 +12,157 @@ |
||
12 | 12 | abstract class Entity |
13 | 13 | { |
14 | 14 | |
15 | - /** |
|
16 | - * @var array The raw data passed to this entity |
|
17 | - */ |
|
18 | - protected array $raw_data; |
|
19 | - |
|
20 | - /** |
|
21 | - * Entity constructor. |
|
22 | - * |
|
23 | - * @param ?array $data The raw data passed to this entity |
|
24 | - */ |
|
25 | - public function __construct(?array $data) |
|
26 | - { |
|
27 | - if (array_key_exists('raw_data', $data)) { |
|
28 | - $this->raw_data = $data['raw_data']; |
|
29 | - } |
|
30 | - |
|
31 | - $this->assignMemberVariables(($this->raw_data = $data)); |
|
32 | - $this->validate(); |
|
33 | - } |
|
34 | - |
|
35 | - /** |
|
36 | - * Get the raw data passed to this entity |
|
37 | - * |
|
38 | - * @param bool $associated |
|
39 | - * @return array|string |
|
40 | - */ |
|
41 | - public function getRawData(bool $associated = true): array|string |
|
42 | - { |
|
43 | - return $associated ? $this->raw_data : json_encode($this->raw_data); |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * Helper to set member variables |
|
48 | - * |
|
49 | - * @param array $data |
|
50 | - * @return void |
|
51 | - */ |
|
52 | - protected function assignMemberVariables(array $data): void |
|
53 | - { |
|
54 | - foreach ($data as $key => $value) { |
|
55 | - $this->$key = $value; |
|
56 | - } |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * Get a property from the current Entity |
|
61 | - * |
|
62 | - * @param string $property |
|
63 | - * @param mixed $default |
|
64 | - * |
|
65 | - * @return mixed |
|
66 | - */ |
|
67 | - public function getProperty(string $property, mixed $default = null): mixed |
|
68 | - { |
|
69 | - return $this->raw_data[$property] ?? $default; |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * Get the list of the properties that are themselves Entities |
|
74 | - * |
|
75 | - * @return array |
|
76 | - */ |
|
77 | - protected function subEntities(): array |
|
78 | - { |
|
79 | - return []; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Perform any special entity validation |
|
84 | - * |
|
85 | - * @return void |
|
86 | - */ |
|
87 | - protected function validate(): void |
|
88 | - { |
|
89 | - // Do nothing by default |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * @param string $name The name of the property |
|
94 | - * @param array $arguments The arguments passed to the method |
|
95 | - * @return mixed |
|
96 | - */ |
|
97 | - public function __call(string $name, array $arguments): mixed |
|
98 | - { |
|
99 | - if (method_exists($this, $name)) { |
|
100 | - return $this->{$name}(...$arguments); |
|
101 | - } |
|
102 | - |
|
103 | - if (str_starts_with($name, 'get')) { |
|
104 | - $property_name = strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', substr($name, 3)), '_')); |
|
105 | - |
|
106 | - $property = $this->getProperty($property_name); |
|
107 | - $sub_entities = $this->subEntities() ?? []; |
|
108 | - |
|
109 | - if (isset($sub_entities[$property_name])) { |
|
110 | - $class_name = $sub_entities[$property_name]; |
|
111 | - return Factory::resolveEntityClass($class_name, $property); |
|
112 | - } |
|
113 | - |
|
114 | - return $property ?? null; |
|
115 | - } |
|
116 | - |
|
117 | - if (str_starts_with($name, 'set')) { |
|
118 | - $property_name = strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', substr($name, 3)), '_')); |
|
119 | - |
|
120 | - if (property_exists($this, $property_name)) { |
|
121 | - $this->{$property_name} = $arguments[0]; |
|
122 | - $this->raw_data[$property_name] = $arguments[0]; |
|
123 | - |
|
124 | - return $this; |
|
125 | - } |
|
126 | - |
|
127 | - } |
|
128 | - |
|
129 | - throw new \BadMethodCallException("Method '$name' does not exist"); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Escape markdown (v1) special characters |
|
134 | - * |
|
135 | - * @see https://core.telegram.org/bots/api#markdown-style |
|
136 | - * |
|
137 | - * @param string $string |
|
138 | - * |
|
139 | - * @return string |
|
140 | - */ |
|
141 | - public static function escapeMarkdown(string $string): string |
|
142 | - { |
|
143 | - return str_replace( |
|
144 | - ['[', '`', '*', '_',], |
|
145 | - ['\[', '\`', '\*', '\_',], |
|
146 | - $string |
|
147 | - ); |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Escape markdown (v2) special characters |
|
152 | - * |
|
153 | - * @see https://core.telegram.org/bots/api#markdownv2-style |
|
154 | - * |
|
155 | - * @param string $string |
|
156 | - * |
|
157 | - * @return string |
|
158 | - */ |
|
159 | - public static function escapeMarkdownV2(string $string): string |
|
160 | - { |
|
161 | - return str_replace( |
|
162 | - ['_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!'], |
|
163 | - ['\_', '\*', '\[', '\]', '\(', '\)', '\~', '\`', '\>', '\#', '\+', '\-', '\=', '\|', '\{', '\}', '\.', '\!'], |
|
164 | - $string |
|
165 | - ); |
|
166 | - } |
|
15 | + /** |
|
16 | + * @var array The raw data passed to this entity |
|
17 | + */ |
|
18 | + protected array $raw_data; |
|
19 | + |
|
20 | + /** |
|
21 | + * Entity constructor. |
|
22 | + * |
|
23 | + * @param ?array $data The raw data passed to this entity |
|
24 | + */ |
|
25 | + public function __construct(?array $data) |
|
26 | + { |
|
27 | + if (array_key_exists('raw_data', $data)) { |
|
28 | + $this->raw_data = $data['raw_data']; |
|
29 | + } |
|
30 | + |
|
31 | + $this->assignMemberVariables(($this->raw_data = $data)); |
|
32 | + $this->validate(); |
|
33 | + } |
|
34 | + |
|
35 | + /** |
|
36 | + * Get the raw data passed to this entity |
|
37 | + * |
|
38 | + * @param bool $associated |
|
39 | + * @return array|string |
|
40 | + */ |
|
41 | + public function getRawData(bool $associated = true): array|string |
|
42 | + { |
|
43 | + return $associated ? $this->raw_data : json_encode($this->raw_data); |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * Helper to set member variables |
|
48 | + * |
|
49 | + * @param array $data |
|
50 | + * @return void |
|
51 | + */ |
|
52 | + protected function assignMemberVariables(array $data): void |
|
53 | + { |
|
54 | + foreach ($data as $key => $value) { |
|
55 | + $this->$key = $value; |
|
56 | + } |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * Get a property from the current Entity |
|
61 | + * |
|
62 | + * @param string $property |
|
63 | + * @param mixed $default |
|
64 | + * |
|
65 | + * @return mixed |
|
66 | + */ |
|
67 | + public function getProperty(string $property, mixed $default = null): mixed |
|
68 | + { |
|
69 | + return $this->raw_data[$property] ?? $default; |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * Get the list of the properties that are themselves Entities |
|
74 | + * |
|
75 | + * @return array |
|
76 | + */ |
|
77 | + protected function subEntities(): array |
|
78 | + { |
|
79 | + return []; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Perform any special entity validation |
|
84 | + * |
|
85 | + * @return void |
|
86 | + */ |
|
87 | + protected function validate(): void |
|
88 | + { |
|
89 | + // Do nothing by default |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * @param string $name The name of the property |
|
94 | + * @param array $arguments The arguments passed to the method |
|
95 | + * @return mixed |
|
96 | + */ |
|
97 | + public function __call(string $name, array $arguments): mixed |
|
98 | + { |
|
99 | + if (method_exists($this, $name)) { |
|
100 | + return $this->{$name}(...$arguments); |
|
101 | + } |
|
102 | + |
|
103 | + if (str_starts_with($name, 'get')) { |
|
104 | + $property_name = strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', substr($name, 3)), '_')); |
|
105 | + |
|
106 | + $property = $this->getProperty($property_name); |
|
107 | + $sub_entities = $this->subEntities() ?? []; |
|
108 | + |
|
109 | + if (isset($sub_entities[$property_name])) { |
|
110 | + $class_name = $sub_entities[$property_name]; |
|
111 | + return Factory::resolveEntityClass($class_name, $property); |
|
112 | + } |
|
113 | + |
|
114 | + return $property ?? null; |
|
115 | + } |
|
116 | + |
|
117 | + if (str_starts_with($name, 'set')) { |
|
118 | + $property_name = strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', substr($name, 3)), '_')); |
|
119 | + |
|
120 | + if (property_exists($this, $property_name)) { |
|
121 | + $this->{$property_name} = $arguments[0]; |
|
122 | + $this->raw_data[$property_name] = $arguments[0]; |
|
123 | + |
|
124 | + return $this; |
|
125 | + } |
|
126 | + |
|
127 | + } |
|
128 | + |
|
129 | + throw new \BadMethodCallException("Method '$name' does not exist"); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Escape markdown (v1) special characters |
|
134 | + * |
|
135 | + * @see https://core.telegram.org/bots/api#markdown-style |
|
136 | + * |
|
137 | + * @param string $string |
|
138 | + * |
|
139 | + * @return string |
|
140 | + */ |
|
141 | + public static function escapeMarkdown(string $string): string |
|
142 | + { |
|
143 | + return str_replace( |
|
144 | + ['[', '`', '*', '_',], |
|
145 | + ['\[', '\`', '\*', '\_',], |
|
146 | + $string |
|
147 | + ); |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Escape markdown (v2) special characters |
|
152 | + * |
|
153 | + * @see https://core.telegram.org/bots/api#markdownv2-style |
|
154 | + * |
|
155 | + * @param string $string |
|
156 | + * |
|
157 | + * @return string |
|
158 | + */ |
|
159 | + public static function escapeMarkdownV2(string $string): string |
|
160 | + { |
|
161 | + return str_replace( |
|
162 | + ['_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!'], |
|
163 | + ['\_', '\*', '\[', '\]', '\(', '\)', '\~', '\`', '\>', '\#', '\+', '\-', '\=', '\|', '\{', '\}', '\.', '\!'], |
|
164 | + $string |
|
165 | + ); |
|
166 | + } |
|
167 | 167 | |
168 | 168 | } |
169 | 169 | \ No newline at end of file |
@@ -141,8 +141,8 @@ |
||
141 | 141 | public static function escapeMarkdown(string $string): string |
142 | 142 | { |
143 | 143 | return str_replace( |
144 | - ['[', '`', '*', '_',], |
|
145 | - ['\[', '\`', '\*', '\_',], |
|
144 | + ['[', '`', '*', '_', ], |
|
145 | + ['\[', '\`', '\*', '\_', ], |
|
146 | 146 | $string |
147 | 147 | ); |
148 | 148 | } |
@@ -17,213 +17,213 @@ |
||
17 | 17 | abstract class WebhookHandler extends Telegram |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * @var ?Update |
|
22 | - */ |
|
23 | - protected ?Update $update; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var array<Plugin> |
|
27 | - */ |
|
28 | - private array $plugins = []; |
|
29 | - |
|
30 | - /** |
|
31 | - * @var bool |
|
32 | - */ |
|
33 | - private bool $isActive = false; |
|
34 | - |
|
35 | - /** |
|
36 | - * The default configuration of the webhook. |
|
37 | - * |
|
38 | - * @var array |
|
39 | - */ |
|
40 | - private array $config = [ |
|
41 | - 'autoload_env_file' => false, |
|
42 | - 'env_file_path' => null, |
|
43 | - ]; |
|
44 | - |
|
45 | - /** |
|
46 | - * Webhook constructor. |
|
47 | - * |
|
48 | - * @param string $api_key The API key of the bot. Leave it blank for autoload from .env file. |
|
49 | - */ |
|
50 | - public function __construct(string $api_key = '') |
|
51 | - { |
|
52 | - parent::__construct($api_key); |
|
53 | - |
|
54 | - if (!Telegram::validateToken(self::getApiKey())) { |
|
55 | - throw new InvalidBotTokenException(); |
|
56 | - } |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * Initialize the receiver. |
|
61 | - * |
|
62 | - * @return void |
|
63 | - */ |
|
64 | - public function init(): void |
|
65 | - { |
|
66 | - $this->config['env_file_path'] = $_SERVER['DOCUMENT_ROOT'] . '/.env'; |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * Add a plugin to the receiver |
|
71 | - * |
|
72 | - * @param array<Plugin> $plugins |
|
73 | - */ |
|
74 | - public function addPlugin(array $plugins): void |
|
75 | - { |
|
76 | - foreach ($plugins as $plugin) { |
|
77 | - if (!is_subclass_of($plugin, Plugin::class)) { |
|
78 | - throw new \RuntimeException( |
|
79 | - sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class) |
|
80 | - ); |
|
81 | - } |
|
82 | - $this->plugins[] = $plugin; |
|
83 | - } |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Match the update with the given plugins |
|
88 | - * |
|
89 | - * @param array<Plugin> $plugins |
|
90 | - * @param ?Update $update The update to match |
|
91 | - * @return void |
|
92 | - */ |
|
93 | - public function loadPluginsWith(array $plugins, Update $update = null): void |
|
94 | - { |
|
95 | - $update = $update ?? Telegram::getUpdate(); |
|
96 | - foreach ($plugins as $plugin) { |
|
97 | - if (!is_subclass_of($plugin, Plugin::class)) { |
|
98 | - throw new \InvalidArgumentException( |
|
99 | - sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class) |
|
100 | - ); |
|
101 | - } |
|
102 | - } |
|
103 | - if ($update instanceof Update) { |
|
104 | - $this->spreadUpdateWith($update, $plugins); |
|
105 | - } |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Match the message to the plugins |
|
110 | - * |
|
111 | - * @param ?Update $update The update to match |
|
112 | - * @return void |
|
113 | - */ |
|
114 | - public function loadPlugins(Update $update = null): void |
|
115 | - { |
|
116 | - $update = $update ?? ($this->update ?? Telegram::getUpdate()); |
|
117 | - $this->loadPluginsWith($this->plugins, $update); |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Load the environment's |
|
122 | - * |
|
123 | - * @param string $path |
|
124 | - * @retrun void |
|
125 | - */ |
|
126 | - public function loadFileOfEnv(string $path): void |
|
127 | - { |
|
128 | - DotEnv::load($path); |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Update the configuration |
|
133 | - * |
|
134 | - * @param array $configuration |
|
135 | - * @return void |
|
136 | - */ |
|
137 | - public function updateConfiguration(array $configuration): void |
|
138 | - { |
|
139 | - $this->config = array_merge($this->config, $configuration); |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Resolve the request. |
|
144 | - * |
|
145 | - * @param ?Update $update The custom to work with |
|
146 | - * @param array $config The configuration of the receiver |
|
147 | - * |
|
148 | - * @retrun void |
|
149 | - */ |
|
150 | - public function resolve(Update $update = null, array $config = []): void |
|
151 | - { |
|
152 | - $method = '__process'; |
|
153 | - if (!method_exists($this, $method)) { |
|
154 | - throw new \RuntimeException(sprintf('The method %s does not exist', $method)); |
|
155 | - } |
|
156 | - |
|
157 | - if (is_array($config)) $this->updateConfiguration($config); |
|
158 | - |
|
159 | - if (!empty($update)) $this->update = $update; |
|
160 | - else $this->update = Telegram::getUpdate() !== false ? Telegram::getUpdate() : null; |
|
161 | - |
|
162 | - if (empty($this->update)) { |
|
163 | - TelegramLog::error('The update is empty'); |
|
164 | - return; |
|
165 | - } |
|
166 | - |
|
167 | - try { |
|
168 | - |
|
169 | - Common::arrest($this, $method, $this->update); |
|
170 | - |
|
171 | - } catch (\RuntimeException $e) { |
|
172 | - TelegramLog::error(($message = sprintf('%s: %s', $e->getMessage(), $e->getTraceAsString()))); |
|
173 | - if (defined('TG_ADMIN_ID') && TG_ADMIN_ID && defined('DEBUG_MODE') && DEBUG_MODE) { |
|
174 | - file_put_contents( |
|
175 | - ($file = getcwd() . '/' . uniqid('error_') . '.log'), |
|
176 | - $message . PHP_EOL . PHP_EOL . $update->getRawData(false) |
|
177 | - ); |
|
178 | - Request::sendMessage([ |
|
179 | - 'chat_id' => TG_ADMIN_ID, |
|
180 | - 'text' => $message, |
|
181 | - ]); |
|
182 | - Request::sendDocument([ |
|
183 | - 'chat_id' => TG_ADMIN_ID, |
|
184 | - 'document' => $file, |
|
185 | - ]); |
|
186 | - unlink($file); |
|
187 | - } |
|
188 | - } |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * This function will get update and spread it to the plugins |
|
193 | - * |
|
194 | - * @param Update $update |
|
195 | - * @param array<Plugin> $plugins |
|
196 | - * @return void |
|
197 | - */ |
|
198 | - private function spreadUpdateWith(Update $update, array $plugins): void |
|
199 | - { |
|
200 | - $this->isActive = true; |
|
201 | - |
|
202 | - foreach ($plugins as $plugin) { |
|
203 | - /** @var Plugin $plugin */ |
|
204 | - (new $plugin())->__execute($this, $update); |
|
205 | - if ($this->isActive === false) break; |
|
206 | - } |
|
207 | - |
|
208 | - $this->isActive = false; |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * Kill the speeding process |
|
213 | - * |
|
214 | - * @return void |
|
215 | - */ |
|
216 | - public function kill(): void |
|
217 | - { |
|
218 | - $this->isActive = false; |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Use this method on the webhook class to get the updates |
|
223 | - * |
|
224 | - * @param Update $update |
|
225 | - * @return void |
|
226 | - */ |
|
227 | - abstract public function __process(Update $update): void; |
|
20 | + /** |
|
21 | + * @var ?Update |
|
22 | + */ |
|
23 | + protected ?Update $update; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var array<Plugin> |
|
27 | + */ |
|
28 | + private array $plugins = []; |
|
29 | + |
|
30 | + /** |
|
31 | + * @var bool |
|
32 | + */ |
|
33 | + private bool $isActive = false; |
|
34 | + |
|
35 | + /** |
|
36 | + * The default configuration of the webhook. |
|
37 | + * |
|
38 | + * @var array |
|
39 | + */ |
|
40 | + private array $config = [ |
|
41 | + 'autoload_env_file' => false, |
|
42 | + 'env_file_path' => null, |
|
43 | + ]; |
|
44 | + |
|
45 | + /** |
|
46 | + * Webhook constructor. |
|
47 | + * |
|
48 | + * @param string $api_key The API key of the bot. Leave it blank for autoload from .env file. |
|
49 | + */ |
|
50 | + public function __construct(string $api_key = '') |
|
51 | + { |
|
52 | + parent::__construct($api_key); |
|
53 | + |
|
54 | + if (!Telegram::validateToken(self::getApiKey())) { |
|
55 | + throw new InvalidBotTokenException(); |
|
56 | + } |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * Initialize the receiver. |
|
61 | + * |
|
62 | + * @return void |
|
63 | + */ |
|
64 | + public function init(): void |
|
65 | + { |
|
66 | + $this->config['env_file_path'] = $_SERVER['DOCUMENT_ROOT'] . '/.env'; |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * Add a plugin to the receiver |
|
71 | + * |
|
72 | + * @param array<Plugin> $plugins |
|
73 | + */ |
|
74 | + public function addPlugin(array $plugins): void |
|
75 | + { |
|
76 | + foreach ($plugins as $plugin) { |
|
77 | + if (!is_subclass_of($plugin, Plugin::class)) { |
|
78 | + throw new \RuntimeException( |
|
79 | + sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class) |
|
80 | + ); |
|
81 | + } |
|
82 | + $this->plugins[] = $plugin; |
|
83 | + } |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Match the update with the given plugins |
|
88 | + * |
|
89 | + * @param array<Plugin> $plugins |
|
90 | + * @param ?Update $update The update to match |
|
91 | + * @return void |
|
92 | + */ |
|
93 | + public function loadPluginsWith(array $plugins, Update $update = null): void |
|
94 | + { |
|
95 | + $update = $update ?? Telegram::getUpdate(); |
|
96 | + foreach ($plugins as $plugin) { |
|
97 | + if (!is_subclass_of($plugin, Plugin::class)) { |
|
98 | + throw new \InvalidArgumentException( |
|
99 | + sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class) |
|
100 | + ); |
|
101 | + } |
|
102 | + } |
|
103 | + if ($update instanceof Update) { |
|
104 | + $this->spreadUpdateWith($update, $plugins); |
|
105 | + } |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Match the message to the plugins |
|
110 | + * |
|
111 | + * @param ?Update $update The update to match |
|
112 | + * @return void |
|
113 | + */ |
|
114 | + public function loadPlugins(Update $update = null): void |
|
115 | + { |
|
116 | + $update = $update ?? ($this->update ?? Telegram::getUpdate()); |
|
117 | + $this->loadPluginsWith($this->plugins, $update); |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Load the environment's |
|
122 | + * |
|
123 | + * @param string $path |
|
124 | + * @retrun void |
|
125 | + */ |
|
126 | + public function loadFileOfEnv(string $path): void |
|
127 | + { |
|
128 | + DotEnv::load($path); |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Update the configuration |
|
133 | + * |
|
134 | + * @param array $configuration |
|
135 | + * @return void |
|
136 | + */ |
|
137 | + public function updateConfiguration(array $configuration): void |
|
138 | + { |
|
139 | + $this->config = array_merge($this->config, $configuration); |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Resolve the request. |
|
144 | + * |
|
145 | + * @param ?Update $update The custom to work with |
|
146 | + * @param array $config The configuration of the receiver |
|
147 | + * |
|
148 | + * @retrun void |
|
149 | + */ |
|
150 | + public function resolve(Update $update = null, array $config = []): void |
|
151 | + { |
|
152 | + $method = '__process'; |
|
153 | + if (!method_exists($this, $method)) { |
|
154 | + throw new \RuntimeException(sprintf('The method %s does not exist', $method)); |
|
155 | + } |
|
156 | + |
|
157 | + if (is_array($config)) $this->updateConfiguration($config); |
|
158 | + |
|
159 | + if (!empty($update)) $this->update = $update; |
|
160 | + else $this->update = Telegram::getUpdate() !== false ? Telegram::getUpdate() : null; |
|
161 | + |
|
162 | + if (empty($this->update)) { |
|
163 | + TelegramLog::error('The update is empty'); |
|
164 | + return; |
|
165 | + } |
|
166 | + |
|
167 | + try { |
|
168 | + |
|
169 | + Common::arrest($this, $method, $this->update); |
|
170 | + |
|
171 | + } catch (\RuntimeException $e) { |
|
172 | + TelegramLog::error(($message = sprintf('%s: %s', $e->getMessage(), $e->getTraceAsString()))); |
|
173 | + if (defined('TG_ADMIN_ID') && TG_ADMIN_ID && defined('DEBUG_MODE') && DEBUG_MODE) { |
|
174 | + file_put_contents( |
|
175 | + ($file = getcwd() . '/' . uniqid('error_') . '.log'), |
|
176 | + $message . PHP_EOL . PHP_EOL . $update->getRawData(false) |
|
177 | + ); |
|
178 | + Request::sendMessage([ |
|
179 | + 'chat_id' => TG_ADMIN_ID, |
|
180 | + 'text' => $message, |
|
181 | + ]); |
|
182 | + Request::sendDocument([ |
|
183 | + 'chat_id' => TG_ADMIN_ID, |
|
184 | + 'document' => $file, |
|
185 | + ]); |
|
186 | + unlink($file); |
|
187 | + } |
|
188 | + } |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * This function will get update and spread it to the plugins |
|
193 | + * |
|
194 | + * @param Update $update |
|
195 | + * @param array<Plugin> $plugins |
|
196 | + * @return void |
|
197 | + */ |
|
198 | + private function spreadUpdateWith(Update $update, array $plugins): void |
|
199 | + { |
|
200 | + $this->isActive = true; |
|
201 | + |
|
202 | + foreach ($plugins as $plugin) { |
|
203 | + /** @var Plugin $plugin */ |
|
204 | + (new $plugin())->__execute($this, $update); |
|
205 | + if ($this->isActive === false) break; |
|
206 | + } |
|
207 | + |
|
208 | + $this->isActive = false; |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * Kill the speeding process |
|
213 | + * |
|
214 | + * @return void |
|
215 | + */ |
|
216 | + public function kill(): void |
|
217 | + { |
|
218 | + $this->isActive = false; |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Use this method on the webhook class to get the updates |
|
223 | + * |
|
224 | + * @param Update $update |
|
225 | + * @return void |
|
226 | + */ |
|
227 | + abstract public function __process(Update $update): void; |
|
228 | 228 | |
229 | 229 | } |
230 | 230 | \ No newline at end of file |
@@ -154,10 +154,15 @@ discard block |
||
154 | 154 | throw new \RuntimeException(sprintf('The method %s does not exist', $method)); |
155 | 155 | } |
156 | 156 | |
157 | - if (is_array($config)) $this->updateConfiguration($config); |
|
157 | + if (is_array($config)) { |
|
158 | + $this->updateConfiguration($config); |
|
159 | + } |
|
158 | 160 | |
159 | - if (!empty($update)) $this->update = $update; |
|
160 | - else $this->update = Telegram::getUpdate() !== false ? Telegram::getUpdate() : null; |
|
161 | + if (!empty($update)) { |
|
162 | + $this->update = $update; |
|
163 | + } else { |
|
164 | + $this->update = Telegram::getUpdate() !== false ? Telegram::getUpdate() : null; |
|
165 | + } |
|
161 | 166 | |
162 | 167 | if (empty($this->update)) { |
163 | 168 | TelegramLog::error('The update is empty'); |
@@ -202,7 +207,9 @@ discard block |
||
202 | 207 | foreach ($plugins as $plugin) { |
203 | 208 | /** @var Plugin $plugin */ |
204 | 209 | (new $plugin())->__execute($this, $update); |
205 | - if ($this->isActive === false) break; |
|
210 | + if ($this->isActive === false) { |
|
211 | + break; |
|
212 | + } |
|
206 | 213 | } |
207 | 214 | |
208 | 215 | $this->isActive = false; |
@@ -18,314 +18,314 @@ |
||
18 | 18 | class Telegram |
19 | 19 | { |
20 | 20 | |
21 | - /** |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - private string $api_key; |
|
25 | - |
|
26 | - /** |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - public static string $VERSION = 'v1.0.0'; |
|
30 | - |
|
31 | - /** |
|
32 | - * Telegram constructor. |
|
33 | - * |
|
34 | - * @param string $api_key |
|
35 | - */ |
|
36 | - public function __construct(string $api_key = '') |
|
37 | - { |
|
38 | - if ($api_key === '') { |
|
39 | - $env_file = $this->getEnvFilePath(); |
|
40 | - $api_key = DotEnv::load($env_file)->get('TELEGRAM_API_KEY'); |
|
41 | - } |
|
42 | - |
|
43 | - if (empty($api_key) || !is_string($api_key)) { |
|
44 | - throw new TelegramException('API Key is required'); |
|
45 | - } |
|
46 | - |
|
47 | - DotEnv::put('TG_CURRENT_KEY', ($this->api_key = $api_key)); |
|
48 | - DotEnv::put('TELEGRAM_API_KEY', ($this->api_key = $api_key)); |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * Get env file path and return it |
|
53 | - * |
|
54 | - * @return string |
|
55 | - */ |
|
56 | - private function getEnvFilePath(): string |
|
57 | - { |
|
58 | - $defaultEnvPaths = [ |
|
59 | - $_SERVER['DOCUMENT_ROOT'] . '/.env', |
|
60 | - getcwd() . '/../.env', |
|
61 | - getcwd() . '/.env', |
|
62 | - ]; |
|
63 | - |
|
64 | - foreach ($defaultEnvPaths as $path) { |
|
65 | - if (file_exists($path)) { |
|
66 | - return $path; |
|
67 | - } |
|
68 | - } |
|
69 | - |
|
70 | - return ''; |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * Get API key from temporary ENV variable |
|
75 | - * |
|
76 | - * @return ?string |
|
77 | - */ |
|
78 | - public static function getApiKey(): ?string |
|
79 | - { |
|
80 | - return DotEnv::get('TG_CURRENT_KEY'); |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * Get bot info from given API key |
|
85 | - * |
|
86 | - * @return Response |
|
87 | - * @throws TelegramException |
|
88 | - */ |
|
89 | - public function getInfo(): Response |
|
90 | - { |
|
91 | - $result = Request::getMe(); |
|
92 | - |
|
93 | - if (!$result->isOk()) { |
|
94 | - throw new TelegramException($result->getErrorCode() . ': ' . $result->getDescription()); |
|
95 | - } |
|
96 | - |
|
97 | - return $result; |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Set Webhook for bot |
|
102 | - * |
|
103 | - * @param string $url |
|
104 | - * @param array $data Optional parameters. |
|
105 | - * @return Response |
|
106 | - * @throws TelegramException |
|
107 | - */ |
|
108 | - public function setWebhook(string $url, array $data = []): Response |
|
109 | - { |
|
110 | - if ($url === '') { |
|
111 | - throw new TelegramException('Hook url is empty!'); |
|
112 | - } |
|
113 | - |
|
114 | - if (!str_starts_with($url, 'https://')) { |
|
115 | - throw new TelegramException('Hook url must start with https://'); |
|
116 | - } |
|
117 | - |
|
118 | - $data = array_intersect_key($data, array_flip([ |
|
119 | - 'certificate', |
|
120 | - 'ip_address', |
|
121 | - 'max_connections', |
|
122 | - 'allowed_updates', |
|
123 | - 'drop_pending_updates', |
|
124 | - ])); |
|
125 | - $data['url'] = $url; |
|
126 | - |
|
127 | - $result = Request::setWebhook($data); |
|
128 | - |
|
129 | - if (!$result->isOk()) { |
|
130 | - throw new TelegramException( |
|
131 | - 'Webhook was not set! Error: ' . $result->getErrorCode() . ' ' . $result->getDescription() |
|
132 | - ); |
|
133 | - } |
|
134 | - |
|
135 | - return $result; |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * Delete any assigned webhook |
|
140 | - * |
|
141 | - * @param array $data |
|
142 | - * @return Response |
|
143 | - * @throws TelegramException |
|
144 | - */ |
|
145 | - public function deleteWebhook(array $data = []): Response |
|
146 | - { |
|
147 | - $result = Request::deleteWebhook($data); |
|
148 | - |
|
149 | - if (!$result->isOk()) { |
|
150 | - throw new TelegramException( |
|
151 | - 'Webhook was not deleted! Error: ' . $result->getErrorCode() . ' ' . $result->getDescription() |
|
152 | - ); |
|
153 | - } |
|
154 | - |
|
155 | - return $result; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * This method sets the admin username. and will be used to send you a message if the bot is not working. |
|
160 | - * |
|
161 | - * @param int $chat_id |
|
162 | - * @return void |
|
163 | - */ |
|
164 | - public function setAdmin(int $chat_id): void |
|
165 | - { |
|
166 | - defined('TG_ADMIN_ID') or define('TG_ADMIN_ID', $chat_id); |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * Get input from stdin and return it |
|
171 | - * |
|
172 | - * @return ?string |
|
173 | - */ |
|
174 | - public static function getInput(): ?string |
|
175 | - { |
|
176 | - return file_get_contents('php://input') ?? null; |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * This method will convert a string to an update object |
|
181 | - * |
|
182 | - * @param string $input The input string |
|
183 | - * @param string $apiKey The API key |
|
184 | - * @return Update|false |
|
185 | - */ |
|
186 | - public static function processUpdate(string $input, string $apiKey): Update|false |
|
187 | - { |
|
188 | - if (empty($input)) { |
|
189 | - throw new TelegramException( |
|
190 | - 'Input is empty! Please check your code and try again.' |
|
191 | - ); |
|
192 | - } |
|
193 | - |
|
194 | - if (!self::validateToken($apiKey)) { |
|
195 | - throw new TelegramException( |
|
196 | - 'Invalid token! Please check your code and try again.' |
|
197 | - ); |
|
198 | - } |
|
199 | - |
|
200 | - if (self::validateWebData($apiKey, $input)) { |
|
201 | - if (Common::isUrlEncode($input)) { |
|
202 | - $web_data = Common::urlDecode($input); |
|
203 | - } |
|
204 | - |
|
205 | - if (Common::isJson($input)) { |
|
206 | - $web_data = json_decode($input, true); |
|
207 | - } |
|
208 | - |
|
209 | - $input = json_encode([ |
|
210 | - 'web_data' => $web_data, |
|
211 | - ]); |
|
212 | - } |
|
213 | - |
|
214 | - if (!Common::isJson($input)) { |
|
215 | - throw new TelegramException( |
|
216 | - 'Input is not a valid JSON string! Please check your code and try again.' |
|
217 | - ); |
|
218 | - } |
|
219 | - |
|
220 | - $input = json_decode($input, true); |
|
221 | - |
|
222 | - return new Update($input); |
|
223 | - } |
|
224 | - |
|
225 | - /** |
|
226 | - * Validate webapp data from is from Telegram |
|
227 | - * |
|
228 | - * @link https://core.telegram.org/bots/webapps#validating-data-received-via-the-web-app |
|
229 | - * |
|
230 | - * @param string $token The bot token |
|
231 | - * @param string $body The message body from getInput() |
|
232 | - * @return bool |
|
233 | - */ |
|
234 | - public static function validateWebData(string $token, string $body): bool |
|
235 | - { |
|
236 | - if (!Common::isJson($body)) { |
|
237 | - $raw_data = rawurldecode(str_replace('_auth=', '', $body)); |
|
238 | - $data = Common::urlDecode($raw_data); |
|
239 | - |
|
240 | - $data['user'] = urldecode($data['user']); |
|
241 | - |
|
242 | - } else { |
|
243 | - $data = json_decode($body, true); |
|
244 | - $data['user'] = json_encode($data['user']); |
|
245 | - } |
|
246 | - |
|
247 | - $data_check_string = "auth_date={$data['auth_date']}\nquery_id={$data['query_id']}\nuser={$data['user']}"; |
|
248 | - $secret_key = hash_hmac('sha256', $token, "WebAppData", true); |
|
249 | - |
|
250 | - return hash_hmac('sha256', $data_check_string, $secret_key) == $data['hash']; |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * Get the update from input |
|
255 | - * |
|
256 | - * @return Update|false |
|
257 | - */ |
|
258 | - public static function getUpdate(): Update|false |
|
259 | - { |
|
260 | - $input = self::getInput(); |
|
261 | - if (empty($input)) return false; |
|
262 | - return Telegram::processUpdate($input, self::getApiKey()); |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * Validate the token |
|
267 | - * |
|
268 | - * @param string $token (e.g. 123456789:ABC-DEF1234ghIkl-zyx57W2v1u123ew11) {digit}:{alphanumeric[34]} |
|
269 | - * @return bool |
|
270 | - */ |
|
271 | - public static function validateToken(string $token): bool |
|
272 | - { |
|
273 | - preg_match_all('/([0-9]+:[a-zA-Z0-9-_]+)/', $token, $matches); |
|
274 | - return count($matches[0]) == 1; |
|
275 | - } |
|
276 | - |
|
277 | - /** |
|
278 | - * Pass the update to the given webhook handler |
|
279 | - * |
|
280 | - * @param WebhookHandler $webhook_handler The webhook handler |
|
281 | - * @param ?Update $update By default, it will get the update from input |
|
282 | - * @return void |
|
283 | - */ |
|
284 | - public function fetchWith(WebhookHandler $webhook_handler, ?Update $update = null): void |
|
285 | - { |
|
286 | - if (is_subclass_of($webhook_handler, WebhookHandler::class)) { |
|
287 | - if ($update === null) $update = self::getUpdate(); |
|
288 | - $webhook_handler->resolve($update); |
|
289 | - } |
|
290 | - } |
|
291 | - |
|
292 | - /** |
|
293 | - * Get token from env file. |
|
294 | - * |
|
295 | - * @param string $file |
|
296 | - * @return ?string |
|
297 | - */ |
|
298 | - protected function getTokenFromEnvFile(string $file): ?string |
|
299 | - { |
|
300 | - if (!file_exists($file)) return null; |
|
301 | - return DotEnv::load($file)::get('TELEGRAM_API_KEY'); |
|
302 | - } |
|
303 | - |
|
304 | - /** |
|
305 | - * Debug mode |
|
306 | - * |
|
307 | - * @param ?int $admin_id Fill this or use setAdmin() |
|
308 | - * @return void |
|
309 | - */ |
|
310 | - public static function setDebugMode(?int $admin_id = null): void |
|
311 | - { |
|
312 | - error_reporting(E_ALL); |
|
313 | - ini_set('display_errors', 1); |
|
314 | - defined('DEBUG_MODE') or define('DEBUG_MODE', true); |
|
315 | - if ($admin_id) { |
|
316 | - defined('TG_ADMIN_ID') or define('TG_ADMIN_ID', $admin_id); |
|
317 | - } |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * Just another echo |
|
322 | - * |
|
323 | - * @param string $text |
|
324 | - * @return void |
|
325 | - */ |
|
326 | - public static function echo(string $text): void |
|
327 | - { |
|
328 | - echo $text; |
|
329 | - } |
|
21 | + /** |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + private string $api_key; |
|
25 | + |
|
26 | + /** |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + public static string $VERSION = 'v1.0.0'; |
|
30 | + |
|
31 | + /** |
|
32 | + * Telegram constructor. |
|
33 | + * |
|
34 | + * @param string $api_key |
|
35 | + */ |
|
36 | + public function __construct(string $api_key = '') |
|
37 | + { |
|
38 | + if ($api_key === '') { |
|
39 | + $env_file = $this->getEnvFilePath(); |
|
40 | + $api_key = DotEnv::load($env_file)->get('TELEGRAM_API_KEY'); |
|
41 | + } |
|
42 | + |
|
43 | + if (empty($api_key) || !is_string($api_key)) { |
|
44 | + throw new TelegramException('API Key is required'); |
|
45 | + } |
|
46 | + |
|
47 | + DotEnv::put('TG_CURRENT_KEY', ($this->api_key = $api_key)); |
|
48 | + DotEnv::put('TELEGRAM_API_KEY', ($this->api_key = $api_key)); |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * Get env file path and return it |
|
53 | + * |
|
54 | + * @return string |
|
55 | + */ |
|
56 | + private function getEnvFilePath(): string |
|
57 | + { |
|
58 | + $defaultEnvPaths = [ |
|
59 | + $_SERVER['DOCUMENT_ROOT'] . '/.env', |
|
60 | + getcwd() . '/../.env', |
|
61 | + getcwd() . '/.env', |
|
62 | + ]; |
|
63 | + |
|
64 | + foreach ($defaultEnvPaths as $path) { |
|
65 | + if (file_exists($path)) { |
|
66 | + return $path; |
|
67 | + } |
|
68 | + } |
|
69 | + |
|
70 | + return ''; |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * Get API key from temporary ENV variable |
|
75 | + * |
|
76 | + * @return ?string |
|
77 | + */ |
|
78 | + public static function getApiKey(): ?string |
|
79 | + { |
|
80 | + return DotEnv::get('TG_CURRENT_KEY'); |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * Get bot info from given API key |
|
85 | + * |
|
86 | + * @return Response |
|
87 | + * @throws TelegramException |
|
88 | + */ |
|
89 | + public function getInfo(): Response |
|
90 | + { |
|
91 | + $result = Request::getMe(); |
|
92 | + |
|
93 | + if (!$result->isOk()) { |
|
94 | + throw new TelegramException($result->getErrorCode() . ': ' . $result->getDescription()); |
|
95 | + } |
|
96 | + |
|
97 | + return $result; |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Set Webhook for bot |
|
102 | + * |
|
103 | + * @param string $url |
|
104 | + * @param array $data Optional parameters. |
|
105 | + * @return Response |
|
106 | + * @throws TelegramException |
|
107 | + */ |
|
108 | + public function setWebhook(string $url, array $data = []): Response |
|
109 | + { |
|
110 | + if ($url === '') { |
|
111 | + throw new TelegramException('Hook url is empty!'); |
|
112 | + } |
|
113 | + |
|
114 | + if (!str_starts_with($url, 'https://')) { |
|
115 | + throw new TelegramException('Hook url must start with https://'); |
|
116 | + } |
|
117 | + |
|
118 | + $data = array_intersect_key($data, array_flip([ |
|
119 | + 'certificate', |
|
120 | + 'ip_address', |
|
121 | + 'max_connections', |
|
122 | + 'allowed_updates', |
|
123 | + 'drop_pending_updates', |
|
124 | + ])); |
|
125 | + $data['url'] = $url; |
|
126 | + |
|
127 | + $result = Request::setWebhook($data); |
|
128 | + |
|
129 | + if (!$result->isOk()) { |
|
130 | + throw new TelegramException( |
|
131 | + 'Webhook was not set! Error: ' . $result->getErrorCode() . ' ' . $result->getDescription() |
|
132 | + ); |
|
133 | + } |
|
134 | + |
|
135 | + return $result; |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * Delete any assigned webhook |
|
140 | + * |
|
141 | + * @param array $data |
|
142 | + * @return Response |
|
143 | + * @throws TelegramException |
|
144 | + */ |
|
145 | + public function deleteWebhook(array $data = []): Response |
|
146 | + { |
|
147 | + $result = Request::deleteWebhook($data); |
|
148 | + |
|
149 | + if (!$result->isOk()) { |
|
150 | + throw new TelegramException( |
|
151 | + 'Webhook was not deleted! Error: ' . $result->getErrorCode() . ' ' . $result->getDescription() |
|
152 | + ); |
|
153 | + } |
|
154 | + |
|
155 | + return $result; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * This method sets the admin username. and will be used to send you a message if the bot is not working. |
|
160 | + * |
|
161 | + * @param int $chat_id |
|
162 | + * @return void |
|
163 | + */ |
|
164 | + public function setAdmin(int $chat_id): void |
|
165 | + { |
|
166 | + defined('TG_ADMIN_ID') or define('TG_ADMIN_ID', $chat_id); |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * Get input from stdin and return it |
|
171 | + * |
|
172 | + * @return ?string |
|
173 | + */ |
|
174 | + public static function getInput(): ?string |
|
175 | + { |
|
176 | + return file_get_contents('php://input') ?? null; |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * This method will convert a string to an update object |
|
181 | + * |
|
182 | + * @param string $input The input string |
|
183 | + * @param string $apiKey The API key |
|
184 | + * @return Update|false |
|
185 | + */ |
|
186 | + public static function processUpdate(string $input, string $apiKey): Update|false |
|
187 | + { |
|
188 | + if (empty($input)) { |
|
189 | + throw new TelegramException( |
|
190 | + 'Input is empty! Please check your code and try again.' |
|
191 | + ); |
|
192 | + } |
|
193 | + |
|
194 | + if (!self::validateToken($apiKey)) { |
|
195 | + throw new TelegramException( |
|
196 | + 'Invalid token! Please check your code and try again.' |
|
197 | + ); |
|
198 | + } |
|
199 | + |
|
200 | + if (self::validateWebData($apiKey, $input)) { |
|
201 | + if (Common::isUrlEncode($input)) { |
|
202 | + $web_data = Common::urlDecode($input); |
|
203 | + } |
|
204 | + |
|
205 | + if (Common::isJson($input)) { |
|
206 | + $web_data = json_decode($input, true); |
|
207 | + } |
|
208 | + |
|
209 | + $input = json_encode([ |
|
210 | + 'web_data' => $web_data, |
|
211 | + ]); |
|
212 | + } |
|
213 | + |
|
214 | + if (!Common::isJson($input)) { |
|
215 | + throw new TelegramException( |
|
216 | + 'Input is not a valid JSON string! Please check your code and try again.' |
|
217 | + ); |
|
218 | + } |
|
219 | + |
|
220 | + $input = json_decode($input, true); |
|
221 | + |
|
222 | + return new Update($input); |
|
223 | + } |
|
224 | + |
|
225 | + /** |
|
226 | + * Validate webapp data from is from Telegram |
|
227 | + * |
|
228 | + * @link https://core.telegram.org/bots/webapps#validating-data-received-via-the-web-app |
|
229 | + * |
|
230 | + * @param string $token The bot token |
|
231 | + * @param string $body The message body from getInput() |
|
232 | + * @return bool |
|
233 | + */ |
|
234 | + public static function validateWebData(string $token, string $body): bool |
|
235 | + { |
|
236 | + if (!Common::isJson($body)) { |
|
237 | + $raw_data = rawurldecode(str_replace('_auth=', '', $body)); |
|
238 | + $data = Common::urlDecode($raw_data); |
|
239 | + |
|
240 | + $data['user'] = urldecode($data['user']); |
|
241 | + |
|
242 | + } else { |
|
243 | + $data = json_decode($body, true); |
|
244 | + $data['user'] = json_encode($data['user']); |
|
245 | + } |
|
246 | + |
|
247 | + $data_check_string = "auth_date={$data['auth_date']}\nquery_id={$data['query_id']}\nuser={$data['user']}"; |
|
248 | + $secret_key = hash_hmac('sha256', $token, "WebAppData", true); |
|
249 | + |
|
250 | + return hash_hmac('sha256', $data_check_string, $secret_key) == $data['hash']; |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * Get the update from input |
|
255 | + * |
|
256 | + * @return Update|false |
|
257 | + */ |
|
258 | + public static function getUpdate(): Update|false |
|
259 | + { |
|
260 | + $input = self::getInput(); |
|
261 | + if (empty($input)) return false; |
|
262 | + return Telegram::processUpdate($input, self::getApiKey()); |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * Validate the token |
|
267 | + * |
|
268 | + * @param string $token (e.g. 123456789:ABC-DEF1234ghIkl-zyx57W2v1u123ew11) {digit}:{alphanumeric[34]} |
|
269 | + * @return bool |
|
270 | + */ |
|
271 | + public static function validateToken(string $token): bool |
|
272 | + { |
|
273 | + preg_match_all('/([0-9]+:[a-zA-Z0-9-_]+)/', $token, $matches); |
|
274 | + return count($matches[0]) == 1; |
|
275 | + } |
|
276 | + |
|
277 | + /** |
|
278 | + * Pass the update to the given webhook handler |
|
279 | + * |
|
280 | + * @param WebhookHandler $webhook_handler The webhook handler |
|
281 | + * @param ?Update $update By default, it will get the update from input |
|
282 | + * @return void |
|
283 | + */ |
|
284 | + public function fetchWith(WebhookHandler $webhook_handler, ?Update $update = null): void |
|
285 | + { |
|
286 | + if (is_subclass_of($webhook_handler, WebhookHandler::class)) { |
|
287 | + if ($update === null) $update = self::getUpdate(); |
|
288 | + $webhook_handler->resolve($update); |
|
289 | + } |
|
290 | + } |
|
291 | + |
|
292 | + /** |
|
293 | + * Get token from env file. |
|
294 | + * |
|
295 | + * @param string $file |
|
296 | + * @return ?string |
|
297 | + */ |
|
298 | + protected function getTokenFromEnvFile(string $file): ?string |
|
299 | + { |
|
300 | + if (!file_exists($file)) return null; |
|
301 | + return DotEnv::load($file)::get('TELEGRAM_API_KEY'); |
|
302 | + } |
|
303 | + |
|
304 | + /** |
|
305 | + * Debug mode |
|
306 | + * |
|
307 | + * @param ?int $admin_id Fill this or use setAdmin() |
|
308 | + * @return void |
|
309 | + */ |
|
310 | + public static function setDebugMode(?int $admin_id = null): void |
|
311 | + { |
|
312 | + error_reporting(E_ALL); |
|
313 | + ini_set('display_errors', 1); |
|
314 | + defined('DEBUG_MODE') or define('DEBUG_MODE', true); |
|
315 | + if ($admin_id) { |
|
316 | + defined('TG_ADMIN_ID') or define('TG_ADMIN_ID', $admin_id); |
|
317 | + } |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * Just another echo |
|
322 | + * |
|
323 | + * @param string $text |
|
324 | + * @return void |
|
325 | + */ |
|
326 | + public static function echo(string $text): void |
|
327 | + { |
|
328 | + echo $text; |
|
329 | + } |
|
330 | 330 | |
331 | 331 | } |
332 | 332 | \ No newline at end of file |
@@ -258,7 +258,9 @@ discard block |
||
258 | 258 | public static function getUpdate(): Update|false |
259 | 259 | { |
260 | 260 | $input = self::getInput(); |
261 | - if (empty($input)) return false; |
|
261 | + if (empty($input)) { |
|
262 | + return false; |
|
263 | + } |
|
262 | 264 | return Telegram::processUpdate($input, self::getApiKey()); |
263 | 265 | } |
264 | 266 | |
@@ -284,7 +286,9 @@ discard block |
||
284 | 286 | public function fetchWith(WebhookHandler $webhook_handler, ?Update $update = null): void |
285 | 287 | { |
286 | 288 | if (is_subclass_of($webhook_handler, WebhookHandler::class)) { |
287 | - if ($update === null) $update = self::getUpdate(); |
|
289 | + if ($update === null) { |
|
290 | + $update = self::getUpdate(); |
|
291 | + } |
|
288 | 292 | $webhook_handler->resolve($update); |
289 | 293 | } |
290 | 294 | } |
@@ -297,7 +301,9 @@ discard block |
||
297 | 301 | */ |
298 | 302 | protected function getTokenFromEnvFile(string $file): ?string |
299 | 303 | { |
300 | - if (!file_exists($file)) return null; |
|
304 | + if (!file_exists($file)) { |
|
305 | + return null; |
|
306 | + } |
|
301 | 307 | return DotEnv::load($file)::get('TELEGRAM_API_KEY'); |
302 | 308 | } |
303 | 309 |
@@ -25,142 +25,142 @@ |
||
25 | 25 | class TelegramLog |
26 | 26 | { |
27 | 27 | |
28 | - /** |
|
29 | - * Logger instance |
|
30 | - * |
|
31 | - * @var LoggerInterface |
|
32 | - */ |
|
33 | - protected static LoggerInterface $logger; |
|
34 | - |
|
35 | - /** |
|
36 | - * Logger instance for update |
|
37 | - * |
|
38 | - * @var LoggerInterface |
|
39 | - */ |
|
40 | - protected static LoggerInterface $update_logger; |
|
41 | - |
|
42 | - /** |
|
43 | - * Always log the request and response data to the debug log, also for successful requests |
|
44 | - * |
|
45 | - * @var bool |
|
46 | - */ |
|
47 | - public static bool $always_log_request_and_response = false; |
|
48 | - |
|
49 | - /** |
|
50 | - * Temporary stream handle for debug log |
|
51 | - * |
|
52 | - * @var resource|null |
|
53 | - */ |
|
54 | - protected static $debug_log_temp_stream_handle; |
|
55 | - |
|
56 | - /** |
|
57 | - * Remove bot token from debug stream |
|
58 | - * |
|
59 | - * @var bool |
|
60 | - */ |
|
61 | - public static bool $remove_bot_token = true; |
|
62 | - |
|
63 | - /** |
|
64 | - * Initialise logging. |
|
65 | - * |
|
66 | - * @param LoggerInterface|null $logger |
|
67 | - * @param LoggerInterface|null $update_logger |
|
68 | - */ |
|
69 | - public static function initialize(LoggerInterface $logger = null, LoggerInterface $update_logger = null): void |
|
70 | - { |
|
71 | - self::$logger = $logger ?: new NullLogger(); |
|
72 | - self::$update_logger = $update_logger ?: new NullLogger(); |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * Get the stream handle of the temporary debug output |
|
77 | - * |
|
78 | - * @return mixed The stream if debug is active, else false |
|
79 | - */ |
|
80 | - public static function getDebugLogTempStream(): mixed |
|
81 | - { |
|
82 | - if ((self::$debug_log_temp_stream_handle === null) && $temp_stream_handle = fopen('php://temp', 'wb+')) { |
|
83 | - self::$debug_log_temp_stream_handle = $temp_stream_handle; |
|
84 | - } |
|
85 | - |
|
86 | - return self::$debug_log_temp_stream_handle; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Write the temporary debug stream to log and close the stream handle |
|
91 | - * |
|
92 | - * @param string $message Message (with placeholder) to write to the debug log |
|
93 | - */ |
|
94 | - public static function endDebugLogTempStream(string $message = '%s'): void |
|
95 | - { |
|
96 | - if (is_resource(self::$debug_log_temp_stream_handle)) { |
|
97 | - rewind(self::$debug_log_temp_stream_handle); |
|
98 | - $stream_contents = stream_get_contents(self::$debug_log_temp_stream_handle); |
|
99 | - |
|
100 | - if (self::$remove_bot_token) { |
|
101 | - $stream_contents = preg_replace('/\/bot(\d+):[\w\-]+\//', '/botBOT_TOKEN_REMOVED/', $stream_contents); |
|
102 | - } |
|
103 | - |
|
104 | - self::debug(sprintf($message, $stream_contents)); |
|
105 | - fclose(self::$debug_log_temp_stream_handle); |
|
106 | - self::$debug_log_temp_stream_handle = null; |
|
107 | - } |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Handle any logging method call. |
|
112 | - * |
|
113 | - * @param string $name |
|
114 | - * @param array $arguments |
|
115 | - */ |
|
116 | - public static function __callStatic(string $name, array $arguments) |
|
117 | - { |
|
118 | - // Get the correct logger instance. |
|
119 | - $logger = null; |
|
120 | - self::initialize(); |
|
121 | - if (in_array($name, ['emergency', 'alert', 'critical', 'error', 'warning', 'notice', 'info', 'debug',], true)) { |
|
122 | - $logger = self::$logger; |
|
123 | - } elseif ($name === 'update') { |
|
124 | - $logger = self::$update_logger; |
|
125 | - $name = 'info'; |
|
126 | - } |
|
127 | - |
|
128 | - // Clearly we have no logging enabled. |
|
129 | - if ($logger === null) { |
|
130 | - return; |
|
131 | - } |
|
132 | - |
|
133 | - // Replace any placeholders from the passed context. |
|
134 | - if (count($arguments) >= 2) { |
|
135 | - $arguments[0] = self::interpolate($arguments[0], $arguments[1]); |
|
136 | - } |
|
137 | - |
|
138 | - call_user_func_array([$logger, $name], $arguments); |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Interpolates context values into the message placeholders. |
|
143 | - * |
|
144 | - * @see https://www.php-fig.org/psr/psr-3/#12-message |
|
145 | - * |
|
146 | - * @param string $message |
|
147 | - * @param array $context |
|
148 | - * |
|
149 | - * @return string |
|
150 | - */ |
|
151 | - protected static function interpolate(string $message, array $context = []): string |
|
152 | - { |
|
153 | - // Build a replacement array with braces around the context keys. |
|
154 | - $replace = []; |
|
155 | - foreach ($context as $key => $val) { |
|
156 | - // check that the value can be cast to string |
|
157 | - if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) { |
|
158 | - $replace["{{$key}}"] = $val; |
|
159 | - } |
|
160 | - } |
|
161 | - |
|
162 | - // Interpolate replacement values into the message and return. |
|
163 | - return strtr($message, $replace); |
|
164 | - } |
|
28 | + /** |
|
29 | + * Logger instance |
|
30 | + * |
|
31 | + * @var LoggerInterface |
|
32 | + */ |
|
33 | + protected static LoggerInterface $logger; |
|
34 | + |
|
35 | + /** |
|
36 | + * Logger instance for update |
|
37 | + * |
|
38 | + * @var LoggerInterface |
|
39 | + */ |
|
40 | + protected static LoggerInterface $update_logger; |
|
41 | + |
|
42 | + /** |
|
43 | + * Always log the request and response data to the debug log, also for successful requests |
|
44 | + * |
|
45 | + * @var bool |
|
46 | + */ |
|
47 | + public static bool $always_log_request_and_response = false; |
|
48 | + |
|
49 | + /** |
|
50 | + * Temporary stream handle for debug log |
|
51 | + * |
|
52 | + * @var resource|null |
|
53 | + */ |
|
54 | + protected static $debug_log_temp_stream_handle; |
|
55 | + |
|
56 | + /** |
|
57 | + * Remove bot token from debug stream |
|
58 | + * |
|
59 | + * @var bool |
|
60 | + */ |
|
61 | + public static bool $remove_bot_token = true; |
|
62 | + |
|
63 | + /** |
|
64 | + * Initialise logging. |
|
65 | + * |
|
66 | + * @param LoggerInterface|null $logger |
|
67 | + * @param LoggerInterface|null $update_logger |
|
68 | + */ |
|
69 | + public static function initialize(LoggerInterface $logger = null, LoggerInterface $update_logger = null): void |
|
70 | + { |
|
71 | + self::$logger = $logger ?: new NullLogger(); |
|
72 | + self::$update_logger = $update_logger ?: new NullLogger(); |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * Get the stream handle of the temporary debug output |
|
77 | + * |
|
78 | + * @return mixed The stream if debug is active, else false |
|
79 | + */ |
|
80 | + public static function getDebugLogTempStream(): mixed |
|
81 | + { |
|
82 | + if ((self::$debug_log_temp_stream_handle === null) && $temp_stream_handle = fopen('php://temp', 'wb+')) { |
|
83 | + self::$debug_log_temp_stream_handle = $temp_stream_handle; |
|
84 | + } |
|
85 | + |
|
86 | + return self::$debug_log_temp_stream_handle; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Write the temporary debug stream to log and close the stream handle |
|
91 | + * |
|
92 | + * @param string $message Message (with placeholder) to write to the debug log |
|
93 | + */ |
|
94 | + public static function endDebugLogTempStream(string $message = '%s'): void |
|
95 | + { |
|
96 | + if (is_resource(self::$debug_log_temp_stream_handle)) { |
|
97 | + rewind(self::$debug_log_temp_stream_handle); |
|
98 | + $stream_contents = stream_get_contents(self::$debug_log_temp_stream_handle); |
|
99 | + |
|
100 | + if (self::$remove_bot_token) { |
|
101 | + $stream_contents = preg_replace('/\/bot(\d+):[\w\-]+\//', '/botBOT_TOKEN_REMOVED/', $stream_contents); |
|
102 | + } |
|
103 | + |
|
104 | + self::debug(sprintf($message, $stream_contents)); |
|
105 | + fclose(self::$debug_log_temp_stream_handle); |
|
106 | + self::$debug_log_temp_stream_handle = null; |
|
107 | + } |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Handle any logging method call. |
|
112 | + * |
|
113 | + * @param string $name |
|
114 | + * @param array $arguments |
|
115 | + */ |
|
116 | + public static function __callStatic(string $name, array $arguments) |
|
117 | + { |
|
118 | + // Get the correct logger instance. |
|
119 | + $logger = null; |
|
120 | + self::initialize(); |
|
121 | + if (in_array($name, ['emergency', 'alert', 'critical', 'error', 'warning', 'notice', 'info', 'debug',], true)) { |
|
122 | + $logger = self::$logger; |
|
123 | + } elseif ($name === 'update') { |
|
124 | + $logger = self::$update_logger; |
|
125 | + $name = 'info'; |
|
126 | + } |
|
127 | + |
|
128 | + // Clearly we have no logging enabled. |
|
129 | + if ($logger === null) { |
|
130 | + return; |
|
131 | + } |
|
132 | + |
|
133 | + // Replace any placeholders from the passed context. |
|
134 | + if (count($arguments) >= 2) { |
|
135 | + $arguments[0] = self::interpolate($arguments[0], $arguments[1]); |
|
136 | + } |
|
137 | + |
|
138 | + call_user_func_array([$logger, $name], $arguments); |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Interpolates context values into the message placeholders. |
|
143 | + * |
|
144 | + * @see https://www.php-fig.org/psr/psr-3/#12-message |
|
145 | + * |
|
146 | + * @param string $message |
|
147 | + * @param array $context |
|
148 | + * |
|
149 | + * @return string |
|
150 | + */ |
|
151 | + protected static function interpolate(string $message, array $context = []): string |
|
152 | + { |
|
153 | + // Build a replacement array with braces around the context keys. |
|
154 | + $replace = []; |
|
155 | + foreach ($context as $key => $val) { |
|
156 | + // check that the value can be cast to string |
|
157 | + if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) { |
|
158 | + $replace["{{$key}}"] = $val; |
|
159 | + } |
|
160 | + } |
|
161 | + |
|
162 | + // Interpolate replacement values into the message and return. |
|
163 | + return strtr($message, $replace); |
|
164 | + } |
|
165 | 165 | |
166 | 166 | } |
@@ -118,7 +118,7 @@ |
||
118 | 118 | // Get the correct logger instance. |
119 | 119 | $logger = null; |
120 | 120 | self::initialize(); |
121 | - if (in_array($name, ['emergency', 'alert', 'critical', 'error', 'warning', 'notice', 'info', 'debug',], true)) { |
|
121 | + if (in_array($name, ['emergency', 'alert', 'critical', 'error', 'warning', 'notice', 'info', 'debug', ], true)) { |
|
122 | 122 | $logger = self::$logger; |
123 | 123 | } elseif ($name === 'update') { |
124 | 124 | $logger = self::$update_logger; |