1
0
mirror of https://github.com/danog/MadelineProto.git synced 2025-01-11 15:48:18 +01:00
MadelineProto/src/EventHandler/Message.php

86 lines
3.0 KiB
PHP
Raw Normal View History

2023-06-28 15:50:38 +02:00
<?php declare(strict_types=1);
namespace danog\MadelineProto\EventHandler;
use danog\MadelineProto\MTProto;
2023-07-01 14:39:33 +02:00
use danog\MadelineProto\StrTools;
2023-06-28 15:50:38 +02:00
/**
* Represents an incoming or outgoing message.
*/
2023-07-01 13:04:59 +02:00
abstract class Message extends Update
2023-06-28 15:50:38 +02:00
{
/** Message ID */
public readonly int $id;
/** Content of the message */
public readonly string $message;
/** ID of the chat where the message was sent */
public readonly int $chatId;
2023-07-01 13:04:59 +02:00
/** When was the message sent */
2023-06-28 15:50:38 +02:00
public readonly int $date;
/** Whether we were mentioned in this message */
public readonly bool $mentioned;
/** Whether this message was sent without any notification (silently) */
public readonly bool $silent;
/** Whether this message is a sent scheduled message */
public readonly bool $fromScheduled;
/** Whether this message is a pinned message */
public readonly bool $pinned;
/** Whether this message is protected (and thus can't be forwarded or downloaded) */
public readonly bool $protected;
2023-07-01 13:04:59 +02:00
/** If the message was generated by an inline query, ID of the bot that generated it */
2023-06-28 15:50:38 +02:00
public readonly ?int $viaBotId;
2023-07-01 13:04:59 +02:00
/** Last edit date of the message */
public readonly ?int $editDate;
/** Time-to-live of the message */
public readonly ?int $ttlPeriod;
2023-07-01 17:53:29 +02:00
/** Inline or reply keyboard. */
public readonly ?Keyboard $keyboard;
2023-07-01 14:39:33 +02:00
2023-07-01 18:04:20 +02:00
// Todo media, albums, reactions, replies, reply_to, fwd_from
2023-06-28 15:50:38 +02:00
/** @internal */
2023-07-01 18:04:20 +02:00
protected function __construct(
2023-06-28 15:50:38 +02:00
MTProto $API,
2023-07-01 13:04:59 +02:00
public readonly array $rawMessage
2023-06-28 15:50:38 +02:00
) {
2023-07-01 13:04:59 +02:00
parent::__construct($API);
2023-06-28 15:50:38 +02:00
$this->id = $rawMessage['id'];
$this->message = $rawMessage['message'] ?? '';
$this->chatId = $this->API->getId($rawMessage);
$this->date = $rawMessage['date'];
$this->mentioned = $rawMessage['mentioned'];
$this->silent = $rawMessage['silent'];
$this->fromScheduled = $rawMessage['from_scheduled'];
$this->pinned = $rawMessage['pinned'];
$this->protected = $rawMessage['noforwards'];
$this->viaBotId = $rawMessage['via_bot_id'] ?? null;
2023-07-01 13:04:59 +02:00
$this->editDate = $rawMessage['edit_date'] ?? null;
$this->ttlPeriod = $rawMessage['ttl_period'] ?? null;
2023-07-01 17:53:29 +02:00
$this->keyboard = isset($rawMessage['reply_markup'])
? Keyboard::fromRawReplyMarkup($rawMessage['reply_markup'])
: null;
2023-07-01 14:39:33 +02:00
}
2023-07-01 17:53:29 +02:00
private readonly string $html;
private readonly string $htmlTelegram;
2023-07-01 14:39:33 +02:00
/**
* Get an HTML version of the message.
*
* @param bool $allowTelegramTags Whether to allow telegram-specific tags like tg-spoiler, tg-emoji, mention links and so on...
*/
public function getHTML(bool $allowTelegramTags = false): string
{
2023-07-01 17:53:29 +02:00
if ($allowTelegramTags) {
return $this->htmlTelegram ??= StrTools::messageEntitiesToHtml($this->message, $this->rawMessage['entities'], $allowTelegramTags);
}
return $this->html ??= StrTools::messageEntitiesToHtml($this->message, $this->rawMessage['entities'], $allowTelegramTags);
2023-06-28 15:50:38 +02:00
}
}