2019-06-19 23:40:56 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class TelegramBridge extends BridgeAbstract
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2019-06-19 23:40:56 +03:00
|
|
|
const NAME = 'Telegram Bridge';
|
|
|
|
const URI = 'https://t.me';
|
2022-09-06 01:14:20 +03:00
|
|
|
const DESCRIPTION = 'Returns newest posts from a *public* Telegram channel';
|
2019-06-19 23:40:56 +03:00
|
|
|
const MAINTAINER = 'VerifiedJoseph';
|
|
|
|
const PARAMETERS = [[
|
|
|
|
'username' => [
|
|
|
|
'name' => 'Username',
|
|
|
|
'type' => 'text',
|
2019-06-27 21:03:18 +03:00
|
|
|
'required' => true,
|
2019-06-19 23:40:56 +03:00
|
|
|
'exampleValue' => '@rssbridge',
|
2022-07-01 16:10:30 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
2019-06-19 23:40:56 +03:00
|
|
|
const TEST_DETECT_PARAMETERS = [
|
2022-07-06 04:48:49 +03:00
|
|
|
'https://t.me/s/rssbridge' => ['username' => 'rssbridge'],
|
|
|
|
'https://t.me/rssbridge' => ['username' => 'rssbridge'],
|
|
|
|
'http://t.me/rssbridge' => ['username' => 'rssbridge'],
|
|
|
|
'https://telegram.me/s/rssbridge' => ['username' => 'rssbridge'],
|
|
|
|
'https://telegram.me/rssbridge' => ['username' => 'rssbridge'],
|
|
|
|
'http://telegram.me/rssbridge' => ['username' => 'rssbridge'],
|
2023-02-14 00:19:41 +03:00
|
|
|
'http://rssbridge.t.me/' => ['username' => 'rssbridge'],
|
|
|
|
'https://rssbridge.t.me/' => ['username' => 'rssbridge'],
|
2022-07-01 16:10:30 +03:00
|
|
|
];
|
|
|
|
|
2022-10-16 13:03:57 +03:00
|
|
|
const CACHE_TIMEOUT = 60 * 15; // 15 mins
|
2019-06-19 23:40:56 +03:00
|
|
|
private $feedName = '';
|
2022-11-17 20:06:56 +03:00
|
|
|
|
2019-06-19 23:40:56 +03:00
|
|
|
private $enclosures = [];
|
|
|
|
private $itemTitle = '';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-11-17 20:06:56 +03:00
|
|
|
private const BACKGROUND_IMAGE_REGEX = "/background-image:url\('(.*)'\)/";
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-06-19 23:40:56 +03:00
|
|
|
public function collectData()
|
|
|
|
{
|
|
|
|
$html = getSimpleHTMLDOM($this->getURI());
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-09-06 01:14:20 +03:00
|
|
|
$channelTitle = $html->find('div.tgme_channel_info_header_title span', 0)->plaintext ?? '';
|
|
|
|
$channelTitle = htmlspecialchars_decode($channelTitle, ENT_QUOTES);
|
2022-11-17 20:06:56 +03:00
|
|
|
$this->feedName = $channelTitle . ' (@' . $this->normalizeUsername() . ')';
|
2022-09-06 01:14:20 +03:00
|
|
|
$posts = $html->find('div.tgme_widget_message_wrap.js-widget_message_wrap');
|
|
|
|
if (!$channelTitle && !$posts) {
|
|
|
|
throw new \Exception('Unable to find channel. The channel is non-existing or non-public.');
|
|
|
|
}
|
|
|
|
foreach ($posts as $messageDiv) {
|
2019-06-19 23:40:56 +03:00
|
|
|
$this->itemTitle = '';
|
|
|
|
$this->enclosures = [];
|
|
|
|
$item = [];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-11-17 20:06:56 +03:00
|
|
|
$item['uri'] = $messageDiv->find('a.tgme_widget_message_date', 0)->href;
|
2019-06-19 23:40:56 +03:00
|
|
|
$item['content'] = $this->processContent($messageDiv);
|
2020-11-11 21:31:24 +03:00
|
|
|
$item['title'] = $this->itemTitle;
|
2022-11-17 20:06:56 +03:00
|
|
|
$item['timestamp'] = $messageDiv->find('span.tgme_widget_message_meta', 0)->find('time', 0)->datetime;
|
2019-06-19 23:40:56 +03:00
|
|
|
$item['enclosures'] = $this->enclosures;
|
2023-09-20 04:15:15 +03:00
|
|
|
|
|
|
|
$messageOwner = $messageDiv->find('a.tgme_widget_message_owner_name', 0);
|
|
|
|
if ($messageOwner) {
|
|
|
|
$item['author'] = html_entity_decode(trim($messageOwner->plaintext), ENT_QUOTES);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-06-19 23:40:56 +03:00
|
|
|
$this->items[] = $item;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2019-06-19 23:40:56 +03:00
|
|
|
$this->items = array_reverse($this->items);
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2021-03-16 16:04:07 +03:00
|
|
|
private function processContent($messageDiv)
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2019-06-19 23:40:56 +03:00
|
|
|
$message = '';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2024-08-11 02:23:10 +03:00
|
|
|
$notSupported = $messageDiv->find('div.message_media_not_supported_wrap', 0);
|
|
|
|
if ($notSupported) {
|
|
|
|
// For unknown reasons, the telegram preview page omits the content of this post
|
|
|
|
$message = 'RSS-Bridge was unable to find the content of this post.<br><br>' . $notSupported->innertext;
|
|
|
|
}
|
|
|
|
|
2019-06-19 23:40:56 +03:00
|
|
|
if ($messageDiv->find('div.tgme_widget_message_forwarded_from', 0)) {
|
2024-08-11 02:23:10 +03:00
|
|
|
$message .= $messageDiv->find('div.tgme_widget_message_forwarded_from', 0)->innertext . '<br><br>';
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:40:56 +03:00
|
|
|
if ($messageDiv->find('a.tgme_widget_message_reply', 0)) {
|
2022-04-06 11:15:21 +03:00
|
|
|
$message .= $this->processReply($messageDiv);
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2021-03-16 16:04:07 +03:00
|
|
|
if ($messageDiv->find('div.tgme_widget_message_sticker_wrap', 0)) {
|
2022-04-06 11:15:21 +03:00
|
|
|
$message .= $this->processSticker($messageDiv);
|
2021-03-16 16:04:07 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-06-19 23:40:56 +03:00
|
|
|
if ($messageDiv->find('div.tgme_widget_message_poll', 0)) {
|
|
|
|
$message .= $this->processPoll($messageDiv);
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:40:56 +03:00
|
|
|
if ($messageDiv->find('video', 0)) {
|
|
|
|
$message .= $this->processVideo($messageDiv);
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:40:56 +03:00
|
|
|
if ($messageDiv->find('a.tgme_widget_message_photo_wrap', 0)) {
|
|
|
|
$message .= $this->processPhoto($messageDiv);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-06 11:15:21 +03:00
|
|
|
if ($messageDiv->find('a.not_supported', 0)) {
|
|
|
|
$message .= $this->processNotSupported($messageDiv);
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2022-04-06 11:15:21 +03:00
|
|
|
if ($messageDiv->find('div.tgme_widget_message_text.js-message_text', 0)) {
|
|
|
|
$message .= $messageDiv->find('div.tgme_widget_message_text.js-message_text', 0);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-06 11:15:21 +03:00
|
|
|
$this->itemTitle = $this->ellipsisTitle(
|
2019-06-19 23:40:56 +03:00
|
|
|
$messageDiv->find('div.tgme_widget_message_text.js-message_text', 0)->plaintext
|
2022-07-01 16:10:30 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-06 11:15:21 +03:00
|
|
|
if ($messageDiv->find('div.tgme_widget_message_document', 0)) {
|
|
|
|
$message .= $this->processAttachment($messageDiv);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-06-19 23:40:56 +03:00
|
|
|
if ($messageDiv->find('a.tgme_widget_message_link_preview', 0)) {
|
|
|
|
$message .= $this->processLinkPreview($messageDiv);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-06-19 23:40:56 +03:00
|
|
|
if ($messageDiv->find('a.tgme_widget_message_location_wrap', 0)) {
|
|
|
|
$message .= $this->processLocation($messageDiv);
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2022-04-06 11:15:21 +03:00
|
|
|
return $message;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2022-11-17 20:06:56 +03:00
|
|
|
public function getURI()
|
|
|
|
{
|
|
|
|
$username = $this->getInput('username');
|
|
|
|
if ($username) {
|
|
|
|
return self::URI . '/s/' . $this->normalizeUsername();
|
|
|
|
}
|
|
|
|
return parent::getURI();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
if ($this->feedName) {
|
|
|
|
return $this->feedName . ' - Telegram';
|
|
|
|
}
|
|
|
|
return parent::getName();
|
|
|
|
}
|
|
|
|
|
2019-06-19 23:40:56 +03:00
|
|
|
private function processReply($messageDiv)
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2019-06-19 23:40:56 +03:00
|
|
|
$reply = $messageDiv->find('a.tgme_widget_message_reply', 0);
|
2022-04-06 11:15:21 +03:00
|
|
|
$author = $reply->find('span.tgme_widget_message_author_name', 0)->plaintext;
|
|
|
|
$text = '';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-06 11:15:21 +03:00
|
|
|
if ($reply->find('div.tgme_widget_message_metatext', 0)) {
|
|
|
|
$text = $reply->find('div.tgme_widget_message_metatext', 0)->innertext;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-04-06 11:15:21 +03:00
|
|
|
if ($reply->find('div.tgme_widget_message_text', 0)) {
|
|
|
|
$text = $reply->find('div.tgme_widget_message_text', 0)->innertext;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-06-19 23:40:56 +03:00
|
|
|
return <<<EOD
|
2022-04-06 11:15:21 +03:00
|
|
|
<blockquote>{$author}<br>
|
|
|
|
{$text}
|
2019-06-19 23:40:56 +03:00
|
|
|
<a href="{$reply->href}">{$reply->href}</a></blockquote><hr>
|
|
|
|
EOD;
|
|
|
|
}
|
|
|
|
|
2022-04-06 11:15:21 +03:00
|
|
|
private function processSticker($messageDiv)
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2022-11-17 20:06:56 +03:00
|
|
|
if (!$this->itemTitle) {
|
|
|
|
$this->itemTitle = '@' . $this->normalizeUsername() . ' posted a sticker';
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2019-06-19 23:40:56 +03:00
|
|
|
|
2022-04-06 11:15:21 +03:00
|
|
|
$stickerDiv = $messageDiv->find('div.tgme_widget_message_sticker_wrap', 0);
|
2019-06-19 23:40:56 +03:00
|
|
|
|
2022-04-06 11:15:21 +03:00
|
|
|
if ($stickerDiv->find('picture', 0)) {
|
|
|
|
$stickerDiv->find('picture', 0)->find('div', 0)->style = '';
|
|
|
|
$stickerDiv->find('picture', 0)->style = '';
|
|
|
|
|
|
|
|
return $stickerDiv;
|
2023-07-31 21:43:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$var = $stickerDiv->find('i', 0);
|
|
|
|
if ($var) {
|
|
|
|
$style = $var->style;
|
|
|
|
if (preg_match(self::BACKGROUND_IMAGE_REGEX, $style, $sticker)) {
|
|
|
|
return <<<EOD
|
2022-04-06 11:15:21 +03:00
|
|
|
<a href="{$stickerDiv->children(0)->herf}"><img src="{$sticker[1]}"></a>
|
2019-06-19 23:40:56 +03:00
|
|
|
EOD;
|
2023-07-31 21:43:18 +03:00
|
|
|
}
|
2022-04-06 11:15:21 +03:00
|
|
|
}
|
2023-07-31 21:43:18 +03:00
|
|
|
|
|
|
|
return '';
|
2019-06-19 23:40:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function processPoll($messageDiv)
|
|
|
|
{
|
|
|
|
$poll = $messageDiv->find('div.tgme_widget_message_poll', 0);
|
|
|
|
|
|
|
|
$title = $poll->find('div.tgme_widget_message_poll_question', 0)->plaintext;
|
|
|
|
$type = $poll->find('div.tgme_widget_message_poll_type', 0)->plaintext;
|
|
|
|
|
2022-11-17 20:06:56 +03:00
|
|
|
if (!$this->itemTitle) {
|
2019-06-19 23:40:56 +03:00
|
|
|
$this->itemTitle = $title;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2019-06-19 23:40:56 +03:00
|
|
|
|
|
|
|
$pollOptions = '<ul>';
|
|
|
|
|
|
|
|
foreach ($poll->find('div.tgme_widget_message_poll_option') as $option) {
|
|
|
|
$pollOptions .= '<li>' . $option->children(0)->plaintext . ' - ' .
|
|
|
|
$option->find('div.tgme_widget_message_poll_option_text', 0)->plaintext . '</li>';
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2019-06-19 23:40:56 +03:00
|
|
|
$pollOptions .= '</ul>';
|
|
|
|
|
|
|
|
return <<<EOD
|
|
|
|
{$title}<br><small>$type</small><br>{$pollOptions}
|
|
|
|
EOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function processLinkPreview($messageDiv)
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2019-06-19 23:40:56 +03:00
|
|
|
$image = '';
|
|
|
|
$title = '';
|
|
|
|
$site = '';
|
|
|
|
$description = '';
|
|
|
|
|
|
|
|
$preview = $messageDiv->find('a.tgme_widget_message_link_preview', 0);
|
|
|
|
|
|
|
|
if (trim($preview->innertext) === '') {
|
|
|
|
return '';
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2019-06-19 23:40:56 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
if (
|
2019-06-19 23:40:56 +03:00
|
|
|
$preview->find('i', 0) &&
|
2022-11-17 20:06:56 +03:00
|
|
|
preg_match(self::BACKGROUND_IMAGE_REGEX, $preview->find('i', 0)->style, $photo)
|
2022-07-01 16:10:30 +03:00
|
|
|
) {
|
2019-06-19 23:40:56 +03:00
|
|
|
$image = '<img src="' . $photo[1] . '"/>';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($preview->find('div.link_preview_title', 0)) {
|
|
|
|
$title = $preview->find('div.link_preview_title', 0)->plaintext;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($preview->find('div.link_preview_site_name', 0)) {
|
|
|
|
$site = $preview->find('div.link_preview_site_name', 0)->plaintext;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($preview->find('div.link_preview_description', 0)) {
|
|
|
|
$description = $preview->find('div.link_preview_description', 0)->plaintext;
|
|
|
|
}
|
|
|
|
|
|
|
|
return <<<EOD
|
2022-04-06 11:15:21 +03:00
|
|
|
<blockquote><a href="{$preview->href}">{$image}</a><br><a href="{$preview->href}">
|
2019-06-19 23:40:56 +03:00
|
|
|
{$title} - {$site}</a><br>{$description}</blockquote>
|
|
|
|
EOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function processVideo($messageDiv)
|
|
|
|
{
|
2022-11-17 20:06:56 +03:00
|
|
|
if (!$this->itemTitle) {
|
|
|
|
$this->itemTitle = '@' . $this->normalizeUsername() . ' posted a video';
|
2019-06-19 23:40:56 +03:00
|
|
|
}
|
|
|
|
|
2019-07-06 12:52:56 +03:00
|
|
|
if ($messageDiv->find('i.tgme_widget_message_video_thumb')) {
|
2022-11-17 20:06:56 +03:00
|
|
|
preg_match(self::BACKGROUND_IMAGE_REGEX, $messageDiv->find('i.tgme_widget_message_video_thumb', 0)->style, $photo);
|
2019-07-06 12:52:56 +03:00
|
|
|
} elseif ($messageDiv->find('i.link_preview_video_thumb')) {
|
2022-11-17 20:06:56 +03:00
|
|
|
preg_match(self::BACKGROUND_IMAGE_REGEX, $messageDiv->find('i.link_preview_video_thumb', 0)->style, $photo);
|
2022-09-09 21:18:50 +03:00
|
|
|
} elseif ($messageDiv->find('i.tgme_widget_message_roundvideo_thumb')) {
|
2022-11-17 20:06:56 +03:00
|
|
|
preg_match(self::BACKGROUND_IMAGE_REGEX, $messageDiv->find('i.tgme_widget_message_roundvideo_thumb', 0)->style, $photo);
|
2022-10-16 21:26:33 +03:00
|
|
|
} else {
|
|
|
|
// Not all videos have a poster image
|
|
|
|
$photo = [null, null];
|
2019-07-06 12:52:56 +03:00
|
|
|
}
|
2019-06-19 23:40:56 +03:00
|
|
|
|
|
|
|
$this->enclosures[] = $photo[1];
|
|
|
|
|
2022-10-16 21:26:33 +03:00
|
|
|
// Intentionally omitting preload="none" on <video>
|
2019-06-19 23:40:56 +03:00
|
|
|
return <<<EOD
|
2022-10-16 21:26:33 +03:00
|
|
|
<video controls="" poster="{$photo[1]}" style="max-width:100%;">
|
2019-06-19 23:40:56 +03:00
|
|
|
<source src="{$messageDiv->find('video', 0)->src}" type="video/mp4">
|
|
|
|
</video>
|
|
|
|
EOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function processPhoto($messageDiv)
|
|
|
|
{
|
2022-11-17 20:06:56 +03:00
|
|
|
if (!$this->itemTitle) {
|
|
|
|
$this->itemTitle = '@' . $this->normalizeUsername() . ' posted a photo';
|
2019-06-19 23:40:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$photos = '';
|
|
|
|
|
|
|
|
foreach ($messageDiv->find('a.tgme_widget_message_photo_wrap') as $photoWrap) {
|
2022-11-17 20:06:56 +03:00
|
|
|
preg_match(self::BACKGROUND_IMAGE_REGEX, $photoWrap->style, $photo);
|
2019-06-19 23:40:56 +03:00
|
|
|
|
|
|
|
$photos .= <<<EOD
|
|
|
|
<a href="{$photoWrap->href}"><img src="{$photo[1]}"/></a><br>
|
|
|
|
EOD;
|
|
|
|
}
|
|
|
|
return $photos;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-06-23 09:54:52 +03:00
|
|
|
private function processNotSupported($messageDiv)
|
|
|
|
{
|
2022-11-17 20:06:56 +03:00
|
|
|
if (!$this->itemTitle) {
|
|
|
|
$this->itemTitle = '@' . $this->normalizeUsername() . ' posted a video';
|
2019-06-23 09:54:52 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-07-06 12:52:56 +03:00
|
|
|
if ($messageDiv->find('i.tgme_widget_message_video_thumb')) {
|
2022-11-17 20:06:56 +03:00
|
|
|
preg_match(self::BACKGROUND_IMAGE_REGEX, $messageDiv->find('i.tgme_widget_message_video_thumb', 0)->style, $photo);
|
2019-07-06 12:52:56 +03:00
|
|
|
} elseif ($messageDiv->find('i.link_preview_video_thumb')) {
|
2022-11-17 20:06:56 +03:00
|
|
|
preg_match(self::BACKGROUND_IMAGE_REGEX, $messageDiv->find('i.link_preview_video_thumb', 0)->style, $photo);
|
|
|
|
} else {
|
|
|
|
// Unsupported content ususally don't have a preview image
|
|
|
|
$photo = [null, null];
|
2019-07-06 12:52:56 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-06-23 09:54:52 +03:00
|
|
|
return <<<EOD
|
|
|
|
<a href="{$messageDiv->find('a.not_supported', 0)->href}">
|
|
|
|
{$messageDiv->find('div.message_media_not_supported_label', 0)->innertext}<br><br>
|
|
|
|
{$messageDiv->find('span.message_media_view_in_telegram', 0)->innertext}<br><br>
|
|
|
|
<img src="{$photo[1]}"/></a>
|
|
|
|
EOD;
|
|
|
|
}
|
|
|
|
|
2022-04-06 11:15:21 +03:00
|
|
|
private function processAttachment($messageDiv)
|
|
|
|
{
|
|
|
|
$attachments = 'File attachments:<br>';
|
2019-06-22 19:34:02 +03:00
|
|
|
|
2022-11-17 20:06:56 +03:00
|
|
|
if (!$this->itemTitle) {
|
|
|
|
$this->itemTitle = '@' . $this->normalizeUsername() . ' posted an attachment';
|
2022-04-06 11:15:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2019-06-22 19:34:02 +03:00
|
|
|
|
2022-04-06 11:15:21 +03:00
|
|
|
return $attachments;
|
2019-06-19 23:40:56 +03:00
|
|
|
}
|
|
|
|
|
2022-04-06 11:15:21 +03:00
|
|
|
private function processLocation($messageDiv)
|
|
|
|
{
|
2022-11-17 20:06:56 +03:00
|
|
|
if (!$this->itemTitle) {
|
|
|
|
$this->itemTitle = '@' . $this->normalizeUsername() . ' posted a location';
|
2022-04-06 11:15:21 +03:00
|
|
|
}
|
|
|
|
|
2022-11-17 20:06:56 +03:00
|
|
|
preg_match(self::BACKGROUND_IMAGE_REGEX, $messageDiv->find('div.tgme_widget_message_location', 0)->style, $image);
|
2022-04-06 11:15:21 +03:00
|
|
|
|
|
|
|
$link = $messageDiv->find('a.tgme_widget_message_location_wrap', 0)->href;
|
|
|
|
|
|
|
|
return <<<EOD
|
|
|
|
<a href="{$link}"><img src="{$image[1]}"></a>
|
|
|
|
EOD;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-11-17 20:06:56 +03:00
|
|
|
// todo: extract truncate
|
2022-04-06 11:15:21 +03:00
|
|
|
private function ellipsisTitle($text)
|
|
|
|
{
|
2019-06-19 23:40:56 +03:00
|
|
|
$length = 100;
|
|
|
|
if (strlen($text) > $length) {
|
|
|
|
$text = explode('<br>', wordwrap($text, $length, '<br>'));
|
|
|
|
return $text[0] . '...';
|
|
|
|
}
|
|
|
|
return $text;
|
|
|
|
}
|
2022-11-17 20:06:56 +03:00
|
|
|
|
|
|
|
private function normalizeUsername()
|
|
|
|
{
|
|
|
|
// todo: can be replaced with ltrim($username, '@');
|
|
|
|
$username = $this->getInput('username');
|
|
|
|
if (substr($username, 0, 1) === '@') {
|
|
|
|
return substr($username, 1);
|
|
|
|
}
|
|
|
|
return $username;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function detectParameters($url)
|
|
|
|
{
|
2023-02-14 00:19:41 +03:00
|
|
|
$detectParamsRegex = '/^https?:\/\/(?:(?:t|telegram)\.me\/(?:s\/)?([\w]+)|([\w]+)\.t\.me\/?)$/';
|
2022-11-17 20:06:56 +03:00
|
|
|
$params = [];
|
|
|
|
if (preg_match($detectParamsRegex, $url, $matches) > 0) {
|
2023-02-14 00:19:41 +03:00
|
|
|
if ($matches[1] !== '') {
|
|
|
|
$params['username'] = $matches[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($matches[2]) && $matches[2] !== '') {
|
|
|
|
$params['username'] = $matches[2];
|
|
|
|
}
|
|
|
|
|
2022-11-17 20:06:56 +03:00
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2019-06-19 23:40:56 +03:00
|
|
|
}
|