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