Skip to content

Commit acfadc0

Browse files
committed
Added Default Updates
1 parent 5507688 commit acfadc0

File tree

5 files changed

+55
-16
lines changed

5 files changed

+55
-16
lines changed

docs/01-updates.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,15 @@ $bot2 = new Bot("YOUR_BOT_TOKEN", Bot::UPDATES_FROM_WEBHOOK);
2626

2727
$update = $bot2->updates();
2828
echo (json_encode($update) ?? "No updates found.") . "\n";
29-
```
29+
```
30+
31+
## Default Updates
32+
The library provides general default updates to use
33+
34+
```php
35+
$updates = $bot->updates(true);
36+
```
37+
38+
Here's a list of the currently available general updates:
39+
* [`user`](https://core.telegram.org/bots/api#user): The user that performed the action.
40+
* [`chat`](https://core.telegram.org/bots/api#chat): The chat where the action was performed.

examples/echobot-webhook.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77
$bot = new Bot("YOUR_BOT_TOKEN", Bot::UPDATES_FROM_WEBHOOK);
88

9-
$update = $bot->updates();
9+
$update = $bot->updates(true);
1010

1111
if(isset($update->update_id)){
1212

1313
if(isset($update->message)){
1414
$bot->copyMessage([
15-
"chat_id" => $update->message->chat->id,
16-
"from_chat_id" => $update->message->chat->id,
15+
"chat_id" => $update->chat->id,
16+
"from_chat_id" => $update->chat->id,
1717
"message_id" => $update->message->message_id
1818
]);
1919
}

examples/echobot.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@
1212
echo GREEN_TEXT . "Bot Started!\n" . DEFAULT_TEXT;
1313

1414
while(true){
15-
$updates = $bot->updates($updates->lastUpdateID ?? null);
15+
$updates = $bot->updates(true, $updates->lastUpdateID ?? null);
1616

1717
foreach($updates->result as $update){
1818
if(isset($update->message)){
1919
$res = $bot->copyMessage([
20-
"chat_id" => $update->message->chat->id,
21-
"from_chat_id" => $update->message->chat->id,
20+
"chat_id" => $update->chat->id,
21+
"from_chat_id" => $update->chat->id,
2222
"message_id" => $update->message->message_id
2323
]);
2424

2525
if($res->body->ok){
26-
echo GREEN_TEXT . "Replied to " . $update->message->chat->id . "\n" . DEFAULT_TEXT;
26+
echo GREEN_TEXT . "Replied to " . $update->chat->id . "\n" . DEFAULT_TEXT;
2727
} else{
28-
echo RED_TEXT . "Coulnd't reply to " . $update->message->chat->id . ": " . $res->body->error . "\n" . DEFAULT_TEXT;
28+
echo RED_TEXT . "Coulnd't reply to " . $update->chat->id . ": " . $res->body->error . "\n" . DEFAULT_TEXT;
2929
}
3030
}
3131
}

src/Telegram/Bot.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,17 @@ private function sendRequest(string $method, array|object|null $arguments = null
9898
/**
9999
* Retrieves updates from the Telegram API.
100100
*
101-
* @param int|null $offset The updates offset, only in UPDATES_FROM_WEBHOOK mode.
101+
* @param bool $enableDefaultUpdates Whether the default updates should be enabled or not.
102+
* @param int|null $offset The updates offset, only in UPDATES_FROM_WEBHOOK mode.
102103
*
103-
* @return Updates|null The retrieved updates, null on NO_UPDATES mode.
104+
* @return Updates|null The retrieved updates, null on NO_UPDATES mode.
104105
*/
105-
public function updates(?int $offset = null): ?Updates{
106+
public function updates(bool $enableDefaultUpdates = false, ?int $offset = null): ?Updates{
106107
if($this->updatesMethod === self::UPDATES_FROM_GET_UPDATES){
107108

108109
return new Updates($this->getUpdates([
109-
"offset" => $offset + 1
110-
])->body);
110+
"offset" => isset($offset) ? $offset + 1 : null
111+
])->body, $enableDefaultUpdates);
111112

112113
}else if($this->updatesMethod === self::UPDATES_FROM_WEBHOOK){
113114

src/Telegram/Updates.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,43 @@ class Updates{
2222
/**
2323
* Updates constructor.
2424
*
25-
* @param object|null $data The data object containing Telegram updates.
25+
* @param object|null $data The data object containing Telegram updates.
26+
* @param bool $enableDefaultUpdates Whether the default updates should be enabled or not.
2627
*/
27-
public function __construct(?object $data){
28+
public function __construct(?object $data, bool $enableDefaultUpdates = false){
2829
if($data !== NULL){
2930

3031
if(isset($data->result[0]))
3132
$this->lastUpdateID = $data->result[array_key_last($data->result)]->update_id ?? null;
3233
else
3334
$this->lastUpdateID = null;
3435

36+
if($enableDefaultUpdates){
37+
38+
foreach($data->result ?? [$data] as &$upd){
39+
$upd->user = $upd->message->from ??
40+
$upd->message->sender_chat ??
41+
$upd->edited_message->from ??
42+
$upd->inline_query->from ??
43+
$upd->chosen_inline_result->from ??
44+
$upd->callback_query->from ??
45+
$upd->shipping_query->from ??
46+
$upd->poll_answer->user ??
47+
$upd->chat_member->from ??
48+
$upd->chat_join_request->from ??
49+
null;
50+
51+
$upd->chat = $upd->message->chat ??
52+
$upd->edited_message->chat ??
53+
$upd->channel_post->chat ??
54+
$upd->edited_channel_post->chat ??
55+
$upd->callback_query->message->chat ??
56+
$upd->my_chat_member->chat ??
57+
$upd->chat_member->chat ??
58+
$upd->chat_join_request->chat ??
59+
null;
60+
}
61+
}
3562

3663
foreach($data as $key => $value)
3764
$this->$key = $value;

0 commit comments

Comments
 (0)