Skip to content

Commit af41500

Browse files
committed
boting: Version 2.5
1 parent f411a5d commit af41500

3 files changed

Lines changed: 108 additions & 41 deletions

File tree

README-tr.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ _Boting_, PHP ile hızlı ve asenkron bot yazmanız için en iyi Telegram Bot k
1313
* Her zaman en son BotAPI'ye uygun
1414
* Tek dosya, boyutu küçük, yüklenmesi basit.
1515
* Aynı anda birden fazla komut çalıştırabilirsiniz
16-
* Şu anlık sadece getUpdates methodu ile çalışmaktadır. İleri ki sürümler de webhook desteği gelecektir.
16+
* WebHook & GetUpdates desteği.
1717
## Gereksinimler
1818
Eğer [Guzzle](http://docs.guzzlephp.org/en/stable/overview.html#requirements) yükleyebiliyorsanız rahatlıkla kullanabilirsiniz.
1919

@@ -32,6 +32,24 @@ composer require quiec/boting:dev-master
3232

3333
Eğer Composer yüklü değilse, [bu adresten](https://getcomposer.org/download/) kolaylıkla yükleyebilirsiniz.
3434

35+
## Update Alma
36+
İki yol ile ile Update alabilirsiniz;
37+
38+
### Webhook
39+
Webhook yöntemi ile Update'leri alacak iseniz handler'e "true" eklemeniz yeterli.
40+
41+
```php
42+
...
43+
$Bot->Handler("Token", true);
44+
```
45+
### Get Updates
46+
Default olarak bu yöntem kullanılmaktadır. Ekstradan bir şey eklemenize gerek yoktur.
47+
```php
48+
...
49+
$Bot->Handler("Token");
50+
```
51+
52+
3553
## Olaylar
3654
Boting 2.0 eklenen yeni özellikle artık kolaylık komut ekleyebilir, `on` ile mesaj türlerini yakalayabilirsiniz.
3755
### $bot->command
@@ -86,6 +104,7 @@ $Main = function ($Update) {...};
86104
$Bot->Handler("Token", $Main);
87105
```
88106

107+
❗️Webhook kullanacaksanız `true`, GetUpdates ile alacaksanız `false` yazın.
89108
**Örnek** (_/start mesajına karşılık veren bir fonksiyon_):
90109
```php
91110
<?php
@@ -103,7 +122,7 @@ $Main = function ($Update) use ($Bot) { // We create a function called Main.
103122
}
104123
}
105124
};
106-
$Bot->Handler("Here ur bot token", $Main); // We define our bot token and function.
125+
$Bot->Handler("Here ur bot token", false, $Main); // We define our bot token and function.
107126
```
108127

109128
Daha fazla örnek için [bu dosyaya](https://github.com/Quiec/Boting/blob/master/example.php) bir göz atın.

README.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ _Boting_, The best Telegram Bot library for fast and asynchronous bot with PHP.
1414
* Single file, small size, simple to upload.
1515
* File download
1616
* Events
17-
* Currently works only with the getUpdates method. Webhook support will be added in future releases.
17+
* WebHook & GetUpdates support
1818
## Requirements
1919
If you can install [Guzzle](http://docs.guzzlephp.org/en/stable/overview.html#requirements), you can use it easily.
2020

@@ -33,6 +33,23 @@ composer require quiec/boting:dev-master
3333

3434
If Composer is not installed, you can easily install it [Here](https://getcomposer.org/download/).
3535

36+
## Get Update
37+
You can get Update with two ways;
38+
39+
### Webhook
40+
If you are going to receive Updates with Webhook method, just add "true" to the handler.
41+
42+
```php
43+
...
44+
$Bot->Handler("Token", true);
45+
```
46+
### Get Updates
47+
This method is used by default. You don't need to add anything extra.
48+
```php
49+
...
50+
$Bot->Handler("Token");
51+
```
52+
3653
## Events
3754
With the new feature added to Boting 2.0, you can now add convenience commands and capture message types with `on`.
3855
### $bot->command
@@ -82,9 +99,11 @@ $Bot->answer("inline_query", function ($Update) use ($Bot) {
8299

83100
### Special Events
84101
If you do not want to use ready-made functions, you can define your own function.
102+
103+
❗️Type `true` if you are going to use Webhook or `false` if you will get it with GetUpdates.
85104
```php
86105
$Main = function ($Update) {...};
87-
$Bot->Handler("Token", $Main);
106+
$Bot->Handler("Token", false, $Main);
88107
```
89108

90109
**Example** (_A function that responds to the /start message_):
@@ -104,10 +123,8 @@ $Main = function ($Update) use ($Bot) { // We create a function called Main.
104123
}
105124
}
106125
};
107-
$Bot->Handler("Here ur bot token", $Main); // We define our bot token and function.
126+
$Bot->Handler("Here ur bot token", false, $Main); // We define our bot token and function.
108127
```
109-
110-
Take a look at [this file](https://github.com/Quiec/Boting/blob/master/example.php) for more examples.
111128
## Commands
112129
Commands are the same as [BotAPI](https://core.telegram.org/bots/api) commands. You can use BotAPI commands in the same way.
113130

src/Boting/Boting.php

Lines changed: 65 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
use GuzzleHttp\Client;
55
use GuzzleHttp\Promise;
66
use Spatie\Regex\Regex;
7+
use Amp\Loop;
8+
79
class 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

Comments
 (0)