New high-lvl methods for stories

This commit is contained in:
KurimuzonAkuma 2023-10-18 15:08:10 +03:00
parent 672bae27bf
commit e27e782e50
3 changed files with 149 additions and 0 deletions

View File

@ -17,11 +17,13 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from .apply_boost import ApplyBoost
from .can_send_story import CanSendStory
from .delete_stories import DeleteStories
from .edit_story import EditStory
from .export_story_link import ExportStoryLink
from .get_all_stories import GetAllStories
from .get_peer_stories import GetPeerStories
from .get_pinned_stories import GetPinnedStories
from .get_stories import GetStories
from .get_stories_archive import GetStoriesArchive
from .hide_stories import HideStories
@ -32,11 +34,13 @@ from .send_story import SendStory
class Stories(
ApplyBoost,
CanSendStory,
DeleteStories,
EditStory,
ExportStoryLink,
GetAllStories,
GetPeerStories,
GetPinnedStories,
GetStories,
GetStoriesArchive,
HideStories,

View File

@ -0,0 +1,54 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import Union, Iterable
import pyrogram
from pyrogram import raw
from pyrogram import types
class CanSendStory:
async def can_send_story(
self: "pyrogram.Client",
chat_id: Union[int, str],
) -> bool:
"""Can send story
.. include:: /_includes/usable-by/users-bots.rst
Parameters:
chat_id (``int`` | ``str``):
Unique identifier (int) or username (str) of the target chat.
Returns:
``str``: On success, a bool is returned.
Example:
.. code-block:: python
# Check if you can send story to chat id
app.can_send_story(chat_id)
"""
r = await self.invoke(
raw.functions.stories.CanSendStory(
peer=await self.resolve_peer(chat_id),
)
)
return r

View File

@ -0,0 +1,91 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import AsyncGenerator, Union, Optional
import pyrogram
from pyrogram import raw
from pyrogram import types
class GetPinnedStories:
async def get_pinned_stories(
self: "pyrogram.Client",
chat_id: Union[int, str],
offset_id: int = 0,
limit: int = 0,
) -> Optional[AsyncGenerator["types.Story", None]]:
"""Get pinned stories stories.
.. include:: /_includes/usable-by/users.rst
Parameters:
chat_id (``int`` | ``str``):
Unique identifier (int) or username (str) of the target chat.
offset_id (``int``, *optional*):
Offset event identifier from which to start returning results.
By default, no offset is applied and events will be returned starting from the latest.
limit (``int``, *optional*):
Maximum amount of events to be returned.
By default, all events will be returned.
Yields:
:obj:`~pyrogram.types.Story` objects.
Example:
.. code-block:: python
# Get all pinned story
async for story in app.get_pinned_stories():
print(story)
"""
current = 0
total = abs(limit) or (1 << 31)
limit = min(100, total)
while True:
peer = await self.resolve_peer(chat_id)
r = await self.invoke(
raw.functions.stories.GetPinnedStories(
peer=peer,
offset_id=offset_id,
limit=limit
)
)
if not r.stories:
return
last = r.stories[-1]
offset_id = last.id
for story in r.stories:
yield await types.Story._parse(
self,
story,
{i.id: i for i in r.users},
{i.id: i for i in r.chats},
peer
)
current += 1
if current >= total:
return