Skip to content

Commit 00cec25

Browse files
committed
Removed the concept of default updates.
Now the old default updates can get obtained with methods.
1 parent 2d405d6 commit 00cec25

File tree

4 files changed

+84
-60
lines changed

4 files changed

+84
-60
lines changed

examples/echobot-getupdates.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,24 @@
2222

2323
for ( ; ; sleep(5)) {
2424

25-
$updates = $bot->updates(true, isset($updates) ? $updates->getLastUpdateId() : null);
25+
$updates = $bot->updates(isset($updates) ? $updates->getLastUpdateId() : null);
2626

2727
foreach($updates->result as $update){
2828
if(isset($update->message)){
29+
$chat = $update->getChat();
2930

3031
try {
3132

3233
$res = $bot->copyMessage([
33-
"chat_id" => $update->chat->id,
34-
"from_chat_id" => $update->chat->id,
35-
"message_id" => $update->message->message_id
34+
"chat_id" => $chat->id,
35+
"from_chat_id" => $chat->id,
36+
"message_id" => $update->getMessage()->message_id
3637
]);
3738

38-
echo GREEN_COLOR . "Replied to " . $update->chat->id . "\n" . DEFAULT_COLOR;
39+
echo GREEN_COLOR . "Replied to " . $chat->id . "\n" . DEFAULT_COLOR;
3940

4041
} catch (TelegramException $e) {
41-
echo RED_COLOR . "Coulnd't reply to " . $update->chat->id . ": " . $e->getResponseBody()->description . "\n" . DEFAULT_COLOR;
42+
echo RED_COLOR . "Coulnd't reply to " . $chat->id . ": " . $e->getResponseBody()->description . "\n" . DEFAULT_COLOR;
4243
}
4344

4445
}

examples/echobot-webhook.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@
22

33
require_once "vendor/autoload.php";
44

5-
use TelegramSDK\BotAPI\Exception\TelegramException;
65
use TelegramSDK\BotAPI\Telegram\{Bot, Update};
76

87
$bot = new Bot("YOUR_BOT_TOKEN", Update::UPDATES_FROM_WEBHOOK);
98

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

1211
if(isset($update->update_id)){
1312

1413
if(isset($update->message)){
14+
$chat = $update->getChat();
15+
1516
$bot->copyMessage([
16-
"chat_id" => $update->chat->id,
17-
"from_chat_id" => $update->chat->id,
18-
"message_id" => $update->message->message_id
17+
"chat_id" => $chat->id,
18+
"from_chat_id" => $chat->id,
19+
"message_id" => $update->getMessage()->message_id
1920
]);
2021
}
2122

src/Telegram/Bot.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class Bot
3232
* @param int|null $updatesMethod The updates method to use.
3333
* @param string $apiURL The API URL for Telegram requests.
3434
*
35-
* @throws InvalidTokenException If an invalid token is provided.
3635
* @throws \InvalidArgumentException If an invalid updates method is provided.
3736
*/
3837
public function __construct(string $token, ?int $updatesMethod = null, string $apiURL = self::DEFAULT_API_URL)
@@ -113,21 +112,20 @@ protected function sendRequest(string $method, array|object|null $arguments = nu
113112
/**
114113
* Retrieves updates from the Telegram API.
115114
*
116-
* @param bool $enableDefaultUpdates Whether the default updates should be enabled or not.
117115
* @param int|null $offset The updates offset, only in UPDATES_FROM_WEBHOOK mode.
118116
*
119117
* @return Update|null The retrieved updates, null on NO_UPDATES mode.
120118
*/
121-
public function updates(bool $enableDefaultUpdates = false, ?int $offset = null): ?Update
119+
public function updates(?int $offset = null): ?Update
122120
{
123121
if ($this->updatesMethod === Update::UPDATES_FROM_GET_UPDATES) {
124122
return new Update($this->getUpdates([
125123
"offset" => isset($offset) ? $offset + 1 : null
126-
])->getBody(), $this->updatesMethod, $enableDefaultUpdates);
124+
])->getBody(), $this->updatesMethod);
127125
}
128126

129127
if ($this->updatesMethod === Update::UPDATES_FROM_WEBHOOK) {
130-
return new Update(json_decode(file_get_contents("php://input")), $this->updatesMethod, $enableDefaultUpdates);
128+
return new Update(json_decode(file_get_contents("php://input")), $this->updatesMethod);
131129
}
132130

133131
return null;

src/Telegram/Update.php

Lines changed: 68 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class Update
2020
{
2121
private int $method;
2222
private ?int $lastUpdateID;
23+
private object $data;
24+
private array $customs = [ ];
2325
public bool $ok;
2426
public object|array $result;
2527

@@ -31,23 +33,17 @@ class Update
3133
*
3234
* @param object|null $data The data object containing Telegram updates.
3335
* @param int $updatesMethod The updates method to use.
34-
* @param bool $enableDefaultUpdates Whether the default updates should be enabled or not.
3536
*/
36-
public function __construct(?object $data, int $updatesMethod, bool $enableDefaultUpdates = false)
37+
public function __construct(?object $data, int $updatesMethod)
3738
{
3839
self::validateUpdateMethod($updatesMethod);
3940
$this->method = $updatesMethod;
4041

41-
if ($data !== null) {
42-
$this->lastUpdateID = isset($data->result[0]) ? $data->result[array_key_last($data->result)]->update_id ?? null : null;
42+
$this->data = $data;
43+
$this->lastUpdateID = isset($data->result[0]) ? $data->result[array_key_last($data->result)]->update_id ?? null : null;
4344

44-
if ($enableDefaultUpdates) {
45-
$data = $this->getDefaultUpdates($data);
46-
}
47-
48-
foreach ($data as $key => $value) {
49-
$this->$key = $value;
50-
}
45+
foreach ($data as $key => $value) {
46+
$this->$key = $value;
5147
}
5248
}
5349

@@ -65,39 +61,6 @@ public static function validateUpdateMethod(int $method): void
6561
}
6662
}
6763

68-
private function getDefaultUpdates(object $data): object
69-
{
70-
foreach($data->result ?? [$data] as &$upd) {
71-
$upd->msg = $upd->message ??
72-
$upd->edited_message ??
73-
$upd->channel_post ??
74-
$upd->edited_channel_post ??
75-
$upd->callback_query->message ??
76-
null;
77-
78-
$upd->user = $upd->callback_query->from ??
79-
$upd->msg->from ??
80-
$upd->msg->sender_chat ??
81-
$upd->inline_query->from ??
82-
$upd->chosen_inline_result->from ??
83-
$upd->callback_query->from ??
84-
$upd->shipping_query->from ??
85-
$upd->poll_answer->user ??
86-
$upd->chat_member->from ??
87-
$upd->chat_join_request->from ??
88-
null;
89-
90-
$upd->chat = $upd->msg->chat ??
91-
$upd->callback_query->message->chat ??
92-
$upd->my_chat_member->chat ??
93-
$upd->chat_member->chat ??
94-
$upd->chat_join_request->chat ??
95-
null;
96-
}
97-
98-
return $data;
99-
}
100-
10164
/**
10265
* Checks if the update is from Telegram.
10366
*
@@ -131,4 +94,65 @@ public function getLastUpdateId(): ?int
13194
{
13295
return $this->lastUpdateID;
13396
}
97+
98+
/**
99+
* Get the message from the update.
100+
*
101+
* @return object|null The message object if present; otherwise, null.
102+
*/
103+
public function getMessage(): ?object
104+
{
105+
if(!isset($customs['message'])) {
106+
$this->customs['message'] = $this->data->message ??
107+
$this->data->edited_message ??
108+
$this->data->channel_post ??
109+
$this->data->edited_channel_post ??
110+
$this->data->callback_query->message ??
111+
null;
112+
}
113+
114+
return $this->customs['message'];
115+
}
116+
117+
/**
118+
* Get the chat from the update.
119+
*
120+
* @return object|null The chat object if present; otherwise, null.
121+
*/
122+
public function getChat(): ?object {
123+
if(!isset($customs['chat'])) {
124+
$this->customs['message'] = $this->customs['message']->chat ??
125+
$this->data->callback_query->message->chat ??
126+
$this->data->my_chat_member->chat ??
127+
$this->data->chat_member->chat ??
128+
$this->data->chat_join_request->chat ??
129+
null;
130+
}
131+
132+
return $this->customs['message'];
133+
}
134+
135+
/**
136+
* Get the user from the update.
137+
*
138+
* @return object|null The user object if present; otherwise, null.
139+
*/
140+
public function getUser(): ?object
141+
{
142+
if(!isset($customs['user'])) {
143+
$this->customs['message'] = $this->data->callback_query->from ??
144+
$this->customs['message']->from ??
145+
$this->customs['message']->sender_chat ??
146+
$this->data->inline_query->from ??
147+
$this->data->chosen_inline_result->from ??
148+
$this->data->callback_query->from ??
149+
$this->data->shipping_query->from ??
150+
$this->data->poll_answer->user ??
151+
$this->data->chat_member->from ??
152+
$this->data->chat_join_request->from ??
153+
null;
154+
}
155+
156+
return $this->customs['user'];
157+
}
134158
}

0 commit comments

Comments
 (0)