Skip to content

Commit

Permalink
Feat: sponsored messages support
Browse files Browse the repository at this point in the history
  • Loading branch information
xtrime-ru committed Jan 1, 2022
1 parent ec4fb2e commit 104a2ba
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ RUN apt-get update \
&& docker-php-ext-install -j$(nproc) sockets zip \
&& mkdir -p /usr/src/php/ext/ && cd /usr/src/php/ext/ \
&& pecl bundle openswoole \
&& docker-php-ext-configure openswoole --enable-sockets=yes --enable-openssl=yes \
&& docker-php-ext-configure openswoole --enable-openssl=yes \
&& docker-php-ext-install -j$(nproc) openswoole \
# Cleanup
&& docker-php-source delete \
Expand Down
7 changes: 7 additions & 0 deletions Dockerfile-dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM xtrime/telegram-rss:latest

RUN pecl install xdebug

EXPOSE 9504

ENTRYPOINT php server.php --docker
17 changes: 15 additions & 2 deletions app/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ public function getId($chat) {
return $this->get('getId', [$chat]);
}

public function getSponsoredMessages($peer) {
$messages = (array) $this->get('getSponsoredMessages', $peer);
foreach ($messages as $message) {
$id = $this->getId($message->from_id);
$message->peer = $this->getInfo($id);
}
return $messages;
}

public function viewSponsoredMessage($peer, $message) {
return $this->get('viewSponsoredMessage', ['peer' => $peer, 'message' => $message]);
}

/**
* @param string $method
* @param mixed $parameters
Expand Down Expand Up @@ -187,10 +200,10 @@ private function get(string $method, $parameters = [], array $headers = [], stri
throw new \UnexpectedValueException(static::MESSAGE_CLIENT_UNAVAILABLE, $curl->statusCode);
}

if (!$result = $body->response ?? null) {
if (!property_exists($body, 'response')) {
throw new \UnexpectedValueException(static::MESSAGE_CLIENT_UNAVAILABLE, $curl->statusCode);
}
return $result;
return $body->response;

}
}
6 changes: 5 additions & 1 deletion app/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,10 @@ private function generateResponse(Client $client, Request $request): self
try {
if ($this->request['peer']) {
//Make request to refresh cache.
$isChannel = in_array($client->getInfo($this->request['peer'])->type, ['channel', 'supergroup']);
if (
Config::getInstance()->get('access.only_public_channels') &&
!in_array($client->getInfo($this->request['peer'])->type, ['channel', 'supergroup'])
!$isChannel
) {
throw new UnexpectedValueException('This is not a public channel', 403);
}
Expand Down Expand Up @@ -275,6 +276,9 @@ private function generateResponse(Client $client, Request $request): self
'add_offset' => ($this->request['page'] - 1) * $this->request['limit'],
]
);
if ($isChannel) {
$this->response['data']->sponsored_messages = $client->getSponsoredMessages($this->request['peer']);
}
}
}
} catch (Exception $e) {
Expand Down
51 changes: 35 additions & 16 deletions app/Messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public function __construct(\stdClass $telegramResponse, Client $client, string

private function parseMessages(): self {
if ($messages = $this->telegramResponse->messages ?? []) {
$messages = array_merge($this->telegramResponse->sponsored_messages ?? [], $messages);

$groupedMessages = [];
foreach ($messages as $key => $message) {
if (
Expand All @@ -43,7 +45,7 @@ private function parseMessages(): self {
if ($description || $this->hasMedia($message)) {
$info = $this->getMediaInfo($message);
$parsedMessage = [
'url' => $this->getMessageUrl($message->id),
'url' => $this->getMessageUrl($message),
'title' => null,
'description' => $description,
'media' => [$info],
Expand Down Expand Up @@ -84,7 +86,7 @@ private function parseMessages(): self {

$parsedMessage = $this->setTitle($parsedMessage, $message);

$this->list[$message->id] = $parsedMessage;
$this->list[] = $parsedMessage;
}
}
}
Expand All @@ -102,35 +104,52 @@ private function setTitle(array $parsedMessage, \stdClass $message): array {
$parsedMessage['title'] = $matches['sentence'] ?? null;
$parsedMessage['title'] = trim($parsedMessage['title']);

if ($parsedMessage['title']) {
return $parsedMessage;
}

//Get first 100 symbols from description
$parsedMessage['title'] = mb_strimwidth($descriptionText, 0, 100, ' [...]');
return $parsedMessage;
}

if (!empty($message->media)) {
$mime = $message->media->document->mime_type ?? '';
if (strpos($mime, 'video') !== false) {
$parsedMessage['title'] = '[Video]';
$parsedMessage['title'] = '[Video] ' . $parsedMessage['title'];
} elseif ($message->media->_ === 'messageMediaPhoto') {
$parsedMessage['title'] = '[Photo]';
$parsedMessage['title'] = '[Photo] ' . $parsedMessage['title'];
} else {
$parsedMessage['title'] = '[Media]';
$parsedMessage['title'] = '[Media] ' . $parsedMessage['title'];
}
}

if ($message->_ === 'sponsoredMessage') {
$parsedMessage['title'] = '[Sponsored] ' . $parsedMessage['title'];
}

$parsedMessage['title'] = trim($parsedMessage['title']);

//Get first 100 symbols from description
if (mb_strlen($parsedMessage['title']) > 100) {
$parsedMessage['title'] = mb_strimwidth($descriptionText, 0, 100, ' [...]');
}

return $parsedMessage;
}

/**
* @param string $messageId
* @return string|null
* @param object $message
* @return string
*/
private function getMessageUrl($messageId = '') {
return $this->channelUrl . $messageId;
private function getMessageUrl(object $message): string {
if ($message->_ === 'sponsoredMessage') {
$postId = !empty($message->channel_post) ? '/' . $message->channel_post : '';
$startParam = !empty($message->start_param) ? '/?start=' . $message->start_param : '';
$peer = $message->peer->bot_api_id;
foreach ($message->peer as $property) {
if (property_exists($property, 'username')) {
$peer = $property->username;
break;
}
}
return self::TELEGRAM_URL . $peer . $postId . $startParam;
} else {
return $this->channelUrl . $message->id;
}
}

private function getMediaInfo($message): ?\stdClass {
Expand Down
5 changes: 5 additions & 0 deletions bin/docker-build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

docker buildx build --platform linux/amd64,linux/arm64 -t xtrime/telegram-rss:latest "$@" --push "$SCRIPT_DIR/../"
12 changes: 12 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3.9'
services:
telegram-rss:
image: xtrime/telegram-rss:dev
build:
context: .
dockerfile: Dockerfile-dev
ports:
- "127.0.0.1:9504:9504"
- "9003"
environment:
PHP_IDE_CONFIG: "serverName=Docker"
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '3'
version: '3.9'
services:
telegram-rss:
image: xtrime/telegram-rss:latest
Expand All @@ -17,5 +17,5 @@ services:
max-file: "2"
networks:
default:
external:
name: telegram-api-server
external: true
name: telegram-api-server
5 changes: 5 additions & 0 deletions docker/php/conf.d/xdebug.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
xdebug.max_nesting_level=512
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_host=host.docker.internal
xdebug.client_port=9003

0 comments on commit 104a2ba

Please sign in to comment.