Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,008 changes: 968 additions & 40 deletions bootstrap/bootstrap.php

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions classes/Commands/ChatCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public function execute(): ServerResponse

foreach (\erLhcoreClassModelTelegramChat::getList(['filter' => ['bot_id' => $tBot->id, 'tchat_id' => $message->getMessageThreadId(), 'type' => 1]]) as $tchat) {

if (!\erLhcoreClassExtensionLhctelegram::isTelegramForumChatMessage($tchat, $chat_id)) {
continue;
}

$chat = $tchat->chat;

if (!($chat instanceof \erLhcoreClassModelChat)) {
Expand Down
9 changes: 7 additions & 2 deletions classes/Commands/EndchatCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,13 @@ public function execute(): ServerResponse

if ($operator instanceof \erLhcoreClassModelTelegramOperator) {

foreach (\erLhcoreClassModelTelegramChat::getList(['filter' => ['bot_id' => $tBot->id, 'tchat_id' => $message->getMessageThreadId(), 'type' => 1]]) as $tchat) {
$chat = $tchat->chat;
foreach (\erLhcoreClassModelTelegramChat::getList(['filter' => ['bot_id' => $tBot->id, 'tchat_id' => $message->getMessageThreadId(), 'type' => 1]]) as $tchat) {

if (!\erLhcoreClassExtensionLhctelegram::isTelegramForumChatMessage($tchat, $chat_id)) {
continue;
}

$chat = $tchat->chat;

if ($chat instanceof \erLhcoreClassModelChat) {

Expand Down
5 changes: 5 additions & 0 deletions classes/Commands/EndchattopicCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public function execute(): ServerResponse
if ($operator instanceof \erLhcoreClassModelTelegramOperator) {

foreach (\erLhcoreClassModelTelegramChat::getList(['filter' => ['bot_id' => $tBot->id, 'tchat_id' => $message->getMessageThreadId(), 'type' => 1]]) as $tchat) {

if (!\erLhcoreClassExtensionLhctelegram::isTelegramForumChatMessage($tchat, $chat_id)) {
continue;
}

$chat = $tchat->chat;

if ($chat instanceof \erLhcoreClassModelChat) {
Expand Down
110 changes: 109 additions & 1 deletion classes/Commands/GenericmessageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,19 @@ public function execute(): ServerResponse

foreach (\erLhcoreClassModelTelegramChat::getList(['filter' => ['bot_id' => $tBot->id, 'tchat_id' => $message->getMessageThreadId(), 'type' => 1]]) as $tchat) {

// Telegram message/thread IDs are only unique within one chat.
if (!\erLhcoreClassExtensionLhctelegram::isTelegramForumChatMessage($tchat, $chat_id)) {
continue;
}

$chat = $tchat->chat;

if ($chat instanceof \erLhcoreClassModelChat) {
$topicContext = array(
'bot_id' => (int)$tBot->id,
'group_chat_id' => (string)$chat_id
);
$topicNamespace = \erLhcoreClassExtensionLhctelegram::getTelegramTopicNamespace($topicContext['bot_id'], $topicContext['group_chat_id']);

if ($type === 'photo') {
$text = $this->appendCaptionToFileEmbed($message, $this->processPhoto($chat, $message, $tBot));
Expand Down Expand Up @@ -341,7 +351,105 @@ public function execute(): ServerResponse

if ($ignoreMessage == false) {
$msg = new \erLhcoreClassModelmsg();
$msg->msg = $text;
$msgText = $text;
$metaMsg = [];

$replyData = \erLhcoreClassExtensionLhctelegram::extractTelegramReplyData($message);
$isExplicitReply = !empty($replyData['is_explicit_reply']) && (int)($replyData['reply_message_id'] ?? 0) > 0;
$telegramMessageId = (int)($replyData['message_id'] ?? 0);

if ($telegramMessageId > 0) {
$metaMsg['tg_topic_msg_id'] = $telegramMessageId;
$metaMsg['tg_topic_msg_contexts'] = array(
$topicNamespace => array(
'ids' => array($telegramMessageId),
'latest_id' => $telegramMessageId,
'bot_id' => $topicContext['bot_id'],
'group_chat_id' => $topicContext['group_chat_id'],
'map' => array(
(string)$telegramMessageId => array(
'text' => $this->stripTelegramFileEmbeds($text),
'kind' => 'text'
)
)
)
);
}

if ($isExplicitReply) {
$replyTopicMsgId = (int)$replyData['reply_message_id'];
$db = \ezcDbInstance::get();
$topicMessageIdsPath = '$.tg_topic_msg_contexts.' . $topicNamespace . '.ids';
$replyMsg = \erLhcoreClassModelmsg::findOne([
'filter' => ['chat_id' => $chat->id],
'customfilter' => [
'`meta_msg` != \'\' AND JSON_VALID(`meta_msg`) AND JSON_CONTAINS(JSON_EXTRACT(meta_msg, ' . $db->quote($topicMessageIdsPath) . '), ' . $db->quote(json_encode(array($replyTopicMsgId))) . ')'
]
]);

if (!($replyMsg instanceof \erLhcoreClassModelmsg)) {
$replyMsg = \erLhcoreClassModelmsg::findOne([
'filter' => ['chat_id' => $chat->id],
'customfilter' => [
'meta_msg != \'\' AND JSON_VALID(meta_msg) AND JSON_EXTRACT(meta_msg, \'$.tg_topic_msg_contexts\') IS NULL AND (JSON_EXTRACT(meta_msg, \'$.tg_topic_msg_id\') = ' . $replyTopicMsgId . ' OR JSON_CONTAINS(JSON_EXTRACT(meta_msg, \'$.tg_topic_msg_ids\'), \'[' . $replyTopicMsgId . ']\'))'
]
]);
}

if ($replyMsg instanceof \erLhcoreClassModelmsg) {
$replyMsgMeta = is_array($replyMsg->meta_msg_array) ? $replyMsg->meta_msg_array : array();
if (empty($replyMsgMeta) && isset($replyMsg->meta_msg) && is_string($replyMsg->meta_msg)) {
$decodedReplyMeta = json_decode($replyMsg->meta_msg, true);
if (is_array($decodedReplyMeta)) {
$replyMsgMeta = $decodedReplyMeta;
}
}
$replyExternalId = trim((string)($replyMsgMeta['iwh_msg_id'] ?? ''));
$replyReference = \erLhcoreClassExtensionLhctelegram::buildTelegramReplyReference(
$replyMsg->id,
$replyTopicMsgId,
$replyExternalId
);

// Keep the local quote/reply target even when the
// original LHC message has no external visitor ID.
// The REST core only consumes iwh_msg_id from its
// numeric quote marker; this metadata is consumed
// by the Telegram extension for direct topic replies.
$metaMsg['content']['reply_to'] = $replyReference;
$quoteText = trim((string)($replyData['quote_text'] ?? ''));
if ($quoteText === '') {
$quoteText = \erLhcoreClassExtensionLhctelegram::getStoredTelegramMessageText($replyMsg, $replyTopicMsgId, $topicContext);
}
if ($quoteText === '') {
$quoteText = html_entity_decode(
preg_replace('/\[file=\d+_[a-z0-9]+\]/i', '', (string)$replyMsg->msg),
ENT_QUOTES | ENT_HTML5,
'UTF-8'
);
$quoteText = trim($quoteText);
}
$quoteText = \erLhcoreClassExtensionLhctelegram::normalizeTelegramQuoteText($quoteText);
$replyNick = $replyMsg->name_support != '' ? $replyMsg->name_support : $chat->nick;
$msgText = \erLhcoreClassExtensionLhctelegram::formatTelegramQuotedText(
$msgText,
$replyMsg->id,
$quoteText,
$replyExternalId
);
$metaMsg['content']['quote'] = [
'id' => $replyMsg->id,
'text' => $quoteText,
'nick' => $replyNick
];
}
}

$msg->msg = $msgText;
if (!empty($metaMsg)) {
$msg->meta_msg = json_encode($metaMsg);
$msg->meta_msg_array = $metaMsg;
}
$msg->chat_id = $chat->id;
$msg->user_id = $messageUserId;
$msg->time = time();
Expand Down
Loading