44use GuzzleHttp \Client ;
55use GuzzleHttp \Promise ;
66use Spatie \Regex \Regex ;
7+ use Amp \Loop ;
8+
79class Boting {
810 public $ Token = "" ;
911 public $ Client ;
@@ -22,24 +24,32 @@ public function __construct() {
2224 }
2325
2426 public function getUpdates () {
25- $ Request = $ this ->Client ->getAsync ('getUpdates?timeout=10&offset= ' . $ this ->Offset , ["verify " => false ])->wait ();
27+ if ($ this ->WebHook == false ) {
28+ $ Request = $ this ->Client ->getAsync ('getUpdates?timeout=10&offset= ' . $ this ->Offset , ["verify " => false ])->wait ();
29+ $ Body = $ Request ->getBody ()->getContents ();
30+
31+ if ($ Request ->getStatusCode () == "409 " ) {
32+ echo "\nInvalid token \n" ;
33+ die ();
34+ }
2635
27- if ($ Request ->getStatusCode () == "409 " ) {
28- echo "\nInvalid token \n" ;
29- die ();
36+ $ sonuc = json_decode ($ Body , true )["result " ];
37+ if (!is_array ($ sonuc )) echo $ sonuc ;
38+ if (count ($ sonuc ) >= 1 ) {
39+ $ sonuc = array_reverse ($ sonuc );
40+ if ($ sonuc [0 ]["update_id " ] > $ this ->LatUpdate ) {
41+ $ this ->LatUpdate = $ sonuc [0 ]["update_id " ];
42+ $ this ->Offset = $ sonuc [0 ]["update_id " ] + 1 ;
43+ return $ sonuc ;
44+ } else {
45+ return false ;
46+ }
47+ }
48+ } else {
49+ $ Body = file_get_contents ("php://input " );
50+ return json_decode ($ Body , true );
3051 }
31- $ sonuc = json_decode ($ Request ->getBody ()->getContents (), true )["result " ];
32- if (!is_array ($ sonuc )) echo $ sonuc ;
33- if (count ($ sonuc ) >= 1 ) {
34- $ sonuc = array_reverse ($ sonuc );
35- if ($ sonuc [0 ]["update_id " ] > $ this ->LatUpdate ) {
36- $ this ->LatUpdate = $ sonuc [0 ]["update_id " ];
37- $ this ->Offset = $ sonuc [0 ]["update_id " ] + 1 ;
38- return $ sonuc ;
39- } else {
40- return false ;
41- }
42- }
52+
4353 }
4454
4555 public function downloadFile ($ FileId , $ fileName = NULL ) {
@@ -182,27 +192,48 @@ public function answer($query, $callback) {
182192 }
183193 }
184194
185- public function handler ($ Token , $ CallBack = NULL ) {
195+ public function handler ($ Token , $ WebHook = false , $ CallBack = NULL ) {
186196 $ this ->Token = $ Token ;
187- $ this ->Client = new Client (["base_uri " => "https://api.telegram.org " . "/bot " . $ Token . "/ " ]);
188- while (True ) {
197+ if ($ WebHook == true ) {
198+ echo 0 ;
199+ $ this ->WebHook = true ;
200+ $ this ->Client = new Client (["base_uri " => "https://api.telegram.org " . "/bot " . $ Token . "/ " ]);
201+ $ this ->hookClient = new Client ();
189202 $ Update = $ this ->getUpdates ();
190- if ($ Update != false ) {
191- foreach ($ Update as $ Up ) {
192- if ($ CallBack === NULL ) {
193- $ Command = $ this ->isCommand ($ Up );
194- if ($ Command !== false ) {
195- if (count ($ Command ) === 2 ) {
196- $ Command [0 ]($ Up , $ Command [1 ]);
197- } else {
198- $ Command [0 ]($ Up );
199- }
200- }
203+ if ($ CallBack === NULL ) {
204+ $ Command = $ this ->isCommand ($ Update );
205+ if ($ Command !== false ) {
206+ if (count ($ Command ) === 2 ) {
207+ $ Command [0 ]($ Update , $ Command [1 ]);
201208 } else {
202- $ CallBack ( $ Up );
209+ $ Command [ 0 ]( $ Update );
203210 }
204- }
211+ }
212+ } else {
213+ $ CallBack ($ Update );
205214 }
215+ } else {
216+ $ this ->WebHook = false ;
217+ $ this ->Client = new Client (["base_uri " => "https://api.telegram.org " . "/bot " . $ Token . "/ " ]);
218+ while (True ) {
219+ $ Update = $ this ->getUpdates ();
220+ if ($ Update != false ) {
221+ foreach ($ Update as $ Up ) {
222+ if ($ CallBack === NULL ) {
223+ $ Command = $ this ->isCommand ($ Up );
224+ if ($ Command !== false ) {
225+ if (count ($ Command ) === 2 ) {
226+ $ Command [0 ]($ Up , $ Command [1 ]);
227+ } else {
228+ $ Command [0 ]($ Up );
229+ }
230+ }
231+ } else {
232+ $ CallBack ($ Up );
233+ }
234+ }
235+ }
236+ }
206237 }
207- }
208- }
238+ }
239+ }
0 commit comments