1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the TelegramBot package. |
4
|
|
|
* |
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Longman\TelegramBot\Entities; |
12
|
|
|
|
13
|
|
|
use Longman\TelegramBot\Entities\Games\Game; |
14
|
|
|
use Longman\TelegramBot\Entities\Payments\Invoice; |
15
|
|
|
use Longman\TelegramBot\Entities\Payments\SuccessfulPayment; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Message |
19
|
|
|
* |
20
|
|
|
* @link https://core.telegram.org/bots/api#message |
21
|
|
|
* |
22
|
|
|
* @method int getMessageId() Unique message identifier |
23
|
|
|
* @method User getFrom() Optional. Sender, can be empty for messages sent to channels |
24
|
|
|
* @method int getDate() Date the message was sent in Unix time |
25
|
|
|
* @method Chat getChat() Conversation the message belongs to |
26
|
|
|
* @method User getForwardFrom() Optional. For forwarded messages, sender of the original message |
27
|
|
|
* @method Chat getForwardFromChat() Optional. For messages forwarded from a channel, information about the original channel |
28
|
|
|
* @method int getForwardFromMessageId() Optional. For forwarded channel posts, identifier of the original message in the channel |
29
|
|
|
* @method string getForwardSignature() Optional. For messages forwarded from channels, signature of the post author if present |
30
|
|
|
* @method int getForwardDate() Optional. For forwarded messages, date the original message was sent in Unix time |
31
|
|
|
* @method Message getReplyToMessage() Optional. For replies, the original message. Note that the Message object in this field will not contain further reply_to_message fields even if it itself is a reply. |
32
|
|
|
* @method int getEditDate() Optional. Date the message was last edited in Unix time |
33
|
|
|
* @method string getMediaGroupId() Optional. The unique identifier of a media message group this message belongs to |
34
|
|
|
* @method string getAuthorSignature() Optional. Signature of the post author for messages in channels |
35
|
|
|
* @method Audio getAudio() Optional. Message is an audio file, information about the file |
36
|
|
|
* @method Document getDocument() Optional. Message is a general file, information about the file |
37
|
|
|
* @method Animation getAnimation() Optional. Message is an animation, information about the animation. For backward compatibility, when this field is set, the document field will also be set |
38
|
|
|
* @method Game getGame() Optional. Message is a game, information about the game. |
39
|
|
|
* @method Sticker getSticker() Optional. Message is a sticker, information about the sticker |
40
|
|
|
* @method Video getVideo() Optional. Message is a video, information about the video |
41
|
|
|
* @method Voice getVoice() Optional. Message is a voice message, information about the file |
42
|
|
|
* @method VideoNote getVideoNote() Optional. Message is a video note message, information about the video |
43
|
|
|
* @method string getCaption() Optional. Caption for the document, photo or video, 0-200 characters |
44
|
|
|
* @method Contact getContact() Optional. Message is a shared contact, information about the contact |
45
|
|
|
* @method Location getLocation() Optional. Message is a shared location, information about the location |
46
|
|
|
* @method Venue getVenue() Optional. Message is a venue, information about the venue |
47
|
|
|
* @method User getLeftChatMember() Optional. A member was removed from the group, information about them (this member may be the bot itself) |
48
|
|
|
* @method string getNewChatTitle() Optional. A chat title was changed to this value |
49
|
|
|
* @method bool getDeleteChatPhoto() Optional. Service message: the chat photo was deleted |
50
|
|
|
* @method bool getGroupChatCreated() Optional. Service message: the group has been created |
51
|
|
|
* @method bool getSupergroupChatCreated() Optional. Service message: the supergroup has been created. This field can't be received in a message coming through updates, because bot can’t be a member of a supergroup when it is created. It can only be found in reply_to_message if someone replies to a very first message in a directly created supergroup. |
52
|
|
|
* @method bool getChannelChatCreated() Optional. Service message: the channel has been created. This field can't be received in a message coming through updates, because bot can’t be a member of a channel when it is created. It can only be found in reply_to_message if someone replies to a very first message in a channel. |
53
|
|
|
* @method int getMigrateToChatId() Optional. The group has been migrated to a supergroup with the specified identifier. This number may be greater than 32 bits and some programming languages may have difficulty/silent defects in interpreting it. But it smaller than 52 bits, so a signed 64 bit integer or double-precision float type are safe for storing this identifier. |
54
|
|
|
* @method int getMigrateFromChatId() Optional. The supergroup has been migrated from a group with the specified identifier. This number may be greater than 32 bits and some programming languages may have difficulty/silent defects in interpreting it. But it smaller than 52 bits, so a signed 64 bit integer or double-precision float type are safe for storing this identifier. |
55
|
|
|
* @method Message getPinnedMessage() Optional. Specified message was pinned. Note that the Message object in this field will not contain further reply_to_message fields even if it is itself a reply. |
56
|
|
|
* @method Invoice getInvoice() Optional. Message is an invoice for a payment, information about the invoice. |
57
|
|
|
* @method SuccessfulPayment getSuccessfulPayment() Optional. Message is a service message about a successful payment, information about the payment. |
58
|
|
|
* @method string getConnectedWebsite() Optional. The domain name of the website on which the user has logged in. |
59
|
|
|
*/ |
60
|
|
|
class Message extends Entity |
61
|
|
|
{ |
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
10 |
|
protected function subEntities() |
66
|
|
|
{ |
67
|
|
|
return [ |
68
|
10 |
|
'from' => User::class, |
69
|
|
|
'chat' => Chat::class, |
70
|
|
|
'forward_from' => User::class, |
71
|
|
|
'forward_from_chat' => Chat::class, |
72
|
|
|
'reply_to_message' => ReplyToMessage::class, |
73
|
|
|
'entities' => MessageEntity::class, |
74
|
|
|
'caption_entities' => MessageEntity::class, |
75
|
|
|
'audio' => Audio::class, |
76
|
|
|
'document' => Document::class, |
77
|
|
|
'animation' => Animation::class, |
78
|
|
|
'game' => Game::class, |
79
|
|
|
'photo' => PhotoSize::class, |
80
|
|
|
'sticker' => Sticker::class, |
81
|
|
|
'video' => Video::class, |
82
|
|
|
'voice' => Voice::class, |
83
|
|
|
'video_note' => VideoNote::class, |
84
|
|
|
'contact' => Contact::class, |
85
|
|
|
'location' => Location::class, |
86
|
|
|
'venue' => Venue::class, |
87
|
|
|
'new_chat_members' => User::class, |
88
|
|
|
'left_chat_member' => User::class, |
89
|
|
|
'new_chat_photo' => PhotoSize::class, |
90
|
|
|
'pinned_message' => Message::class, |
91
|
|
|
'invoice' => Invoice::class, |
92
|
|
|
'successful_payment' => SuccessfulPayment::class, |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Message constructor |
98
|
|
|
* |
99
|
|
|
* @param array $data |
100
|
|
|
* @param string $bot_username |
101
|
|
|
* |
102
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
103
|
|
|
*/ |
104
|
12 |
|
public function __construct(array $data, $bot_username = '') |
105
|
|
|
{ |
106
|
12 |
|
parent::__construct($data, $bot_username); |
107
|
12 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Optional. Message is a photo, available sizes of the photo |
111
|
|
|
* |
112
|
|
|
* This method overrides the default getPhoto method |
113
|
|
|
* and returns a nice array of PhotoSize objects. |
114
|
|
|
* |
115
|
|
|
* @return null|PhotoSize[] |
116
|
|
|
*/ |
117
|
6 |
|
public function getPhoto() |
118
|
|
|
{ |
119
|
6 |
|
$pretty_array = $this->makePrettyObjectArray(PhotoSize::class, 'photo'); |
120
|
|
|
|
121
|
6 |
|
return empty($pretty_array) ? null : $pretty_array; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Optional. A chat photo was changed to this value |
126
|
|
|
* |
127
|
|
|
* This method overrides the default getNewChatPhoto method |
128
|
|
|
* and returns a nice array of PhotoSize objects. |
129
|
|
|
* |
130
|
|
|
* @return null|PhotoSize[] |
131
|
|
|
*/ |
132
|
6 |
|
public function getNewChatPhoto() |
133
|
|
|
{ |
134
|
6 |
|
$pretty_array = $this->makePrettyObjectArray(PhotoSize::class, 'new_chat_photo'); |
135
|
|
|
|
136
|
6 |
|
return empty($pretty_array) ? null : $pretty_array; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Optional. A new member(s) was added to the group, information about them (one of this members may be the bot itself) |
141
|
|
|
* |
142
|
|
|
* This method overrides the default getNewChatMembers method |
143
|
|
|
* and returns a nice array of User objects. |
144
|
|
|
* |
145
|
|
|
* @return null|User[] |
146
|
|
|
*/ |
147
|
6 |
|
public function getNewChatMembers() |
148
|
|
|
{ |
149
|
6 |
|
$pretty_array = $this->makePrettyObjectArray(User::class, 'new_chat_members'); |
150
|
|
|
|
151
|
6 |
|
return empty($pretty_array) ? null : $pretty_array; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Optional. For text messages, special entities like usernames, URLs, bot commands, etc. that appear in the text |
156
|
|
|
* |
157
|
|
|
* This method overrides the default getEntities method |
158
|
|
|
* and returns a nice array of MessageEntity objects. |
159
|
|
|
* |
160
|
|
|
* @return null|MessageEntity[] |
161
|
|
|
*/ |
162
|
6 |
|
public function getEntities() |
163
|
|
|
{ |
164
|
6 |
|
$pretty_array = $this->makePrettyObjectArray(MessageEntity::class, 'entities'); |
165
|
|
|
|
166
|
6 |
|
return empty($pretty_array) ? null : $pretty_array; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Optional. For messages with a caption, special entities like usernames, URLs, bot commands, etc. that appear in the caption |
171
|
|
|
* |
172
|
|
|
* This method overrides the default getCaptionEntities method |
173
|
|
|
* and returns a nice array of MessageEntity objects. |
174
|
|
|
* |
175
|
|
|
* @return null|MessageEntity[] |
176
|
|
|
*/ |
177
|
6 |
|
public function getCaptionEntities() |
178
|
|
|
{ |
179
|
6 |
|
$pretty_array = $this->makePrettyObjectArray(MessageEntity::class, 'caption_entities'); |
180
|
|
|
|
181
|
6 |
|
return empty($pretty_array) ? null : $pretty_array; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* return the entire command like /echo or /echo@bot1 if specified |
186
|
|
|
* |
187
|
|
|
* @return string|null |
188
|
|
|
*/ |
189
|
2 |
|
public function getFullCommand() |
190
|
|
|
{ |
191
|
2 |
|
$text = $this->getProperty('text'); |
192
|
2 |
|
if (strpos($text, '/') !== 0) { |
193
|
2 |
|
return null; |
194
|
|
|
} |
195
|
|
|
|
196
|
2 |
|
$no_EOL = strtok($text, PHP_EOL); |
197
|
2 |
|
$no_space = strtok($text, ' '); |
198
|
|
|
|
199
|
|
|
//try to understand which separator \n or space divide /command from text |
200
|
2 |
|
return strlen($no_space) < strlen($no_EOL) ? $no_space : $no_EOL; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get command |
205
|
|
|
* |
206
|
|
|
* @return string|null |
207
|
|
|
*/ |
208
|
2 |
|
public function getCommand() |
209
|
|
|
{ |
210
|
2 |
|
if ($command = $this->getProperty('command')) { |
211
|
|
|
return $command; |
212
|
|
|
} |
213
|
|
|
|
214
|
2 |
|
$full_command = $this->getFullCommand(); |
215
|
2 |
|
if (strpos($full_command, '/') !== 0) { |
216
|
2 |
|
return null; |
217
|
|
|
} |
218
|
2 |
|
$full_command = substr($full_command, 1); |
219
|
|
|
|
220
|
|
|
//check if command is followed by bot username |
221
|
2 |
|
$split_cmd = explode('@', $full_command); |
222
|
2 |
|
if (!isset($split_cmd[1])) { |
223
|
|
|
//command is not followed by name |
224
|
2 |
|
return $full_command; |
225
|
|
|
} |
226
|
|
|
|
227
|
1 |
|
if (strtolower($split_cmd[1]) === strtolower($this->getBotUsername())) { |
228
|
|
|
//command is addressed to me |
229
|
1 |
|
return $split_cmd[0]; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return null; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* For text messages, the actual UTF-8 text of the message, 0-4096 characters. |
237
|
|
|
* |
238
|
|
|
* @param bool $without_cmd |
239
|
|
|
* |
240
|
|
|
* @return string |
241
|
|
|
*/ |
242
|
9 |
|
public function getText($without_cmd = false) |
243
|
|
|
{ |
244
|
9 |
|
$text = $this->getProperty('text'); |
245
|
|
|
|
246
|
9 |
|
if ($without_cmd && $command = $this->getFullCommand()) { |
247
|
1 |
|
if (strlen($command) + 1 < strlen($text)) { |
248
|
1 |
|
return substr($text, strlen($command) + 1); |
249
|
|
|
} |
250
|
|
|
|
251
|
1 |
|
return ''; |
252
|
|
|
} |
253
|
|
|
|
254
|
9 |
|
return $text; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Bot added in chat |
259
|
|
|
* |
260
|
|
|
* @return bool |
261
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
262
|
|
|
*/ |
263
|
|
|
public function botAddedInChat() |
264
|
|
|
{ |
265
|
|
|
foreach ($this->getNewChatMembers() as $member) { |
266
|
|
|
if ($member instanceof User && $member->getUsername() === $this->getBotUsername()) { |
267
|
|
|
return true; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
return false; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Detect type based on properties. |
276
|
|
|
* |
277
|
|
|
* @return string |
278
|
|
|
*/ |
279
|
1 |
|
public function getType() |
280
|
|
|
{ |
281
|
|
|
$types = [ |
282
|
1 |
|
'text', |
283
|
|
|
'audio', |
284
|
|
|
'document', |
285
|
|
|
'animation', |
286
|
|
|
'game', |
287
|
|
|
'photo', |
288
|
|
|
'sticker', |
289
|
|
|
'video', |
290
|
|
|
'voice', |
291
|
|
|
'contact', |
292
|
|
|
'location', |
293
|
|
|
'venue', |
294
|
|
|
'new_chat_members', |
295
|
|
|
'left_chat_member', |
296
|
|
|
'new_chat_title', |
297
|
|
|
'new_chat_photo', |
298
|
|
|
'delete_chat_photo', |
299
|
|
|
'group_chat_created', |
300
|
|
|
'supergroup_chat_created', |
301
|
|
|
'channel_chat_created', |
302
|
|
|
'migrate_to_chat_id', |
303
|
|
|
'migrate_from_chat_id', |
304
|
|
|
'pinned_message', |
305
|
|
|
'invoice', |
306
|
|
|
'successful_payment', |
307
|
|
|
]; |
308
|
|
|
|
309
|
1 |
|
$is_command = strlen($this->getCommand()) > 0; |
310
|
1 |
|
foreach ($types as $type) { |
311
|
1 |
|
if ($this->getProperty($type)) { |
312
|
1 |
|
if ($is_command && $type === 'text') { |
313
|
1 |
|
return 'command'; |
314
|
|
|
} |
315
|
|
|
|
316
|
1 |
|
return $type; |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|
320
|
1 |
|
return 'message'; |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|