Skip to content

Commit b9d2edb

Browse files
committed
Update examples to v1.8
1 parent 1c4e024 commit b9d2edb

File tree

6 files changed

+72
-97
lines changed

6 files changed

+72
-97
lines changed

β€ŽCHANGELOG.mdβ€Ž

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# CHANGELOG
22

3-
## v1.8 - [_Not released yet_](https://github.com/skrtdev/NovaGram/)
3+
## v1.8 - [Source Code](https://github.com/skrtdev/NovaGram/releases/tag/v1.8)
44
- New handlers: `onTextMessage`, `onText`, and `onCommand`
55
- New Bot settings:
66
- `command_prefixes`: Array of characters for commands prefixes. e.g. `/start`, `.info`
@@ -23,7 +23,8 @@
2323
- You can now use PHP8 `named arguments` instead of the `$args` array in both `Bot` and `Objects` methods
2424
- A custom `PDO` instance can now be used as database. Pass it in the database Bot setting instead of the Database array (`novagram` will be used as prefix)
2525
- `Exception::$response_parameters` is now an instance of `ResponseParameters` or null
26-
- Return types of every method has been updated
26+
- Return types of every method has been updated
27+
- Now a warning is emitted when using getUpdates if a webhook url is set
2728

2829
## v1.7 - [Source Code](https://github.com/skrtdev/NovaGram/releases/tag/v1.7)
2930
- Improved performances of [skrtdev/async](https://github.com/skrtdev/php-async)

β€ŽREADME.mdβ€Ž

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,14 @@ use skrtdev\Telegram\Message;
1717

1818
$Bot = new Bot("YOUR_TOKEN");
1919

20-
$Bot->onMessage(function (Message $message) use ($Bot) {
21-
22-
if(isset($message->text)){ // update is a message and has text
23-
$chat = $message->chat;
24-
$user = $message->from;
25-
$text = $message->text;
26-
27-
if($text === "/start"){
28-
$message->reply("Hey! Nice to meet you. Use /info to know more about me.");
29-
}
30-
if($text === "/info"){
31-
$message->reply("Well, I'm just an example, but you can learn more about NovaGram at docs.novagram.ga");
32-
}
33-
}
20+
$Bot->onCommand("start", function (Message $message) use ($Bot) {
21+
$message->reply("Hey! Nice to meet you. Use /info to know more about me.");
3422
});
23+
24+
$Bot->onCommand("info", function (Message $message) use ($Bot) {
25+
$message->reply("Well, I'm just an example, but you can learn more about NovaGram at docs.novagram.ga");
26+
});
27+
3528
```
3629

3730
## Features
@@ -53,7 +46,7 @@ NovaGram is built in order to bring a lightweight alternative to make bots, so t
5346

5447
Install NovaGram via Composer
5548
```
56-
composer require skrtdev/novagram
49+
composer require skrtdev/novagram ^1.8
5750
```
5851

5952
After Installation, include NovaGram with:
Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,9 @@
1616
"debug" => YOURCHATID, // chat id where debug will be sent when api errors occurs
1717
]);
1818

19-
$Bot->onMessage(function (Message $message) use ($Bot) { // update is a message
20-
19+
$Bot->onTextMessage(function (Message $message) use ($Bot) { // update is a message and contains text
2120
$chat = $message->chat;
22-
$user = $message->from;
23-
24-
if(isset($message->text)){ // update message contains text
25-
$chat->sendMessage($message->text, true); // send a Message in the Chat. Text is the same as the just received message
26-
}
27-
else{
28-
$chat->sendMessage("that's not text", true); // Message doesn't cointain text
29-
}
30-
21+
$chat->sendMessage($message->text, true); // send a Message in the Chat. Text is the same as the just received message
3122
});
3223

3324
?>
Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,32 @@
1717
"parse_mode" => "HTML" // will set parse_mode automatically in methods that require it if not providedΓ₯
1818
]);
1919

20-
$Bot->onMessage(function (Message $message) use ($Bot) { // update is a message
20+
$Bot->onTextMessage(function (Message $message) use ($Bot) { // update is a message and contains text
2121

2222
$chat = $message->chat;
2323
$user = $message->from;
24+
$text = $message->text;
2425

25-
if(isset($message->text)){ // update message contains text
26-
// Message Text
27-
$chat->sendMessage("Text: \n<code>".$update->message->text."</code>", [ // send a Message in the Chat
28-
"reply_markup" => [
29-
"inline_keyboard" => [ // Message Inline Keyboard
26+
$chat->sendMessage("Text: \n<code>".$text."</code>", [ // send a Message in the Chat
27+
"reply_markup" => [
28+
"inline_keyboard" => [ // Message Inline Keyboard
29+
[
3030
[
31-
[
32-
"text" => "MD5",
33-
"callback_data" => md5($message->text)
34-
],
35-
[
36-
"text" => "sha256",
37-
"callback_data" => hash("sha256", $message->text)
38-
],
39-
[
40-
"text" => "sha1",
41-
"callback_data" => hash("sha1", $message->text)
42-
]
31+
"text" => "MD5",
32+
"callback_data" => md5($message->text)
33+
],
34+
[
35+
"text" => "sha256",
36+
"callback_data" => hash("sha256", $message->text)
37+
],
38+
[
39+
"text" => "sha1",
40+
"callback_data" => hash("sha1", $message->text)
4341
]
4442
]
4543
]
46-
], true); // this true will execute this api call as json payload
47-
48-
}
49-
else $chat->sendMessage("that's not text", true); // Message doesn't cointain text
44+
]
45+
], true); // this true will execute this api call as json payload
5046

5147
});
5248

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,27 @@
1818
$Bot = new Bot(readline("Insert Bot token: "));
1919

2020

21-
$Bot->onMessage(function (Message $message) use ($Bot) { // update is a message
22-
23-
if(isset($message->from)){ // message has a sender
24-
$text = $message->text;
25-
26-
if($text === "/novagram"){
27-
throw new NovaGramException("Sample Exception");
28-
}
29-
if($text === "/telegram"){
30-
$Bot->sendMessage(0, "uh");
31-
}
32-
if($text === "/getUpdates"){
33-
$Bot->getUpdates();
34-
}
35-
if($text === "/exception"){
36-
throw new \Exception("Sample Exception");
37-
}
38-
if($text === "/error"){
39-
throw new \Error("Sample Error");
40-
}
41-
}
21+
$Bot->onCommand("novagram", function (Message $message) {
22+
throw new NovaGramException("Sample Exception");
23+
});
24+
25+
$Bot->onCommand("telegram", function (Message $message) use ($Bot) {
26+
$Bot->sendMessage(0, "uh");
27+
});
28+
29+
$Bot->onCommand("getUpdates", function (Message $message) use ($Bot) {
30+
$Bot->getUpdates();
4231
});
4332

33+
$Bot->onCommand("exception", function (Message $message) {
34+
throw new Exception("Sample Exception");
35+
});
36+
37+
$Bot->onCommand("error", function (Message $message) {
38+
throw new Error("Sample Error");
39+
});
40+
41+
4442
$Bot->addErrorHandler(function (NovaGramException $e) {
4543
print("Caught ".get_class($e)." exception from speficic handler".PHP_EOL);
4644
});

β€Žexamples/ping.phpβ€Ž

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,35 @@
1818
"debug" => YOURCHATID, // chat id where debug will be sent when api errors occurs
1919
]);
2020

21-
$Bot->onMessage(function (Message $message) use ($Bot) { // update is a message
22-
23-
if(isset($message->text)){ // message contains text
24-
$text = $message->text;
25-
$user = $message->from;
26-
$chat = $message->chat;
21+
$Bot->onCommand("start", function (Message $message) {
22+
$message->reply("/ping\n\n/moon");
23+
});
2724

28-
if($text === "/start"){
29-
$message->reply("/ping\n\n/moon");
30-
}
25+
$Bot->onCommand("ping", function (Message $message) {
26+
$chat = $message->chat;
3127

32-
if($text === "/ping"){
33-
$started = hrtime(true)/10**9;
34-
$mex = $chat->sendMessage("Pong.");
35-
$mex->editText("Ping: ".(((hrtime(true)/10**9)-$started)*1000).'ms', true);
36-
}
28+
$started = hrtime(true)/10**9;
29+
$mex = $chat->sendMessage("Pong.");
30+
$mex->editText("Ping: ".(((hrtime(true)/10**9)-$started)*1000).'ms', true);
31+
});
3732

38-
if($text === "/moon"){
39-
$emojis = "πŸŒ‘πŸŒ’πŸŒ“πŸŒ”πŸŒ•πŸŒ–πŸŒ—πŸŒ˜πŸŒ‘";
40-
$mex = $chat->sendMessage($emojis);
41-
for ($n=0; $n < 4; $n++) {
42-
for ($i=0; $i < strlen($emojis)+$n+1; $i++) {
43-
$thistext = mb_substr($emojis, $i, null, 'UTF-8').mb_substr($emojis, 0, $i, 'UTF-8');
44-
if ($thistext === $emojis) continue;
45-
$mex->editText($thistext);
46-
usleep(75);
47-
}
48-
}
49-
$mex->editText($emojis);
33+
$Bot->onCommand("moon", function (Message $message) {
34+
$chat = $message->chat;
35+
36+
$emojis = "πŸŒ‘πŸŒ’πŸŒ“πŸŒ”πŸŒ•πŸŒ–πŸŒ—πŸŒ˜πŸŒ‘";
37+
$mex = $chat->sendMessage($emojis);
38+
for ($n=0; $n < 4; $n++) {
39+
for ($i=0; $i < strlen($emojis)+$n+1; $i++) {
40+
$thistext = mb_substr($emojis, $i, null, 'UTF-8').mb_substr($emojis, 0, $i, 'UTF-8');
41+
if ($thistext === $emojis) continue;
42+
$mex->editText($thistext);
43+
usleep(75);
5044
}
5145
}
46+
$mex->editText($emojis);
5247
});
5348

49+
5450
$Bot->setErrorHandler(function(Throwable $e) {
5551
print("uff, another exception occured:".PHP_EOL);
5652
print($e);

0 commit comments

Comments
Β (0)