1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-30 10:59:02 +01:00

Add sponsored message API

This commit is contained in:
Daniil Gentili 2021-12-10 18:20:14 +01:00
parent 37efcdfc7a
commit e07a521501
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
5 changed files with 100 additions and 8 deletions

2
docs

@ -1 +1 @@
Subproject commit b01fcdc6714ab071256e5829b25e104cf2d98e40
Subproject commit 0b0c17a38bf999b500d0865230d547e28b3aefa0

View File

@ -126,13 +126,6 @@ class RedisArray extends DriverArray
$this->setCache($index, $value);
/*
$request = $this->db->setMultiple(
[
$this->rKey($index) => \serialize($value),
$this->tsKey($index) => \time()
]
);*/
$request = $this->db->set($this->rKey($index), \serialize($value));
//Ensure that cache is synced with latest insert in case of concurrent requests.

View File

@ -6223,6 +6223,19 @@ class InternalDoc extends APIFactory
{
return $this->__call(__FUNCTION__, []);
}
/**
* Get sponsored messages for channel.
* This method will return an array of [sponsored message objects](https://docs.madelineproto.xyz/API_docs/constructors/sponsoredMessage.html).
*
* See [the API documentation](https://core.telegram.org/api/sponsored-messages) for more info on how to handle sponsored messages.
*
* @param int|array $peer Channel ID, or Update, or Message, or Peer.
* @return \Amp\Promise
*/
public function getSponsoredMessages($peer, array $extra = [])
{
return $this->__call(__FUNCTION__, [$peer, $extra]);
}
/**
* Get TL serializer.
*
@ -7245,6 +7258,18 @@ class InternalDoc extends APIFactory
{
return $this->__call(__FUNCTION__, [$url, $size, $fileName, $cb, $encrypted, $extra]);
}
/**
* Mark sponsored message as read.
*
* @param int|array $peer Channel ID, or Update, or Message, or Peer.
* @param string|array{random_id: string} $message Random ID or sponsored message to mark as read.
*
* @return \Amp\Promise Bool
*/
public function viewSponsoredMessage($peer, $message, array $extra = [])
{
return $this->__call(__FUNCTION__, [$peer, $message, $extra]);
}
/**
* Synchronously wait for a promise|generator.
*

View File

@ -71,6 +71,7 @@ class MTProto extends AsyncConstruct implements TLCallback
use \danog\MadelineProto\TL\Conversion\BotAPIFiles;
use \danog\MadelineProto\TL\Conversion\TD;
use \danog\MadelineProto\VoIP\AuthKeyHandler;
use \danog\MadelineProto\Wrappers\Ads;
use \danog\MadelineProto\Wrappers\Button;
use \danog\MadelineProto\Wrappers\DialogHandler;
use \danog\MadelineProto\Wrappers\Events;
@ -353,6 +354,12 @@ class MTProto extends AsyncConstruct implements TLCallback
* @var DbArray|Promise[]
*/
public $full_chats;
/**
* Sponsored message database.
*
* @var DbArray
*/
public $sponsoredMessages;
/**
* Latest chat message ID map for update handling.
*
@ -526,6 +533,7 @@ class MTProto extends AsyncConstruct implements TLCallback
protected static array $dbProperties = [
'chats' => 'array',
'full_chats' => 'array',
'sponsoredMessages' => 'array',
'channelParticipants' => 'array',
'usernames' => 'array',
'session' => [

View File

@ -0,0 +1,66 @@
<?php
/**
* Ads module.
*
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Daniil Gentili <daniil@daniil.it>
* @copyright 2016-2020 Daniil Gentili <daniil@daniil.it>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
*
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
namespace danog\MadelineProto\Wrappers;
use danog\MadelineProto\Db\DbArray;
/**
* Manages ads.
*
* @property DbArray $sponsoredMessages
*/
trait Ads
{
/**
* Get sponsored messages for channel.
* This method will return an array of [sponsored message objects](https://docs.madelineproto.xyz/API_docs/constructors/sponsoredMessage.html).
*
* See [the API documentation](https://core.telegram.org/api/sponsored-messages) for more info on how to handle sponsored messages.
*
* @param int|array $peer Channel ID, or Update, or Message, or Peer.
* @return \Generator
*/
public function getSponsoredMessages($peer): \Generator
{
$peer = (yield from $this->getInfo($peer))['bot_api_id'];
$cache = yield $this->sponsoredMessages[$peer];
if ($cache && $cache[0] < \time()) {
return $cache[1];
}
$result = (yield from $this->methodCallAsyncRead('channels.getSponsoredMessages', ['channel' => $peer]))['messages'];
$this->sponsoredMessages->set($peer, [\time() + 5*60, $result]);
return $result;
}
/**
* Mark sponsored message as read.
*
* @param int|array $peer Channel ID, or Update, or Message, or Peer.
* @param string|array{random_id: string} $message Random ID or sponsored message to mark as read.
*
* @return \Generator Bool
*/
public function viewSponsoredMessage($peer, $message): \Generator
{
if (\is_array($message)) {
$message = $message['random_id'];
}
return $this->methodCallAsyncRead('channels.viewSponsoredMessage', ['channel' => $peer, 'random_id' => $message]);
}
}