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