Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TelegramBridge] Fix issues & add support for location messages #2133

Merged
merged 11 commits into from
Apr 6, 2022
97 changes: 68 additions & 29 deletions bridges/TelegramBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TelegramBridge extends BridgeAbstract {
'name' => 'Username',
'type' => 'text',
'required' => true,
'exampleValue' => '@telegram',
'exampleValue' => '@rssbridge',
)
)
);
Expand All @@ -36,13 +36,13 @@ public function detectParameters($url) {

public function collectData() {

$html = getSimpleHTMLDOM($this->getURI())
or returnServerError('Could not request: ' . $this->getURI());
$html = getSimpleHTMLDOM($this->getURI());

$channelTitle = htmlspecialchars_decode(
$html->find('div.tgme_channel_info_header_title span', 0)->plaintext,
ENT_QUOTES
);

$this->feedName = $channelTitle . ' (@' . $this->processUsername() . ')';

foreach($html->find('div.tgme_widget_message_wrap.js-widget_message_wrap') as $index => $messageDiv) {
Expand All @@ -64,7 +64,6 @@ public function collectData() {
}

public function getURI() {

if (!is_null($this->getInput('username'))) {
return self::URI . '/s/' . $this->processUsername();
}
Expand All @@ -73,7 +72,6 @@ public function getURI() {
}

public function getName() {

if (!empty($this->feedName)) {
return $this->feedName . ' - Telegram';
}
Expand All @@ -82,7 +80,6 @@ public function getName() {
}

private function processUsername() {

if (substr($this->getInput('username'), 0, 1) === '@') {
return substr($this->getInput('username'), 1);
}
Expand All @@ -94,6 +91,11 @@ private function processUri($messageDiv) {
return $messageDiv->find('a.tgme_widget_message_date', 0)->href;
}

private function processDate($messageDiv) {
$messageMeta = $messageDiv->find('span.tgme_widget_message_meta', 0);
return $messageMeta->find('time', 0)->datetime;
}

private function processContent($messageDiv) {
$message = '';

Expand All @@ -102,7 +104,7 @@ private function processContent($messageDiv) {
}

if ($messageDiv->find('a.tgme_widget_message_reply', 0)) {
$message = $this->processReply($messageDiv);
$message .= $this->processReply($messageDiv);
}

if ($messageDiv->find('div.tgme_widget_message_sticker_wrap', 0)) {
Expand Down Expand Up @@ -132,46 +134,62 @@ private function processContent($messageDiv) {
$messageDiv->find('div.tgme_widget_message_text.js-message_text', 0)->plaintext
);
}

if ($messageDiv->find('div.tgme_widget_message_document', 0)) {
$message .= 'Attachments:';
foreach ($messageDiv->find('div.tgme_widget_message_document') as $attachments) {
$message .= $attachments->find('div.tgme_widget_message_document_title.accent_color', 0);
}
$message .= $this->processAttachment($messageDiv);
}

if ($messageDiv->find('a.tgme_widget_message_link_preview', 0)) {
$message .= $this->processLinkPreview($messageDiv);
}

if ($messageDiv->find('a.tgme_widget_message_location_wrap', 0)) {
$message .= $this->processLocation($messageDiv);
}

return $message;
}

private function processReply($messageDiv) {

$reply = $messageDiv->find('a.tgme_widget_message_reply', 0);
$author = $reply->find('span.tgme_widget_message_author_name', 0)->plaintext;
$text = '';

if ($reply->find('div.tgme_widget_message_metatext', 0)) {
$text = $reply->find('div.tgme_widget_message_metatext', 0)->innertext;
}

if ($reply->find('div.tgme_widget_message_text', 0)) {
$text = $reply->find('div.tgme_widget_message_text', 0)->innertext;
}

return <<<EOD
<blockquote>{$reply->find('span.tgme_widget_message_author_name', 0)->plaintext}<br>
{$reply->find('div.tgme_widget_message_text', 0)->innertext}
<blockquote>{$author}<br>
{$text}
<a href="{$reply->href}">{$reply->href}</a></blockquote><hr>
EOD;
}

private function processSticker($messageDiv) {

if (empty($this->itemTitle)) {
$this->itemTitle = '@' . $this->processUsername() . ' posted a sticker';
}

$stickerDiv = $messageDiv->find('div.tgme_widget_message_sticker_wrap', 0);

preg_match($this->backgroundImageRegex, $stickerDiv->find('i', 0)->style, $sticker);
if ($stickerDiv->find('picture', 0)) {
$stickerDiv->find('picture', 0)->find('div', 0)->style = '';
$stickerDiv->find('picture', 0)->style = '';

$this->enclosures[] = $sticker[1];
return $stickerDiv;

return <<<EOD
<a href="{$stickerDiv->children(0)->herf}"><img src="{$sticker[1]}"></a>
} elseif (preg_match($this->backgroundImageRegex, $stickerDiv->find('i', 0)->style, $sticker)) {
$this->enclosures[] = $sticker[1];

return <<<EOD
<a href="{$stickerDiv->children(0)->herf}"><img src="{$sticker[1]}"></a>
EOD;
}
}

private function processPoll($messageDiv) {
Expand Down Expand Up @@ -199,7 +217,6 @@ private function processPoll($messageDiv) {
}

private function processLinkPreview($messageDiv) {

$image = '';
$title = '';
$site = '';
Expand Down Expand Up @@ -231,13 +248,12 @@ private function processLinkPreview($messageDiv) {
}

return <<<EOD
<blockquote><a href="{$preview->href}">$image</a><br><a href="{$preview->href}">
<blockquote><a href="{$preview->href}">{$image}</a><br><a href="{$preview->href}">
{$title} - {$site}</a><br>{$description}</blockquote>
EOD;
}

private function processVideo($messageDiv) {

if (empty($this->itemTitle)) {
$this->itemTitle = '@' . $this->processUsername() . ' posted a video';
}
Expand All @@ -251,14 +267,13 @@ private function processVideo($messageDiv) {
$this->enclosures[] = $photo[1];

return <<<EOD
<video controls="" poster="{$photo[1]}" preload="none">
<video controls="" poster="{$photo[1]}" style="max-width:100%;" preload="none">
<source src="{$messageDiv->find('video', 0)->src}" type="video/mp4">
</video>
EOD;
}

private function processPhoto($messageDiv) {

if (empty($this->itemTitle)) {
$this->itemTitle = '@' . $this->processUsername() . ' posted a photo';
}
Expand All @@ -278,7 +293,6 @@ private function processPhoto($messageDiv) {
}

private function processNotSupported($messageDiv) {

if (empty($this->itemTitle)) {
$this->itemTitle = '@' . $this->processUsername() . ' posted a video';
}
Expand All @@ -299,15 +313,40 @@ private function processNotSupported($messageDiv) {
EOD;
}

private function processDate($messageDiv) {
private function processAttachment($messageDiv) {
$attachments = 'File attachments:<br>';

$messageMeta = $messageDiv->find('span.tgme_widget_message_meta', 0);
return $messageMeta->find('time', 0)->datetime;
if (empty($this->itemTitle)) {
$this->itemTitle = '@' . $this->processUsername() . ' posted an attachment';
}

foreach ($messageDiv->find('div.tgme_widget_message_document') as $document) {
$attachments .= <<<EOD
{$document->find('div.tgme_widget_message_document_title', 0)->plaintext} -
{$document->find('div.tgme_widget_message_document_extra', 0)->plaintext}<br>
EOD;
}

return $attachments;
}

private function ellipsisTitle($text) {
private function processLocation($messageDiv) {
if (empty($this->itemTitle)) {
$this->itemTitle = '@' . $this->processUsername() . ' posted a location';
}

preg_match($this->backgroundImageRegex, $messageDiv->find('div.tgme_widget_message_location', 0)->style, $image);

$link = $messageDiv->find('a.tgme_widget_message_location_wrap', 0)->href;

$this->enclosures[] = $image[1];

return <<<EOD
<a href="{$link}"><img src="{$image[1]}"></a>
EOD;
}

private function ellipsisTitle($text) {
$length = 100;

if (strlen($text) > $length) {
Expand Down