1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the TelegramBot package. |
5
|
|
|
* |
6
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Longman\TelegramBot\Entities; |
13
|
|
|
|
14
|
|
|
use Longman\TelegramBot\Entities\Payments\PreCheckoutQuery; |
15
|
|
|
use Longman\TelegramBot\Entities\Payments\ShippingQuery; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Update |
19
|
|
|
* |
20
|
|
|
* @link https://core.telegram.org/bots/api#update |
21
|
|
|
* |
22
|
|
|
* @method int getUpdateId() The update's unique identifier. Update identifiers start from a certain positive number and increase sequentially. This ID becomes especially handy if you’re using Webhooks, since it allows you to ignore repeated updates or to restore the correct update sequence, should they get out of order. |
23
|
|
|
* @method Message getMessage() Optional. New incoming message of any kind — text, photo, sticker, etc. |
24
|
|
|
* @method Message getEditedMessage() Optional. New version of a message that is known to the bot and was edited |
25
|
|
|
* @method Message getChannelPost() Optional. New post in the channel, can be any kind — text, photo, sticker, etc. |
26
|
|
|
* @method Message getEditedChannelPost() Optional. New version of a post in the channel that is known to the bot and was edited |
27
|
|
|
* @method InlineQuery getInlineQuery() Optional. New incoming inline query |
28
|
|
|
* @method ChosenInlineResult getChosenInlineResult() Optional. The result of an inline query that was chosen by a user and sent to their chat partner. |
29
|
|
|
* @method CallbackQuery getCallbackQuery() Optional. New incoming callback query |
30
|
|
|
* @method ShippingQuery getShippingQuery() Optional. New incoming shipping query. Only for invoices with flexible price |
31
|
|
|
* @method PreCheckoutQuery getPreCheckoutQuery() Optional. New incoming pre-checkout query. Contains full information about checkout |
32
|
|
|
* @method Poll getPoll() Optional. New poll state. Bots receive only updates about polls, which are sent or stopped by the bot |
33
|
|
|
* @method PollAnswer getPollAnswer() Optional. A user changed their answer in a non-anonymous poll. Bots receive new votes only in polls that were sent by the bot itself. |
34
|
|
|
* @method ChatMemberUpdated getMyChatMember() Optional. The bot's chat member status was updated in a chat. For private chats, this update is received only when the bot is blocked or unblocked by the user. |
35
|
|
|
* @method ChatMemberUpdated getChatMember() Optional. A chat member's status was updated in a chat. The bot must be an administrator in the chat and must explicitly specify “chat_member” in the list of allowed_updates to receive these updates. |
36
|
|
|
* @method ChatJoinRequest getChatJoinRequest() Optional. A request to join the chat has been sent. The bot must have the can_invite_users administrator right in the chat to receive these updates. |
37
|
|
|
*/ |
38
|
|
|
class Update extends Entity |
39
|
|
|
{ |
40
|
|
|
public const TYPE_MESSAGE = 'message'; |
41
|
|
|
public const TYPE_EDITED_MESSAGE = 'edited_message'; |
42
|
|
|
public const TYPE_CHANNEL_POST = 'channel_post'; |
43
|
|
|
public const TYPE_EDITED_CHANNEL_POST = 'edited_channel_post'; |
44
|
|
|
public const TYPE_INLINE_QUERY = 'inline_query'; |
45
|
|
|
public const TYPE_CHOSEN_INLINE_RESULT = 'chosen_inline_result'; |
46
|
|
|
public const TYPE_CALLBACK_QUERY = 'callback_query'; |
47
|
|
|
public const TYPE_SHIPPING_QUERY = 'shipping_query'; |
48
|
|
|
public const TYPE_PRE_CHECKOUT_QUERY = 'pre_checkout_query'; |
49
|
|
|
public const TYPE_POLL = 'poll'; |
50
|
|
|
public const TYPE_POLL_ANSWER = 'poll_answer'; |
51
|
|
|
public const TYPE_MY_CHAT_MEMBER = 'my_chat_member'; |
52
|
|
|
public const TYPE_CHAT_MEMBER = 'chat_member'; |
53
|
|
|
public const TYPE_CHAT_JOIN_REQUEST = 'chat_join_request'; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
3 |
|
protected function subEntities(): array |
59
|
|
|
{ |
60
|
|
|
return [ |
61
|
3 |
|
self::TYPE_MESSAGE => Message::class, |
62
|
|
|
self::TYPE_EDITED_MESSAGE => EditedMessage::class, |
63
|
|
|
self::TYPE_CHANNEL_POST => ChannelPost::class, |
64
|
|
|
self::TYPE_EDITED_CHANNEL_POST => EditedChannelPost::class, |
65
|
|
|
self::TYPE_INLINE_QUERY => InlineQuery::class, |
66
|
|
|
self::TYPE_CHOSEN_INLINE_RESULT => ChosenInlineResult::class, |
67
|
|
|
self::TYPE_CALLBACK_QUERY => CallbackQuery::class, |
68
|
|
|
self::TYPE_SHIPPING_QUERY => ShippingQuery::class, |
69
|
|
|
self::TYPE_PRE_CHECKOUT_QUERY => PreCheckoutQuery::class, |
70
|
|
|
self::TYPE_POLL => Poll::class, |
71
|
|
|
self::TYPE_POLL_ANSWER => PollAnswer::class, |
72
|
|
|
self::TYPE_MY_CHAT_MEMBER => ChatMemberUpdated::class, |
73
|
|
|
self::TYPE_CHAT_MEMBER => ChatMemberUpdated::class, |
74
|
|
|
self::TYPE_CHAT_JOIN_REQUEST => ChatJoinRequest::class, |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the list of all available update types |
80
|
|
|
* |
81
|
|
|
* @return string[] |
82
|
|
|
*/ |
83
|
|
|
public static function getUpdateTypes(): array |
84
|
|
|
{ |
85
|
|
|
return array_keys((new self([]))->subEntities()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the update type based on the set properties |
90
|
|
|
* |
91
|
|
|
* @return string|null |
92
|
|
|
*/ |
93
|
|
|
public function getUpdateType(): ?string |
94
|
|
|
{ |
95
|
|
|
foreach (self::getUpdateTypes() as $type) { |
96
|
|
|
if ($this->getProperty($type)) { |
97
|
|
|
return $type; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return null; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get update content |
106
|
|
|
* |
107
|
|
|
* @return CallbackQuery|ChatMemberUpdated|ChosenInlineResult|InlineQuery|Message|PollAnswer|Poll|PreCheckoutQuery|ShippingQuery |
108
|
|
|
*/ |
109
|
|
|
public function getUpdateContent() |
110
|
|
|
{ |
111
|
|
|
if ($update_type = $this->getUpdateType()) { |
112
|
|
|
// Instead of just getting the property as an array, |
113
|
|
|
// use the __call method to get the correct Entity object. |
114
|
|
|
$method = 'get' . str_replace('_', '', ucwords($update_type, '_')); |
115
|
|
|
return $this->$method(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return null; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|