Skip to content
This repository has been archived by the owner on Aug 27, 2022. It is now read-only.

Commit

Permalink
[TelegramBridge] Fix issues & add support for location messages (RSS-…
Browse files Browse the repository at this point in the history
  • Loading branch information
VerifiedJoseph authored and Kwbmm committed Jun 17, 2022
1 parent 7298ca9 commit cdbd9e6
Showing 1 changed file with 67 additions and 27 deletions.
94 changes: 67 additions & 27 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 Down Expand Up @@ -47,6 +47,7 @@ public function collectData() {
$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 @@ -68,7 +69,6 @@ public function collectData() {
}

public function getURI() {

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

public function getName() {

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

private function processUsername() {

if (substr($this->getInput('username'), 0, 1) === '@') {
return substr($this->getInput('username'), 1);
}
Expand All @@ -98,6 +96,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 @@ -106,7 +109,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 @@ -136,46 +139,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 @@ -203,7 +222,6 @@ private function processPoll($messageDiv) {
}

private function processLinkPreview($messageDiv) {

$image = '';
$title = '';
$site = '';
Expand Down Expand Up @@ -235,13 +253,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 @@ -255,14 +272,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 @@ -282,7 +298,6 @@ private function processPhoto($messageDiv) {
}

private function processNotSupported($messageDiv) {

if (empty($this->itemTitle)) {
$this->itemTitle = '@' . $this->processUsername() . ' posted a video';
}
Expand All @@ -303,15 +318,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

0 comments on commit cdbd9e6

Please sign in to comment.