Skip to content

Commit 4e5afb8

Browse files
committed
boting: Version 2.0
1 parent d138288 commit 4e5afb8

5 files changed

Lines changed: 374 additions & 77 deletions

File tree

README-tr.md

Lines changed: 88 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,85 @@ composer require quiec/boting:dev-master
3131
```
3232

3333
Eğer Composer yüklü değilse, [bu adresten](https://getcomposer.org/download/) kolaylıkla yükleyebilirsiniz.
34+
35+
## Olaylar
36+
Boting 2.0 eklenen yeni özellikle artık kolaylık komut ekleyebilir, `on` ile mesaj türlerini yakalayabilirsiniz.
37+
### $bot->command
38+
Komut, **mutlaka regex olmalıdır.**
39+
40+
**Örnek** (_/start komutunu yakalayalım_):
41+
42+
```php
43+
$Bot->command("/\/start/m", function ($Update, $Match) use ($Bot) {
44+
$ChatId = $Update["message"]["chat"]["id"];
45+
$Bot->sendMessage(["chat_id" => $ChatId, "text" => "Started bot."]);
46+
});
47+
```
48+
**Başka komut handler'i ekleyelim:**
49+
```php
50+
$Bot->command("/[!.\/]start/m", function ($Update, $Match) use ($Bot) {
51+
$ChatId = $Update["message"]["chat"]["id"];
52+
$Bot->sendMessage(["chat_id" => $ChatId, "text" => "Started bot."]);
53+
});
54+
```
55+
Bot artık `/start, !start, .start` komutlarına da yanıt verecektir.
56+
57+
### $bot->on
58+
Bot belirtilen türden bir mesaj gelirse fonksiyonu çalıştıracaktır.
59+
60+
**On'da match kullanılmamaktadır.**
61+
62+
**Örnek** (_fotoğraf gelirse_):
63+
```php
64+
$Bot->on("photo", function ($Update) use ($Bot) {
65+
$ChatId = $Update["message"]["chat"]["id"];
66+
$Bot->sendMessage(["chat_id" => $ChatId, "text" => "Fotoğraf geldi"]);
67+
});
68+
```
69+
On Türlerine [buradan](https://core.telegram.org/bots/api#message) bakabilirsiniz.
70+
71+
### $bot->answer
72+
`inline_query` veya `callback_query` yanıt vermek için answer fonksiyonunu kullanabilirsiniz.
73+
74+
**Örnek** (_Inline yanıt verelim_):
75+
```php
76+
$Bot->answer("inline_query", function ($Update) use ($Bot) {
77+
$Bir = ["type" => "article", "id" => 0, "title" => "test", "input_message_content" => ["message_text" => "This bot created by Boting..."]];
78+
$Bot->answerInlineQuery(["inline_query_id" => $Update["inline_query"]["id"], "results" => json_encode([$Bir])]);
79+
});
80+
```
81+
82+
### Özel Events
83+
Hazır fonksiyonları kullanmak istemiyorsanız, kendi fonksiyonunuzu tanımlayabilirsiniz.
84+
```php
85+
$Main = function ($Update) {...};
86+
$Bot->Handler("Token", $Main);
87+
```
88+
89+
**Örnek** (_/start mesajına karşılık veren bir fonksiyon_):
90+
```php
91+
<?php
92+
require __DIR__ . '/vendor/autoload.php'; //We include the base of the bot.
93+
use Boting\Boting; // We say we want to use the base.
94+
95+
$Main = function ($Bot, $Update) { // We create a function called Main.
96+
if (!empty($Update["message"])) { // We check if a message has arrived.
97+
$Mesaj = $Update["message"]["text"]; // We throw the message into the variable.
98+
$ChatId = $Update["message"]["chat"]["id"]; // We get the chat id to send messages.
99+
100+
if ($Mesaj === "/start") { // We check if the message is start.
101+
$Bot->sendMessage(["chat_id" => $ChatId, "text" => "Bot'u başlattınız."]); // We use the sendMessage function.
102+
}
103+
}
104+
};
105+
106+
107+
$Bot = new Boting(); // We start the base.
108+
$Bot->Handler("Here ur bot token", $Main); // We define our bot token and function.
109+
```
110+
111+
Daha fazla örnek için [bu dosyaya](https://github.com/Quiec/Boting/blob/master/example.php) bir göz atın.
112+
34113
## Komut Çağırma
35114
Komutlar [BotAPI](https://core.telegram.org/bots/api) komutları ile aynı. BotAPI komutları aynı şekilde kullanabilirsiniz.
36115
Örnek verelim mesaj göndermek istediniz, [BotAPI](https://core.telegram.org/bots/api#sendmessage)'den gerekli parametrelere bakıyoruz.
@@ -51,23 +130,15 @@ Ayrıca basit bir `/start` mesajına yanıt veren bir kod:
51130

52131
```php
53132
<?php
54-
require __DIR__ . '/vendor/autoload.php'; // Bot'un tabanını dahil ediyoruz.
55-
use Boting\Boting; // Tabanı kullanmak istediğimizi söylüyoruz.
56-
57-
$Ana = function ($Bot, $Update) { // Ana diye bir fonksiyon oluşturuyoruz.
58-
if (!empty($Update["message"])) { // Mesaj mı geldi diye kontrol ediyoruz.
59-
$Mesaj = $Update["message"]["text"]; // Mesajı değişkene atıyoruz.
60-
$ChatId = $Update["message"]["chat"]["id"]; // Mesaj göndermek için sohbet id'isini alıyoruz.
61-
62-
if ($Mesaj === "/start") { // Mesajın start olup olmadığını kontrol ediyoruz.
63-
$Bot->sendMessage(["chat_id" => $ChatId, "text" => "Bot'u başlattınız."]); // sendMessage fonksiyonu kullanıyoruz.
64-
}
65-
}
66-
};
67-
68-
69-
$Bot = new Boting(); // Tabanı başlatıyoruz.
70-
$Bot->Handler("Buraya Bot Tokeniniz", $Ana); // Bot tokenimizi ve fonksiyonumuzu tanımlıyoruz.
133+
require __DIR__ . '/vendor/autoload.php'; //We include the base of the bot.
134+
use Boting\Boting; // We say we want to use the base.
135+
136+
$Bot = new Boting(); // We start the base.
137+
$Bot->command("/[!.\/]start/m", function ($Update, $Match) use ($Bot) {
138+
$ChatId = $Update["message"]["chat"]["id"];
139+
$Bot->sendMessage(["chat_id" => $ChatId, "text" => "Started bot."]);
140+
});
141+
$Bot->Handler("Here ur bot token"); // We define our bot token.
71142
```
72143

73144
## Lisans

README.md

Lines changed: 86 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
![](https://img.shields.io/packagist/dt/quiec/boting) ![](https://img.shields.io/packagist/l/quiec/boting) ![](https://img.shields.io/packagist/php-v/quiec/boting) ![](https://img.shields.io/packagist/v/quiec/boting)
77

88

9-
_Boting_, The best Telegram Bot library for fast and asynchronous bot typing with PHP.
9+
_Boting_, The best Telegram Bot library for fast and asynchronous bot with PHP.
1010

1111
## Features
1212
* %100 Async (😳)
1313
* Always compatible with the latest BotAPI
1414
* Single file, small size, simple to upload.
15-
* You can run multiple commands at the same time.
15+
* File download
16+
* Events
1617
* Currently works only with the getUpdates method. Webhook support will be added in future releases.
1718
## Requirements
1819
If you can install [Guzzle](http://docs.guzzlephp.org/en/stable/overview.html#requirements), you can use it easily.
@@ -31,24 +32,62 @@ composer require quiec/boting:dev-master
3132
```
3233

3334
If Composer is not installed, you can easily install it [Here](https://getcomposer.org/download/).
34-
## Commands
35-
Commands are the same as [BotAPI](https://core.telegram.org/bots/api) commands. You can use BotAPI commands in the same way.
36-
Let's give an example you wanted to send a message,We look at the required parameters from [BotAPI](https://core.telegram.org/bots/api#sendmessage).
3735

38-
<img src="https://i.hizliresim.com/CVaBQE.png" width=600 height=300>
36+
## Events
37+
Boting 2.0 eklenen yeni özellikle artık kolaylık komut ekleyebilir, `on` ile mesaj türlerini yakalayabilirsiniz.
38+
### $bot->command
39+
Komut, **mutlaka regex olmalıdır.**
3940

40-
We need `chat_id` and` text`. So let's write our code.
41+
**Örnek** (_/start komutunu yakalayalım_):
4142

4243
```php
43-
$Bot->sendMessage(["chat_id" => "@fusuf", "text" => "Hello!"]);
44+
$Bot->command("/\/start/m", function ($Update, $Match) use ($Bot) {
45+
$ChatId = $Update["message"]["chat"]["id"];
46+
$Bot->sendMessage(["chat_id" => $ChatId, "text" => "Started bot."]);
47+
});
4448
```
49+
**Başka komut handler'i ekleyelim:**
50+
```php
51+
$Bot->command("/[!.\/]start/m", function ($Update, $Match) use ($Bot) {
52+
$ChatId = $Update["message"]["chat"]["id"];
53+
$Bot->sendMessage(["chat_id" => $ChatId, "text" => "Started bot."]);
54+
});
55+
```
56+
Bot artık `/start, !start, .start` komutlarına da yanıt verecektir.
4557

46-
The process is complete.
58+
### $bot->on
59+
Bot belirtilen türden bir mesaj gelirse fonksiyonu çalıştıracaktır.
4760

48-
## Examples
49-
We can show [this file](https://github.com/Quiec/Boting/blob/master/example.php) as a very good example of using the library.
50-
Also a code that responds to a simple `/start` message:
61+
**On'da match kullanılmamaktadır.**
5162

63+
**Örnek** (_fotoğraf gelirse_):
64+
```php
65+
$Bot->on("photo", function ($Update) use ($Bot) {
66+
$ChatId = $Update["message"]["chat"]["id"];
67+
$Bot->sendMessage(["chat_id" => $ChatId, "text" => "Fotoğraf geldi"]);
68+
});
69+
```
70+
On Türlerine [buradan](https://core.telegram.org/bots/api#message) bakabilirsiniz.
71+
72+
### $bot->answer
73+
`inline_query` veya `callback_query` yanıt vermek için answer fonksiyonunu kullanabilirsiniz.
74+
75+
**Örnek** (_Inline yanıt verelim_):
76+
```php
77+
$Bot->answer("inline_query", function ($Update) use ($Bot) {
78+
$Bir = ["type" => "article", "id" => 0, "title" => "test", "input_message_content" => ["message_text" => "This bot created by Boting..."]];
79+
$Bot->answerInlineQuery(["inline_query_id" => $Update["inline_query"]["id"], "results" => json_encode([$Bir])]);
80+
});
81+
```
82+
83+
### Özel Events
84+
Hazır fonksiyonları kullanmak istemiyorsanız, kendi fonksiyonunuzu tanımlayabilirsiniz.
85+
```php
86+
$Main = function ($Update) {...};
87+
$Bot->Handler("Token", $Main);
88+
```
89+
90+
**Örnek** (_/start mesajına karşılık veren bir fonksiyon_):
5291
```php
5392
<?php
5493
require __DIR__ . '/vendor/autoload.php'; //We include the base of the bot.
@@ -70,9 +109,43 @@ $Bot = new Boting(); // We start the base.
70109
$Bot->Handler("Here ur bot token", $Main); // We define our bot token and function.
71110
```
72111

112+
Daha fazla örnek için [bu dosyaya](https://github.com/Quiec/Boting/blob/master/example.php) bir göz atın.
113+
## Commands
114+
Commands are the same as [BotAPI](https://core.telegram.org/bots/api) commands. You can use BotAPI commands in the same way.
115+
116+
Let's give an example you wanted to send a message,We look at the required parameters from [BotAPI](https://core.telegram.org/bots/api#sendmessage).
117+
118+
<img src="https://i.hizliresim.com/CVaBQE.png" width=600 height=300>
119+
120+
We need `chat_id` and` text`. So let's write our code.
121+
122+
```php
123+
$Bot->sendMessage(["chat_id" => "@fusuf", "text" => "Hello!"]);
124+
```
125+
126+
The process is complete.
127+
Komutlar işlem sonrası Array döndürmektedir.
128+
129+
## Examples
130+
We can show [this file](https://github.com/Quiec/Boting/blob/master/example.php) as a very good example of using the library.
131+
Also a code that responds to a simple `/start` message:
132+
133+
```php
134+
<?php
135+
require __DIR__ . '/vendor/autoload.php'; //We include the base of the bot.
136+
use Boting\Boting; // We say we want to use the base.
137+
138+
$Bot = new Boting(); // We start the base.
139+
$Bot->command("/[!.\/]start/m", function ($Update, $Match) use ($Bot) {
140+
$ChatId = $Update["message"]["chat"]["id"];
141+
$Bot->sendMessage(["chat_id" => $ChatId, "text" => "Started bot."]);
142+
});
143+
$Bot->Handler("Here ur bot token"); // We define our bot token.
144+
```
145+
73146
## Licence
74147
This project is completely open source and protected under MIT license. Please refer to the LICENSE.md file
75148

76149

77150
## Contact
78-
You can contact me on [Telegram] (https://t.me/fusuf) or open Issue.
151+
You can contact me on [Telegram](https://t.me/fusuf) or open Issue.

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
}
1717
],
1818
"require": {
19-
"guzzlehttp/guzzle": "^6.5"
19+
"guzzlehttp/guzzle": "^6.5",
20+
"spatie/regex": "^1.4"
2021
},
2122
"autoload": {
2223
"psr-4": {

example.php

Lines changed: 72 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,75 @@
11
<?php
2-
require __DIR__ . '/vendor/autoload.php';
3-
use Boting\Boting;
4-
5-
echo "Started";
6-
$Main = function ($Bot, $Update) {
7-
if (!empty($Update["inline_query"])) {
8-
$Bir = ["type" => "article", "id" => 0, "title" => "test", "input_message_content" => ["message_text" => "sad"]];
9-
$Bot->answerInlineQuery(["inline_query_id" => $Update["inline_query"]["id"], "results" => json_encode([$Bir])]);
10-
} elseif (!empty($Update["message"])) {
11-
$Message = $Update["message"]["text"];
12-
$ChatId = $Update["message"]["chat"]["id"];
13-
14-
if ($Message === "/start") {
15-
$Bot->sendMessage(["chat_id" => $ChatId, "text" => "Bot Started..."])
16-
->sendMessage(["chat_id" => $ChatId, "text" => "Fully Async"]);
17-
} else if ($Message === "/photo") {
18-
$Bot->sendPhoto(["chat_id" => $ChatId, "photo" => "https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Telegram_2019_Logo.svg/1200px-Telegram_2019_Logo.svg.png"]);
19-
} else if ($Message === "/callback") {
20-
$Bot->sendMessage(["chat_id" => $ChatId, "text" => "Example message for callback query", "reply_markup" => json_encode(["inline_keyboard" => [[["text" => "Click me", "callback_data" => "test"], ["text" => "Don't click me", "callback_data" => "test2"]]]])]);
21-
} else if ($Message === "/keyboard") {
22-
$Bot->sendMessage(["chat_id" => $ChatId, "text" => "Example message for keyboard", "reply_markup" => json_encode(["keyboard" => [[["text" => "Click me"], ["text" => "Don't click me"]]]])]);
23-
} else if ($Message === "Click me") {
24-
$Bot->sendMessage(["chat_id" => $ChatId, "text" => "Thanks for click"]);
25-
} else if ($Message === "Don't click me") {
26-
$Bot->sendMessage(["chat_id" => $ChatId, "text" => "Why clicked?!", "reply_markup" => json_encode(["remove_keyboard" => TRUE])]);
27-
}
28-
29-
} elseif (!empty($Update["callback_query"])) {
30-
$Data = $Update["callback_query"]["data"];
31-
if ($Data == "test") {
32-
$Bot->editMessageText(["chat_id" => $Update["callback_query"]["message"]["chat"]["id"], "message_id" => $Update["callback_query"]["message"]["message_id"], "text" => "You clicked button!"]);
33-
} else {
34-
$Bot->answerCallbackQuery(["callback_query_id" => $Update["callback_query"]["id"], "text" => "Unknown callback: " . $Data, "show_alert" => true]);
35-
}
36-
}
37-
};
38-
2+
require __DIR__ . '/vendor/autoload.php';
3+
use Boting\Boting;
394

405
$Bot = new Boting();
41-
$Bot->Handler("YOUR BOT TOKEN", $Main);
6+
7+
$Bot->command("/[!.\/]start/m", function ($Update, $Match) use ($Bot) {
8+
$ChatId = $Update["message"]["chat"]["id"];
9+
$Bot->sendMessage(["chat_id" => $ChatId, "text" => "Started bot."]);
10+
});
11+
12+
$Bot->command("/\/name ?(.*)/m", function ($Update, $Match) use ($Bot) {
13+
$ChatId = $Update["message"]["chat"]["id"];
14+
$Name = $Match->group(1);
15+
$Bot->sendMessage(["chat_id" => $ChatId, "text" => "Hello, $Name"]);
16+
});
17+
18+
$Bot->command("/\/photo/m", function ($Update, $Match) use ($Bot) {
19+
$ChatId = $Update["message"]["chat"]["id"];
20+
$Bot->sendPhoto(["chat_id" => $ChatId, "photo" => "https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Telegram_2019_Logo.svg/1200px-Telegram_2019_Logo.svg.png"]);
21+
});
22+
23+
$Bot->command("/\/callback/m", function ($Update, $Match) use ($Bot) {
24+
$ChatId = $Update["message"]["chat"]["id"];
25+
$Bot->sendMessage(["chat_id" => $ChatId, "text" => "Example message for callback query", "reply_markup" => json_encode(["inline_keyboard" => [[["text" => "Click me", "callback_data" => "test"], ["text" => "Don't click me", "callback_data" => "test2"]]]])]);
26+
});
27+
28+
$Bot->command("/\/keyboard/m", function ($Update, $Match) use ($Bot) {
29+
$ChatId = $Update["message"]["chat"]["id"];
30+
$Bot->sendMessage(["chat_id" => $ChatId, "text" => "Example message for keyboard", "reply_markup" => json_encode(["keyboard" => [[["text" => "Click me"], ["text" => "Don't click me"]]]])]);
31+
});
32+
33+
$Bot->command("/Click me/m", function ($Update, $Match) use ($Bot) {
34+
$ChatId = $Update["message"]["chat"]["id"];
35+
$Bot->sendMessage(["chat_id" => $ChatId, "text" => "Thanks for click"]);
36+
});
37+
38+
$Bot->command("/Don\'t click me/m", function ($Update, $Match) use ($Bot) {
39+
$ChatId = $Update["message"]["chat"]["id"];
40+
$Bot->sendMessage(["chat_id" => $ChatId, "text" => "Why clicked?!", "reply_markup" => json_encode(["remove_keyboard" => TRUE])]);
41+
});
42+
43+
$Bot->on("sticker", function ($Update) use ($Bot) {
44+
$ChatId = $Update["message"]["chat"]["id"];
45+
$MId = $Update["message"]["message_id"];
46+
$bir = microtime(true);
47+
$First = $Bot->sendMessage(["chat_id" => $ChatId, "text" => "I don't like stickers."]);
48+
$Bot->deleteMessage(["chat_id" => $ChatId, "message_id" => $MId]);
49+
$Bot->editMessageText(["chat_id" => $ChatId, "message_id" => $First["result"]["message_id"], "text" => "Deleted in " . (microtime(true) - $bir) . "seconds"]);
50+
});
51+
52+
$Bot->on("photo", function ($Update) use ($Bot) {
53+
$ChatId = $Update["message"]["chat"]["id"];
54+
echo $FileId = $Update["message"]["photo"][2]["file_id"];
55+
$Ilk = $Bot->sendMessage(["chat_id" => $ChatId, "text" => "Downloading "]);
56+
$FileName = $Bot->downloadFile($FileId);
57+
$Bot->editMessageText(["chat_id" => $ChatId, "message_id" => $Ilk["result"]["message_id"], "text" => "Downloaded file as $FileName"]);
58+
59+
});
60+
61+
$Bot->answer("inline_query", function ($Update) use ($Bot) {
62+
$Bir = ["type" => "article", "id" => 0, "title" => "test", "input_message_content" => ["message_text" => "This bot created"]];
63+
$Bot->answerInlineQuery(["inline_query_id" => $Update["inline_query"]["id"], "results" => json_encode([$Bir])]);
64+
});
65+
66+
$Bot->answer("callback_query", function ($Update) use ($Bot) {
67+
$Data = $Update["callback_query"]["data"];
68+
if ($Data == "test") {
69+
$Bot->editMessageText(["chat_id" => $Update["callback_query"]["message"]["chat"]["id"], "message_id" => $Update["callback_query"]["message"]["message_id"], "text" => "You clicked button!"]);
70+
} else {
71+
$Bot->answerCallbackQuery(["callback_query_id" => $Update["callback_query"]["id"], "text" => "Unknown callback: " . $Data, "show_alert" => true]);
72+
}
73+
});
74+
75+
$Bot->handler("920664706:AAEo8NTV_1q_UP8F93kl3CoeD6CzUgJ7xSo");

0 commit comments

Comments
 (0)