2023-06-28 15:50:38 +02:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace danog\MadelineProto\EventHandler;
|
|
|
|
|
2023-07-01 19:14:53 +02:00
|
|
|
use danog\MadelineProto\EventHandler\Keyboard\InlineKeyboard;
|
|
|
|
use danog\MadelineProto\EventHandler\Keyboard\ReplyKeyboard;
|
2023-07-05 21:28:17 +02:00
|
|
|
use danog\MadelineProto\EventHandler\Media\Audio;
|
|
|
|
use danog\MadelineProto\EventHandler\Media\Document;
|
|
|
|
use danog\MadelineProto\EventHandler\Media\DocumentPhoto;
|
|
|
|
use danog\MadelineProto\EventHandler\Media\Gif;
|
|
|
|
use danog\MadelineProto\EventHandler\Media\MaskSticker;
|
|
|
|
use danog\MadelineProto\EventHandler\Media\Photo;
|
|
|
|
use danog\MadelineProto\EventHandler\Media\RoundVideo;
|
|
|
|
use danog\MadelineProto\EventHandler\Media\Sticker;
|
|
|
|
use danog\MadelineProto\EventHandler\Media\Video;
|
|
|
|
use danog\MadelineProto\EventHandler\Media\Voice;
|
2023-06-28 15:50:38 +02:00
|
|
|
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-06 21:39:20 +02:00
|
|
|
abstract class Message extends Update
|
2023-06-28 15:50:38 +02:00
|
|
|
{
|
2023-07-06 21:39:20 +02:00
|
|
|
/** Message ID */
|
|
|
|
public readonly int $id;
|
2023-06-28 15:50:38 +02:00
|
|
|
/** Content of the message */
|
|
|
|
public readonly string $message;
|
2023-07-06 21:39:20 +02:00
|
|
|
/** ID of the chat where the message was sent */
|
|
|
|
public readonly int $chatId;
|
|
|
|
/** ID of the message to which this message is replying */
|
|
|
|
public readonly ?int $replyToMsgId;
|
|
|
|
/** When was the message sent */
|
|
|
|
public readonly int $date;
|
2023-06-28 15:50:38 +02:00
|
|
|
|
2023-07-01 19:06:30 +02:00
|
|
|
/** Info about a forwarded message */
|
|
|
|
public readonly ?ForwardedInfo $fwdInfo;
|
|
|
|
|
2023-07-06 21:39:20 +02:00
|
|
|
/** ID of the forum topic where the message was sent */
|
|
|
|
public readonly ?int $topicId;
|
|
|
|
|
|
|
|
/** ID of the message thread where the message was sent */
|
|
|
|
public readonly ?int $threadId;
|
|
|
|
|
2023-07-05 22:00:57 +02:00
|
|
|
/** Bot command (if present) */
|
|
|
|
public readonly ?string $command;
|
|
|
|
/** @var list<string> Bot command arguments (if present) */
|
|
|
|
public readonly ?array $commandArgs;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
*
|
|
|
|
* @var list<string> Regex matches, if a filter regex is present
|
|
|
|
*/
|
|
|
|
public ?array $matches = null;
|
|
|
|
|
2023-07-05 21:28:17 +02:00
|
|
|
/**
|
|
|
|
* Attached media.
|
|
|
|
*
|
|
|
|
* @var Audio|Document|DocumentPhoto|Gif|MaskSticker|Photo|RoundVideo|Sticker|Video|Voice|null
|
|
|
|
*/
|
2023-07-04 19:48:23 +02:00
|
|
|
public readonly ?Media $media;
|
|
|
|
|
2023-07-06 21:39:20 +02:00
|
|
|
/** Whether this is a reply to a scheduled message */
|
|
|
|
public readonly bool $replyToScheduled;
|
|
|
|
/** Whether we were mentioned in this message */
|
|
|
|
public readonly bool $mentioned;
|
|
|
|
/** Whether this message was sent without any notification (silently) */
|
|
|
|
public readonly bool $silent;
|
2023-06-28 15:50:38 +02:00
|
|
|
/** 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;
|
|
|
|
|
2023-07-06 21:39:20 +02:00
|
|
|
/** Time-to-live of the message */
|
|
|
|
public readonly ?int $ttlPeriod;
|
|
|
|
|
2023-07-01 17:53:29 +02:00
|
|
|
/** Inline or reply keyboard. */
|
2023-07-01 19:14:53 +02:00
|
|
|
public readonly InlineKeyboard|ReplyKeyboard|null $keyboard;
|
2023-07-01 14:39:33 +02:00
|
|
|
|
2023-07-01 19:06:30 +02:00
|
|
|
/** Whether this message was [imported from a foreign chat service](https://core.telegram.org/api/import) */
|
|
|
|
public readonly bool $imported;
|
|
|
|
|
|
|
|
/** For Public Service Announcement messages, the PSA type */
|
|
|
|
public readonly string $psaType;
|
|
|
|
|
2023-07-06 21:59:36 +02:00
|
|
|
/** Service information, for service messages */
|
|
|
|
public readonly ?Service $service;
|
|
|
|
|
2023-07-05 22:00:57 +02:00
|
|
|
// Todo media (waveform, photosizes, thumbs), albums, reactions, replies, games eventually, service messages
|
2023-06-28 15:50:38 +02:00
|
|
|
|
|
|
|
/** @internal */
|
2023-07-04 19:48:23 +02:00
|
|
|
public function __construct(
|
2023-06-28 15:50:38 +02:00
|
|
|
MTProto $API,
|
2023-07-04 18:19:06 +02:00
|
|
|
array $rawMessage,
|
2023-07-06 21:39:20 +02:00
|
|
|
/** Whether the message is outgoing */
|
|
|
|
public readonly bool $out
|
2023-06-28 15:50:38 +02:00
|
|
|
) {
|
2023-07-06 21:39:20 +02:00
|
|
|
parent::__construct($API);
|
|
|
|
$info = $this->API->getInfo($rawMessage);
|
2023-06-28 15:50:38 +02:00
|
|
|
|
2023-07-01 19:06:30 +02:00
|
|
|
$this->entities = $rawMessage['entities'] ?? null;
|
2023-07-06 21:39:20 +02:00
|
|
|
$this->id = $rawMessage['id'];
|
2023-06-28 15:50:38 +02:00
|
|
|
$this->message = $rawMessage['message'] ?? '';
|
2023-07-06 21:39:20 +02:00
|
|
|
$this->chatId = $info['bot_api_id'];
|
|
|
|
$this->date = $rawMessage['date'];
|
|
|
|
$this->mentioned = $rawMessage['mentioned'];
|
|
|
|
$this->silent = $rawMessage['silent'];
|
2023-06-28 15:50:38 +02:00
|
|
|
$this->fromScheduled = $rawMessage['from_scheduled'];
|
2023-07-06 20:58:12 +02:00
|
|
|
$this->pinned = $rawMessage['pinned'] ?? false;
|
|
|
|
$this->protected = $rawMessage['noforwards'] ?? false;
|
2023-06-28 15:50:38 +02:00
|
|
|
$this->viaBotId = $rawMessage['via_bot_id'] ?? null;
|
2023-07-01 13:04:59 +02:00
|
|
|
$this->editDate = $rawMessage['edit_date'] ?? null;
|
2023-07-06 21:39:20 +02:00
|
|
|
$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 18:45:35 +02:00
|
|
|
|
2023-07-06 21:39:20 +02:00
|
|
|
if (isset($rawMessage['reply_to'])) {
|
|
|
|
$replyTo = $rawMessage['reply_to'];
|
|
|
|
$this->replyToScheduled = $replyTo['reply_to_scheduled'];
|
|
|
|
if ($replyTo['forum_topic']) {
|
|
|
|
if (isset($replyTo['reply_to_top_id'])) {
|
|
|
|
$this->topicId = $replyTo['reply_to_top_id'];
|
|
|
|
$this->replyToMsgId = $replyTo['reply_to_msg_id'];
|
|
|
|
} else {
|
|
|
|
$this->topicId = $replyTo['reply_to_msg_id'];
|
|
|
|
$this->replyToMsgId = null;
|
|
|
|
}
|
|
|
|
$this->threadId = null;
|
|
|
|
} elseif ($info['Chat']['forum'] ?? false) {
|
|
|
|
$this->topicId = 1;
|
|
|
|
$this->replyToMsgId = $replyTo['reply_to_msg_id'];
|
|
|
|
$this->threadId = $replyTo['reply_to_top_id'] ?? null;
|
|
|
|
} else {
|
|
|
|
$this->topicId = null;
|
|
|
|
$this->replyToMsgId = $replyTo['reply_to_msg_id'];
|
|
|
|
$this->threadId = $replyTo['reply_to_top_id'] ?? null;
|
|
|
|
}
|
|
|
|
} elseif ($info['Chat']['forum'] ?? false) {
|
|
|
|
$this->topicId = 1;
|
|
|
|
$this->replyToMsgId = null;
|
|
|
|
$this->threadId = null;
|
|
|
|
$this->replyToScheduled = false;
|
|
|
|
} else {
|
|
|
|
$this->topicId = null;
|
|
|
|
$this->replyToMsgId = null;
|
|
|
|
$this->threadId = null;
|
|
|
|
$this->replyToScheduled = false;
|
|
|
|
}
|
|
|
|
|
2023-07-01 19:06:30 +02:00
|
|
|
if (isset($rawMessage['fwd_from'])) {
|
|
|
|
$fwdFrom = $rawMessage['fwd_from'];
|
|
|
|
$this->fwdInfo = new ForwardedInfo(
|
|
|
|
$fwdFrom['date'],
|
|
|
|
isset($fwdFrom['from_id'])
|
|
|
|
? $this->API->getIdInternal($fwdFrom['from_id'])
|
|
|
|
: null,
|
|
|
|
$fwdFrom['from_name'] ?? null,
|
|
|
|
$fwdFrom['channel_post'] ?? null,
|
|
|
|
$fwdFrom['post_author'] ?? null,
|
|
|
|
isset($fwdFrom['saved_from_peer'])
|
|
|
|
? $this->API->getIdInternal($fwdFrom['saved_from_peer'])
|
|
|
|
: null,
|
|
|
|
$fwdFrom['saved_from_msg_id'] ?? null
|
|
|
|
);
|
|
|
|
$this->psaType = $fwdFrom['psa_type'] ?? null;
|
|
|
|
} else {
|
|
|
|
$this->fwdInfo = null;
|
|
|
|
$this->psaType = null;
|
|
|
|
}
|
2023-07-05 21:28:17 +02:00
|
|
|
|
|
|
|
$this->media = isset($rawMessage['media'])
|
|
|
|
? $API->wrapMedia($rawMessage['media'])
|
|
|
|
: null;
|
2023-07-05 22:00:57 +02:00
|
|
|
|
|
|
|
if (
|
|
|
|
$this->entities
|
|
|
|
&& $this->entities[0]['_'] === 'messageEntityBotCommand'
|
|
|
|
&& $this->entities[0]['offset'] === 0
|
|
|
|
) {
|
|
|
|
$this->command = StrTools::mbSubstr(
|
|
|
|
$this->message,
|
|
|
|
1,
|
|
|
|
$this->entities[0]['length']-1
|
|
|
|
);
|
|
|
|
$this->commandArgs = \explode(
|
|
|
|
' ',
|
|
|
|
StrTools::mbSubstr($this->message, $this->entities[0]['length']+1)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$this->command = null;
|
|
|
|
$this->commandArgs = null;
|
|
|
|
}
|
2023-07-06 21:59:36 +02:00
|
|
|
|
|
|
|
$this->service = $rawMessage['action'] ?? 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 19:06:30 +02:00
|
|
|
private readonly ?array $entities;
|
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 19:06:30 +02:00
|
|
|
if (!$this->entities) {
|
2023-07-04 18:19:06 +02:00
|
|
|
return \htmlentities($this->message);
|
2023-07-01 19:06:30 +02:00
|
|
|
}
|
2023-07-01 17:53:29 +02:00
|
|
|
if ($allowTelegramTags) {
|
2023-07-07 16:56:03 +02:00
|
|
|
return $this->htmlTelegram ??= StrTools::entitiesToHtml($this->message, $this->entities, $allowTelegramTags);
|
2023-07-01 17:53:29 +02:00
|
|
|
}
|
2023-07-07 16:56:03 +02:00
|
|
|
return $this->html ??= StrTools::entitiesToHtml($this->message, $this->entities, $allowTelegramTags);
|
2023-06-28 15:50:38 +02:00
|
|
|
}
|
|
|
|
}
|