33namespace TheCoder \MonologTelegram ;
44
55use GuzzleHttp \Client ;
6+ use GuzzleHttp \Exception \GuzzleException ;
67use Illuminate \Routing \Router ;
78use Illuminate \Support \Facades \Route ;
89use Monolog \Handler \AbstractProcessingHandler ;
@@ -67,7 +68,7 @@ public function __construct(
6768 string $ token ,
6869 string $ chat_id ,
6970 ?string $ topic_id = null ,
70- $ topics_level ,
71+ $ topics_level = [] ,
7172 $ level = Logger::DEBUG ,
7273 bool $ bubble = true ,
7374 $ bot_api = 'https://api.telegram.org/bot ' ,
@@ -112,9 +113,13 @@ private function truncateTextToTelegramLimit(string $textMessage): string
112113 /**
113114 * Send request to @link https://api.telegram.org/bot on SendMessage action.
114115 * @param string $message
116+ * @param null $token
117+ * @param null $chatId
118+ * @param null $topicId
115119 * @param array $option
120+ * @throws GuzzleException
116121 */
117- protected function send (string $ message , $ token = null , $ chatId = null , $ topicId = null , $ option = []): void
122+ protected function send (string $ message , $ token = null , $ chatId = null , $ topicId = null , array $ option = []): void
118123 {
119124 try {
120125
@@ -151,11 +156,10 @@ protected function send(string $message, $token = null, $chatId = null, $topicId
151156
152157 $ response = $ httpClient ->post ($ url , $ options );
153158 } catch (\Exception $ e ) {
154-
159+ $ a = 1 ;
155160 }
156161 }
157162
158-
159163 protected function getTopicByAttribute (): string |null
160164 {
161165 $ route = Route::current ();
@@ -169,25 +173,75 @@ protected function getTopicByAttribute(): string|null
169173 return null ;
170174 }
171175
172- try {
176+ $ topicId = $ this ->getTopicIdByReflection ($ action );
177+ if ($ topicId === false ) {
178+ $ topicId = $ this ->getTopicIdByRegex ($ action );
179+ }
180+
181+ return $ topicId ;
182+ }
173183
184+ protected function getTopicIdByReflection ($ action ): bool |string |null
185+ {
186+ try {
174187 [$ controller , $ method ] = explode ('@ ' , $ action ['controller ' ]);
175188 $ reflectionMethod = new ReflectionMethod ($ controller , $ method );
176189
177190 $ attributes = $ reflectionMethod ->getAttributes ();
191+ $ attributes [0 ]?->newInstance() ?? null ;
178192
179- if (empty ($ attributes )) {
180- return null ;
193+ if ($ attributes [0 ] !== null ) {
194+ /** @var TopicLogInterface $notifyException */
195+ $ notifyException = $ attributes [0 ]->newInstance () ?? null ;
196+ return $ notifyException ->getTopicId ($ this ->topicsLevel );
181197 }
182198
183- /** @var TopicLogInterface $notifyException */
184- $ notifyException = $ attributes [0 ]->newInstance ();
199+ } catch (\Throwable $ e ) {
185200
186- return $ notifyException ->getTopicId ($ this ->topicsLevel );
201+ }
202+ return false ;
203+ }
187204
188- } catch (\Throwable ) {
189- return null ;
205+ protected function getTopicIdByRegex ($ action )
206+ {
207+ try {
208+ // if reflection coud not get attribute use reagex instead
209+ [$ controller , $ method ] = explode ('@ ' , $ action ['controller ' ]);
210+
211+ $ filePath = base_path (str_replace ('App ' , 'app ' , $ controller ) . '.php ' );
212+ $ fileContent = file_get_contents ($ filePath );
213+ $ allAttributes = [];
214+
215+ // Regex to match attributes and methods
216+ $ regex = '/\#\[\s*(.*?)\s*\]\s*public\s*function\s*(\w+)/ ' ;
217+ if (preg_match_all ($ regex , $ fileContent , $ matches , PREG_SET_ORDER )) {
218+ foreach ($ matches as $ match ) {
219+ $ attributeString = $ match [1 ];
220+ $ methodName = $ match [2 ];
221+
222+ $ attributes = array_map ('trim ' , explode (', ' , $ attributeString ));
223+ foreach ($ attributes as $ attribute ) {
224+ $ attributeName = preg_replace ('/\(.*/ ' , '' , $ attribute );
225+ $ allAttributes [$ methodName ][] = $ attributeName ;
226+ }
227+ }
228+ }
229+
230+ if (empty ($ allAttributes )) {
231+ return null ;
232+ }
233+
234+ if (isset ($ allAttributes [$ method ][0 ])) {
235+ foreach ($ this ->topicsLevel as $ key => $ _topicLevel ) {
236+ if (str_contains ($ key , $ allAttributes [$ method ][0 ])) {
237+ return $ _topicLevel ;
238+ }
239+ }
240+ }
241+
242+ } catch (\Throwable $ e ) {
190243 }
244+ return null ;
191245 }
192246
193247 public function setToken (string $ token ): static
0 commit comments