This repository has been archived by the owner on Apr 21, 2024. It is now read-only.
forked from irazasyed/telegram-bot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Update.php
169 lines (156 loc) · 5.66 KB
/
Update.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace Telegram\Bot\Objects;
use Illuminate\Support\Collection;
use Telegram\Bot\Objects\Payments\PreCheckoutQuery;
use Telegram\Bot\Objects\Payments\ShippingQuery;
/**
* Class Update.
*
*
* @property int $updateId The update's unique identifier. Update identifiers start from a
* certain positive number and increase sequentially.
* @property Message $message (Optional). New incoming message of any kind - text, photo,
* sticker, etc.
* @property EditedMessage $editedMessage (Optional). New version of a message that is known to the bot
* and was edited.
* @property Message $channelPost (Optional).Optional. New incoming channel post of any kind — text,
* photo, sticker, etc.
* @property EditedMessage $editedChannelPost (Optional). New version of a channel post that is known to the
* bot and was edited sticker, etc.
* @property InlineQuery $inlineQuery (Optional). New incoming inline query.
* @property ChosenInlineResult $chosenInlineResult (Optional). A result of an inline query that was chosen by the
* user and sent to their chat partner.
* @property CallbackQuery $callbackQuery (Optional). Incoming callback query.
* @property ShippingQuery $shippingQuery (Optional). New incoming shipping query. Only for invoices with
* flexible price
* @property PreCheckoutQuery $preCheckoutQuery (Optional). New incoming pre-checkout query. Contains full
* information about checkout
* @property Poll $poll (Optional). Optional. New poll state. Bots receive only updates about stopped polls and polls, which are sent by the bot
*
* @link https://core.telegram.org/bots/api#update
*/
class Update extends BaseObject
{
/**
* {@inheritdoc}
*/
public function relations()
{
return [
'message' => Message::class,
'edited_message' => EditedMessage::class,
'channel_post' => Message::class,
'edited_channel_post' => EditedMessage::class,
'inline_query' => InlineQuery::class,
'chosen_inline_result' => ChosenInlineResult::class,
'callback_query' => CallbackQuery::class,
'shipping_query' => ShippingQuery::class,
'pre_checkout_query' => PreCheckoutQuery::class,
'poll' => Poll::class,
];
}
/**
* Get recent message.
*
* @return Update
*/
public function recentMessage()
{
return new static($this->last());
}
/**
* Determine if the update is of given type.
*
* @param string $type
*
* @return bool
*/
public function isType($type)
{
if ($this->has(strtolower($type))) {
return true;
}
return $this->detectType() === $type;
}
/**
* Detect type based on properties.
*
* @return string|null
*/
public function detectType()
{
$types = [
'message',
'edited_message',
'channel_post',
'edited_channel_post',
'inline_query',
'chosen_inline_result',
'callback_query',
'shipping_query',
'pre_checkout_query',
'poll',
'poll_answer',
'my_chat_member',
'chat_member',
'chat_join_request',
];
return $this->keys()
->intersect($types)
->pop();
}
/**
* Get the message contained in the Update.
*
* @return Message|EditedMessage|Collection
*/
public function getMessage(): Collection
{
switch ($this->detectType()) {
case 'message':
return $this->message;
case 'edited_message':
return $this->editedMessage;
case 'channel_post':
return $this->channelPost;
case 'edited_channel_post':
return $this->editedChannelPost;
case 'inline_query':
return $this->inlineQuery;
case 'chosen_inline_result':
return $this->chosenInlineResult;
case 'callback_query':
$callbackQuery = $this->callbackQuery;
if ($callbackQuery->has('message')) {
return $callbackQuery->message;
}
break;
case 'shipping_query':
return $this->shippingQuery;
case 'pre_checkout_query':
return $this->preCheckoutQuery;
case 'poll':
return $this->poll;
}
return collect();
}
/**
* Get chat object (if exists).
*
* @return Chat|Collection
*/
public function getChat(): Collection
{
$message = $this->getMessage();
return $message->has('chat') ? $message->get('chat') : collect();
}
/**
* Is there a command entity in this update object.
*
* @return bool
*/
public function hasCommand()
{
return (bool) $this->getMessage()->get('entities', collect())->contains('type', 'bot_command');
}
}