Message.php 14.8 KB
Newer Older
MBoretto's avatar
MBoretto committed
1
<?php
Jack'lul's avatar
Jack'lul committed
2
/**
MBoretto's avatar
MBoretto committed
3 4 5 6 7 8
 * This file is part of the TelegramBot package.
 *
 * (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
Jack'lul's avatar
Jack'lul committed
9
 */
MBoretto's avatar
MBoretto committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

namespace Longman\TelegramBot\Entities;

use Longman\TelegramBot\Exception\TelegramException;

class Message extends Entity
{
    protected $message_id;

    protected $from;

    protected $date;

    protected $chat;

    protected $forward_from;

27 28
    protected $forward_from_chat;

MBoretto's avatar
MBoretto committed
29 30
    protected $forward_date;

Jack'lul's avatar
Jack'lul committed
31 32
    protected $edit_date;

MBoretto's avatar
MBoretto committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46
    protected $reply_to_message;

    protected $text;

    protected $audio;

    protected $document;

    protected $photo;

    protected $sticker;

    protected $video;

MBoretto's avatar
MBoretto committed
47 48 49 50
    protected $voice;

    protected $caption;

MBoretto's avatar
MBoretto committed
51 52 53 54
    protected $contact;

    protected $location;

55
    protected $venue;
MBoretto's avatar
MBoretto committed
56

57 58 59
    protected $new_chat_member;

    protected $left_chat_member;
MBoretto's avatar
MBoretto committed
60 61 62 63 64 65 66 67

    protected $new_chat_title;

    protected $new_chat_photo;

    protected $delete_chat_photo;

    protected $group_chat_created;
MBoretto's avatar
MBoretto committed
68 69 70 71 72 73 74 75 76

    protected $supergroup_chat_created;

    protected $channel_chat_created;

    protected $migrate_to_chat_id;

    protected $migrate_from_chat_id;

77 78 79
    protected $pinned_message;

    protected $entities;
MBoretto's avatar
MBoretto committed
80

81
    private $command;
MBoretto's avatar
MBoretto committed
82 83 84

    private $type;

MBoretto's avatar
MBoretto committed
85 86 87 88 89 90
    /**
     * Message constructor.
     *
     * @param array $data
     * @param $bot_name
     */
MBoretto's avatar
MBoretto committed
91
    public function __construct(array $data, $bot_name)
92 93 94 95 96 97 98 99 100 101 102 103
    {

        $this->reply_to_message = isset($data['reply_to_message']) ? $data['reply_to_message'] : null;
        if (!empty($this->reply_to_message)) {
            $this->reply_to_message = new ReplyToMessage($this->reply_to_message, $bot_name);
        }

        $this->init($data, $bot_name);
    }

    //Common init to Message and ReplyToMessage
    protected function init(array & $data, & $bot_name)
MBoretto's avatar
MBoretto committed
104 105
    {
        $this->bot_name = $bot_name;
106

MBoretto's avatar
MBoretto committed
107
        $this->type = 'Message';
MBoretto's avatar
MBoretto committed
108 109 110 111 112 113 114

        $this->message_id = isset($data['message_id']) ? $data['message_id'] : null;
        if (empty($this->message_id)) {
            throw new TelegramException('message_id is empty!');
        }

        $this->from = isset($data['from']) ? $data['from'] : null;
MBoretto's avatar
MBoretto committed
115 116
        if (!empty($this->from)) {
            $this->from = new User($this->from);
MBoretto's avatar
MBoretto committed
117 118 119 120 121 122 123 124
        }

        $this->chat = isset($data['chat']) ? $data['chat'] : null;
        if (empty($this->chat)) {
            throw new TelegramException('chat is empty!');
        }
        $this->chat = new Chat($this->chat);

125 126 127 128 129
        $this->date = isset($data['date']) ? $data['date'] : null;
        if (empty($this->date)) {
            throw new TelegramException('date is empty!');
        }

MBoretto's avatar
MBoretto committed
130 131 132
        $this->forward_from = isset($data['forward_from']) ? $data['forward_from'] : null;
        if (!empty($this->forward_from)) {
            $this->forward_from = new User($this->forward_from);
133 134 135 136 137
        }

        $this->forward_from_chat = isset($data['forward_from_chat']) ? $data['forward_from_chat'] : null;
        if (!empty($this->forward_from_chat)) {
            $this->forward_from_chat = new Chat($this->forward_from_chat);
MBoretto's avatar
MBoretto committed
138 139 140 141
        }

        $this->forward_date = isset($data['forward_date']) ? $data['forward_date'] : null;

Jack'lul's avatar
Jack'lul committed
142 143
        $this->edit_date = isset($data['edit_date']) ? $data['edit_date'] : null;

MBoretto's avatar
MBoretto committed
144 145 146 147 148 149 150 151 152
        $this->text = isset($data['text']) ? $data['text'] : null;
        $command = $this->getCommand();
        if (!empty($command)) {
            $this->type = 'command';
        }

        $this->audio = isset($data['audio']) ? $data['audio'] : null;
        if (!empty($this->audio)) {
            $this->audio = new Audio($this->audio);
MBoretto's avatar
MBoretto committed
153
            $this->type = 'Audio';
MBoretto's avatar
MBoretto committed
154 155 156 157 158
        }

        $this->document = isset($data['document']) ? $data['document'] : null;
        if (!empty($this->document)) {
            $this->document = new Document($this->document);
MBoretto's avatar
MBoretto committed
159
            $this->type = 'Document';
MBoretto's avatar
MBoretto committed
160 161 162 163
        }

        $this->photo = isset($data['photo']) ? $data['photo'] : null; //array of photosize
        if (!empty($this->photo)) {
164 165 166
            foreach ($this->photo as $photo) {
                if (!empty($photo)) {
                    $photos[] = new PhotoSize($photo);
MBoretto's avatar
MBoretto committed
167 168 169
                }
            }
            $this->photo = $photos;
MBoretto's avatar
MBoretto committed
170
            $this->type = 'Photo';
MBoretto's avatar
MBoretto committed
171
        }
MBoretto's avatar
MBoretto committed
172

MBoretto's avatar
MBoretto committed
173 174 175
        $this->sticker = isset($data['sticker']) ? $data['sticker'] : null;
        if (!empty($this->sticker)) {
            $this->sticker = new Sticker($this->sticker);
MBoretto's avatar
MBoretto committed
176
            $this->type = 'Sticker';
MBoretto's avatar
MBoretto committed
177 178 179 180 181
        }

        $this->video = isset($data['video']) ? $data['video'] : null;
        if (!empty($this->video)) {
            $this->video = new Video($this->video);
MBoretto's avatar
MBoretto committed
182
            $this->type = 'Video';
MBoretto's avatar
MBoretto committed
183 184 185 186 187
        }

        $this->voice = isset($data['voice']) ? $data['voice'] : null;
        if (!empty($this->voice)) {
            $this->voice = new Voice($this->voice);
MBoretto's avatar
MBoretto committed
188
            $this->type = 'Voice';
MBoretto's avatar
MBoretto committed
189 190 191 192 193 194 195 196 197 198 199 200
        }

        $this->caption = isset($data['caption']) ? $data['caption'] : null;//string

        $this->contact = isset($data['contact']) ? $data['contact'] : null;
        if (!empty($this->contact)) {
            $this->contact = new Contact($this->contact);
        }

        $this->location = isset($data['location']) ? $data['location'] : null;
        if (!empty($this->location)) {
            $this->location = new Location($this->location);
MBoretto's avatar
MBoretto committed
201
            $this->type = 'Location';
MBoretto's avatar
MBoretto committed
202 203
        }

204 205 206 207
        $this->venue = isset($data['venue']) ? $data['venue'] : null;
        if (!empty($this->venue)) {
            $this->venue = new Venue($this->venue);
            $this->type = 'Venue';
MBoretto's avatar
MBoretto committed
208 209
        }

210 211 212
        //retrocompatibility
        if (isset($data['new_chat_participant'])) {
            $data['new_chat_member'] = $data['new_chat_participant'];
213 214
        }

215 216
        if (isset($data['left_chat_participant'])) {
            $data['left_chat_member'] = $data['left_chat_participant'];
217 218
        }

219 220 221 222 223 224 225 226 227 228
        $this->new_chat_member = isset($data['new_chat_member']) ? $data['new_chat_member'] : null;
        if (!empty($this->new_chat_member)) {
            $this->new_chat_member = new User($this->new_chat_member);
            $this->type = 'new_chat_member';
        }

        $this->left_chat_member = isset($data['left_chat_member']) ? $data['left_chat_member'] : null;
        if (!empty($this->left_chat_member)) {
            $this->left_chat_member = new User($this->left_chat_member);
            $this->type = 'left_chat_member';
MBoretto's avatar
MBoretto committed
229 230 231
        }

        $this->new_chat_title = isset($data['new_chat_title']) ? $data['new_chat_title'] : null;
MBoretto's avatar
MBoretto committed
232
        if (!is_null($this->new_chat_title)) {
MBoretto's avatar
MBoretto committed
233 234 235
            $this->type = 'new_chat_title';
        }

MBoretto's avatar
MBoretto committed
236 237 238 239 240 241 242 243
        $this->new_chat_photo = isset($data['new_chat_photo']) ? $data['new_chat_photo'] : null; //array of photosize
        if (!empty($this->new_chat_photo)) {
            foreach ($this->new_chat_photo as $photo) {
                if (!empty($photo)) {
                    $photos[] = new PhotoSize($photo);
                }
            }
            $this->new_chat_photo = $photos;
244
            $this->type = 'new_chat_photo';
MBoretto's avatar
MBoretto committed
245 246
        }

MBoretto's avatar
MBoretto committed
247 248 249 250 251 252 253 254 255
        $this->delete_chat_photo = isset($data['delete_chat_photo']) ? $data['delete_chat_photo'] : null;
        if ($this->delete_chat_photo) {
            $this->type = 'delete_chat_photo';
        }

        $this->group_chat_created = isset($data['group_chat_created']) ? $data['group_chat_created'] : null;
        if ($this->group_chat_created) {
            $this->type = 'group_chat_created';
        }
256

MBoretto's avatar
MBoretto committed
257 258 259 260 261 262 263 264 265 266 267
        $this->supergroup_chat_created = isset($data['supergroup_chat_created']) ? $data['supergroup_chat_created'] : null;
        if ($this->supergroup_chat_created) {
            $this->type = 'supergroup_chat_created';
        }

        $this->channel_chat_created = isset($data['channel_chat_created']) ? $data['channel_chat_created'] : null;
        if ($this->channel_chat_created) {
            $this->type = 'channel_chat_created';
        }

        $this->migrate_to_chat_id = isset($data['migrate_to_chat_id']) ? $data['migrate_to_chat_id'] : null;
268 269 270
        if ($this->migrate_to_chat_id) {
            $this->type = 'migrate_to_chat_id';
        }
271

MBoretto's avatar
MBoretto committed
272
        $this->migrate_from_chat_id = isset($data['migrate_from_chat_id']) ? $data['migrate_from_chat_id'] : null;
273 274 275
        if ($this->migrate_from_chat_id) {
            $this->type = 'migrate_from_chat_id';
        }
MBoretto's avatar
MBoretto committed
276

277
        $this->pinned_message = isset($data['pinned_message']) ? $data['pinned_message'] : null;
278 279 280
        if ($this->pinned_message) {
            $this->pinned_message = new Message($this->pinned_message, $this->getBotName());
        }
281 282 283 284 285 286 287 288 289 290

        $this->entities = isset($data['entities']) ? $data['entities'] : null;
        if (!empty($this->entities)) {
            foreach ($this->entities as $entity) {
                if (!empty($entity)) {
                    $entities[] = new MessageEntity($entity);
                }
            }
            $this->entities = $entities;
        }
MBoretto's avatar
MBoretto committed
291 292 293 294 295 296
    }

    //return the entire command like /echo or /echo@bot1 if specified
    public function getFullCommand()
    {
        if (substr($this->text, 0, 1) === '/') {
LONGMAN's avatar
LONGMAN committed
297
            $no_EOL = strtok($this->text, PHP_EOL);
298 299 300 301 302 303 304 305
            $no_space = strtok($this->text, ' ');

            //try to understand which separator \n or space divide /command from text
            if (strlen($no_space) < strlen($no_EOL)) {
                return $no_space;
            } else {
                return $no_EOL;
            }
MBoretto's avatar
MBoretto committed
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
        } else {
            return;
        }
    }

    public function getCommand()
    {
        if (!empty($this->command)) {
            return $this->command;
        }

        $cmd = $this->getFullCommand();

        if (substr($cmd, 0, 1) === '/') {
            $cmd = substr($cmd, 1);

            //check if command is follow by botname
            $split_cmd = explode('@', $cmd);
            if (isset($split_cmd[1])) {
                //command is followed by name check if is addressed to me
                if (strtolower($split_cmd[1]) == strtolower($this->bot_name)) {
                    return $this->command = $split_cmd[0];
                }
            } else {
                //command is not followed by name
                return $this->command = $cmd;
            }
        }

        return false;
    }

    public function getMessageId()
    {
        return $this->message_id;
    }

MBoretto's avatar
MBoretto committed
343 344 345 346 347
    /**
     * Get User object related to the  message
     *
     * @return \Longman\TelegramBot\Entities\User
     */
MBoretto's avatar
MBoretto committed
348
    public function getFrom()
MBoretto's avatar
MBoretto committed
349
    {
MBoretto's avatar
MBoretto committed
350
        return $this->from;
MBoretto's avatar
MBoretto committed
351 352
    }

MBoretto's avatar
MBoretto committed
353
    public function getDate()
MBoretto's avatar
MBoretto committed
354
    {
MBoretto's avatar
MBoretto committed
355
        return $this->date;
MBoretto's avatar
MBoretto committed
356 357
    }

MBoretto's avatar
MBoretto committed
358 359 360 361 362
    /**
     * Get User object related to the  message
     *
     * @return \Longman\TelegramBot\Entities\Chat
     */
MBoretto's avatar
MBoretto committed
363 364 365 366 367
    public function getChat()
    {
        return $this->chat;
    }

MBoretto's avatar
MBoretto committed
368 369 370 371 372
    /**
     * Get User object related to the forwarded message
     *
     * @return \Longman\TelegramBot\Entities\User
     */
MBoretto's avatar
MBoretto committed
373 374 375 376 377
    public function getForwardFrom()
    {
        return $this->forward_from;
    }

MBoretto's avatar
MBoretto committed
378 379 380 381 382
    /**
     * Get User object related to the  message
     *
     * @return \Longman\TelegramBot\Entities\Chat
     */
383 384 385 386 387
    public function getForwardFromChat()
    {
        return $this->forward_from_chat;
    }

MBoretto's avatar
MBoretto committed
388 389 390 391 392
    public function getForwardDate()
    {
        return $this->forward_date;
    }

Jack'lul's avatar
Jack'lul committed
393 394 395 396 397
    public function getEditDate()
    {
        return $this->edit_date;
    }

MBoretto's avatar
MBoretto committed
398 399 400 401 402
    public function getReplyToMessage()
    {
        return $this->reply_to_message;
    }

MBoretto's avatar
MBoretto committed
403 404 405
    public function getText($without_cmd = false)
    {
        $text = $this->text;
406 407 408 409 410 411

        if ($without_cmd && $command = $this->getFullCommand()) {
            if (strlen($command) + 1 < strlen($text)) {
                $text = substr($text, strlen($command) + 1);
            } else {
                $text = '';
MBoretto's avatar
MBoretto committed
412 413 414 415 416 417
            }
        }

        return $text;
    }

MBoretto's avatar
MBoretto committed
418 419 420
    /**
     * @return \Longman\TelegramBot\Entities\Audio
     */
MBoretto's avatar
MBoretto committed
421 422 423 424
    public function getAudio()
    {
        return $this->audio;
    }
MBoretto's avatar
MBoretto committed
425 426 427 428

    /**
     * @return \Longman\TelegramBot\Entities\Document
     */
MBoretto's avatar
MBoretto committed
429 430 431 432 433
    public function getDocument()
    {
        return $this->document;
    }

MBoretto's avatar
MBoretto committed
434 435 436
    /**
     * @return array
     */
MBoretto's avatar
MBoretto committed
437 438 439 440 441
    public function getPhoto()
    {
        return $this->photo;
    }

MBoretto's avatar
MBoretto committed
442 443 444
    /**
     * @return \Longman\TelegramBot\Entities\Sticker
     */
MBoretto's avatar
MBoretto committed
445 446 447 448 449
    public function getSticker()
    {
        return $this->sticker;
    }

MBoretto's avatar
MBoretto committed
450 451 452
    /**
     * @return \Longman\TelegramBot\Entities\Video
     */
MBoretto's avatar
MBoretto committed
453 454 455 456
    public function getVideo()
    {
        return $this->video;
    }
MBoretto's avatar
MBoretto committed
457 458 459
    /**
     * @return \Longman\TelegramBot\Entities\Voice
     */
MBoretto's avatar
MBoretto committed
460 461 462 463 464 465 466 467 468 469
    public function getVoice()
    {
        return $this->voice;
    }

    public function getCaption()
    {
        return $this->caption;
    }

MBoretto's avatar
MBoretto committed
470 471 472
    /**
     * @return \Longman\TelegramBot\Entities\Contact
     */
MBoretto's avatar
MBoretto committed
473 474 475 476 477
    public function getContact()
    {
        return $this->contact;
    }

MBoretto's avatar
MBoretto committed
478 479 480
    /**
     * @return \Longman\TelegramBot\Entities\Location
     */
MBoretto's avatar
MBoretto committed
481 482 483 484 485
    public function getLocation()
    {
        return $this->location;
    }

486 487 488 489 490
    public function getVenue()
    {
        return $this->venue;
    }

491 492 493 494 495 496 497 498 499
    public function getNewChatParticipant()
    {
        return $this->new_chat_member;
    }

    public function getLeftChatParticipant()
    {
        return $this->left_chat_member;
    }
MBoretto's avatar
MBoretto committed
500 501 502 503 504
    /**
     * Get User object related to the new member
     *
     * @return \Longman\TelegramBot\Entities\User
     */
505
    public function getNewChatMember()
MBoretto's avatar
MBoretto committed
506
    {
507
        return $this->new_chat_member;
MBoretto's avatar
MBoretto committed
508 509
    }

MBoretto's avatar
MBoretto committed
510 511 512 513 514
    /**
     * Get User object related to the left member
     *
     * @return \Longman\TelegramBot\Entities\User
     */
515
    public function getLeftChatMember()
MBoretto's avatar
MBoretto committed
516
    {
517
        return $this->left_chat_member;
MBoretto's avatar
MBoretto committed
518 519 520 521 522 523 524
    }

    public function getNewChatTitle()
    {
        return $this->new_chat_title;
    }

MBoretto's avatar
MBoretto committed
525 526 527 528 529
    public function getNewChatPhoto()
    {
        return $this->new_chat_photo;
    }

MBoretto's avatar
MBoretto committed
530 531 532 533 534 535 536 537 538 539
    public function getDeleteChatPhoto()
    {
        return $this->delete_chat_photo;
    }

    public function getGroupChatCreated()
    {
        return $this->group_chat_created;
    }

MBoretto's avatar
MBoretto committed
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
    public function getSupergroupChatCreated()
    {
        return $this->supergroup_chat_created;
    }

    public function getChannelChatCreated()
    {
        return $this->channel_chat_created;
    }

    public function getMigrateToChatId()
    {
        return $this->migrate_to_chat_id;
    }

    public function getMigrateFromChatId()
    {
        return $this->migrate_from_chat_id;
    }
MBoretto's avatar
MBoretto committed
559 560 561

    public function botAddedInChat()
    {
562 563
        if (!empty($this->new_chat_member)) {
            if ($this->new_chat_member->getUsername() == $this->getBotName()) {
MBoretto's avatar
MBoretto committed
564 565 566 567 568 569 570 571 572 573 574
                return true;
            }
        }

        return false;
    }

    public function getType()
    {
        return $this->type;
    }
575 576 577 578 579 580 581 582 583 584

    public function getPinnedMessage()
    {
        return $this->pinned_message;
    }

    public function getEntities()
    {
        return $this->entities;
    }
MBoretto's avatar
MBoretto committed
585
}