33use GuzzleHttp \Client ;
44use Spatie \Regex \Regex ;
55
6- class Exception extends \Exception
7- {
6+ class Exception extends \Exception {
87 private $ Message = '' ;
98 private $ Description = '' ;
109
11- public function __construct ($ Message , $ Code = 0 , \Exception $ Previous = null , $ Error = '' , $ Description = '' )
12- {
10+ public function __construct ($ Message , $ Code = 0 , \Exception $ Previous = null , $ Error = '' , $ Description = '' ) {
1311 $ this ->Code = $ Code ;
1412 $ this ->Message = $ Error ;
1513 $ this ->Description = $ Description ;
1614 parent ::__construct ($ Message , $ Code , $ Previous );
1715 }
1816
19- public function getErrorCode (): string
20- {
17+ public function getErrorCode (): string {
2118 return $ this ->Code ;
2219 }
2320
24- public function getErrorDescription (): string
25- {
21+ public function getErrorDescription (): string {
2622 return $ this ->Description ;
2723 }
2824
29- public function __toString ()
30- {
25+ public function __toString () {
3126 return 'Telegram returned an error: ' . $ this ->Description .PHP_EOL .'Backtrace: ' .PHP_EOL .$ this ->getTraceAsString ();
3227 }
3328}
@@ -47,26 +42,33 @@ public function __construct() {
4742 $ this ->Type = ["animation " , "audio " , "document " , "photo " , "sticker " , "video " , "video_note " , "voice " , "contact " , "dice " , "game " , "poll " , "venue " , "location " , "new_chat_members " , "left_chat_member " , "new_chat_title " , "new_chat_photo " , "delete_chat_photo " , "group_chat_created " , "supergroup_chat_created " , "pinned_message " , "text " ];
4843 $ this ->Types = [];
4944 $ this ->Request = [];
50-
45+ $ this -> errorHandler = false ;
5146 }
5247
5348 public function getUpdates () {
5449 if ($ this ->WebHook == false ) {
5550 $ Request = $ this ->Client ->getAsync ('getUpdates?timeout=10&offset= ' . $ this ->Offset , ["verify " => false ])->wait ();
5651 $ Body = $ Request ->getBody ()->getContents ();
5752
58- $ sonuc = json_decode ($ Body , true )["result " ];
59- if (!is_array ($ sonuc )) echo $ sonuc ;
60- if (count ($ sonuc ) >= 1 ) {
61- $ sonuc = array_reverse ($ sonuc );
62- if ($ sonuc [0 ]["update_id " ] > $ this ->LatUpdate ) {
63- $ this ->LatUpdate = $ sonuc [0 ]["update_id " ];
64- $ this ->Offset = $ sonuc [0 ]["update_id " ] + 1 ;
65- return $ sonuc ;
66- } else {
67- return false ;
53+ $ sonuc = json_decode ($ Body , true );
54+ if ($ sonuc ["ok " ] === false ) {
55+ if ($ sonuc ["error_code " ] === 401 ) {
56+ throw new Exception ("ERROR_ " . $ sonuc ["error_code " ], $ sonuc ["error_code " ], NULL , "Invalid Bot Token " , "Invalid Bot Token " );
6857 }
69- }
58+ } else {
59+ $ sonuc = $ sonuc ["result " ];
60+ if (!is_array ($ sonuc )) return ;
61+ if (count ($ sonuc ) >= 1 ) {
62+ $ sonuc = array_reverse ($ sonuc );
63+ if ($ sonuc [0 ]["update_id " ] > $ this ->LatUpdate ) {
64+ $ this ->LatUpdate = $ sonuc [0 ]["update_id " ];
65+ $ this ->Offset = $ sonuc [0 ]["update_id " ] + 1 ;
66+ return $ sonuc ;
67+ } else {
68+ return false ;
69+ }
70+ }
71+ }
7072 } else {
7173 $ Body = file_get_contents ("php://input " );
7274 return json_decode ($ Body , true );
@@ -92,13 +94,21 @@ public function downloadFile($FileId, $fileName = NULL) {
9294 return $ fileName ;
9395 }
9496
97+ public function catch ($ function ) {
98+ return $ this ->errorHandler = [$ function ];
99+ }
100+
95101
96102 public function __call ($ method , $ args ) {
97103 $ Request = $ this ->Client ->postAsync ($ method , ["form_params " => $ args [0 ]])->wait ();
98104 $ Json = json_decode ($ Request ->getBody ()->getContents (), true );
99105
100106 if ($ Json ["ok " ] === false ) {
101- throw new Exception ("ERROR_ " . $ Json ["error_code " ], $ Json ["error_code " ], NULL , $ Json ["description " ], $ Json ["description " ]);
107+ if ($ this ->errorHandler === false ) {
108+ throw new Exception ("ERROR_ " . $ Json ["error_code " ], $ Json ["error_code " ], NULL , $ Json ["description " ], $ Json ["description " ]);
109+ } else {
110+ $ this ->errorHandler [0 ](new Exception ("ERROR_ " . $ Json ["error_code " ], $ Json ["error_code " ], NULL , $ Json ["description " ], $ Json ["description " ]));
111+ }
102112 } else {
103113 return $ Json ;
104114 }
0 commit comments