Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions compiler/docs/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ def get_title_list(s: str) -> list:
send_dice
send_document
send_location
send_paid_media
send_media_group
send_message
send_photo
Expand Down Expand Up @@ -515,6 +516,17 @@ def get_title_list(s: str) -> list:
InputPhoneContact
LinkPreviewOptions
""",
input_paid_media="""
Input Paid Media
InputPaidMedia
InputPaidMediaPhoto
InputPaidMediaVideo
PaidMediaInfo
PaidMedia
PaidMediaPreview
PaidMediaPhoto
PaidMediaVideo
""",
input_message_content="""
InputMessageContent
ExternalReplyInfo
Expand Down
13 changes: 13 additions & 0 deletions compiler/docs/template/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ Input Media

{input_media}

Input Paid Media
-----------

.. autosummary::
:nosignatures:

{input_paid_media}

.. toctree::
:hidden:

{input_paid_media}

Inline Mode
-----------

Expand Down
3 changes: 3 additions & 0 deletions docs/source/releases/changes-in-this-fork.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ If you found any issue or have any suggestions, feel free to make `an issue <htt
| Scheme layer used: 183 |
+------------------------+

- Added the classes :obj:`~pyrogram.types.PaidMedia`, :obj:`~pyrogram.types.PaidMediaInfo`, :obj:`~pyrogram.types.PaidMediaPreview`, :obj:`~pyrogram.types.PaidMediaPhoto` and :obj:`~pyrogram.types.PaidMediaVideo`, containing information about paid media.
- Added the method :meth:`~pyrogram.Client.send_paid_media` and the classes :obj:`~pyrogram.types.InputPaidMedia`, :obj:`~pyrogram.types.InputPaidMediaPhoto` and :obj:`~pyrogram.types.InputPaidMediaVideo`, to support sending paid media.
- Added the field ``paid_media`` to the classes :obj:`~pyrogram.types.Message` and :obj:`~pyrogram.types.ExternalReplyInfo`.
- Added :meth:`~pyrogram.Client.get_stories`.
- Added :obj:`~pyrogram.filters.thread` and :obj:`~pyrogram.filters.self_destruct`.
- Added the field ``can_send_paid_media`` to the class :obj:`~pyrogram.types.Chat`.
Expand Down
3 changes: 3 additions & 0 deletions pyrogram/enums/message_media_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,6 @@ class MessageMediaType(AutoName):

INVOICE = auto()
"Invoice"

PAID_MEDIA = auto()
"Paid Media"
2 changes: 2 additions & 0 deletions pyrogram/methods/messages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
from .send_location import SendLocation
from .send_media_group import SendMediaGroup
from .send_message import SendMessage
from .send_paid_media import SendPaidMedia
from .send_photo import SendPhoto
from .send_poll import SendPoll
from .set_reaction import SetReaction
Expand Down Expand Up @@ -112,6 +113,7 @@ class Messages(
SendLocation,
SendMediaGroup,
SendMessage,
SendPaidMedia,
SendPhoto,
SendPoll,
SendSticker,
Expand Down
2 changes: 2 additions & 0 deletions pyrogram/methods/messages/copy_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ async def copy_message(
) -> "types.Message":
"""Copy messages of any kind.

Service messages, paid media messages, giveaway messages, giveaway winners messages, and invoice messages can't be copied. A quiz poll can be copied only if the value of the field ``correct_option_id`` is known to the bot.

The method is analogous to the method :meth:`~Client.forward_messages`, but the copied message doesn't have a
link to the original message.

Expand Down
274 changes: 274 additions & 0 deletions pyrogram/methods/messages/send_paid_media.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
# 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/>.

import os
import re

from datetime import datetime
from typing import Union, List, Optional

import pyrogram
from pyrogram import enums, raw, types, utils
from pyrogram.file_id import FileType


class SendPaidMedia:
async def send_paid_media(
self: "pyrogram.Client",
chat_id: Union[int, str],
star_count: int,
media: List[Union[
"types.InputPaidMediaPhoto",
"types.InputPaidMediaVideo"
]],
caption: str = "",
parse_mode: Optional["enums.ParseMode"] = None,
caption_entities: List["types.MessageEntity"] = None,
show_caption_above_media: bool = None,
disable_notification: bool = None,
protect_content: bool = None,
reply_parameters: "types.ReplyParameters" = None,
reply_markup: Union[
"types.InlineKeyboardMarkup",
"types.ReplyKeyboardMarkup",
"types.ReplyKeyboardRemove",
"types.ForceReply"
] = None,
schedule_date: datetime = None
) -> "types.Message":
"""Use this method to send paid media to channel chats.

.. include:: /_includes/usable-by/users-bots.rst

Parameters:
chat_id (``int`` | ``str``):
Unique identifier for the target chat or username of the target channel (in the format @channelusername).

star_count (``int``):
The number of Telegram Stars that must be paid to buy access to the media.

media (List of :obj:`~pyrogram.types.InputPaidMedia`):
A list describing the media to be sent; up to 10 items.

caption (``str``, *optional*):
Media caption, 0-1024 characters after entities parsing.

parse_mode (:obj:`~pyrogram.enums.ParseMode`, *optional*):
By default, texts are parsed using both Markdown and HTML styles.
You can combine both syntaxes together.

caption_entities (List of :obj:`~pyrogram.types.MessageEntity`):
List of special entities that appear in the caption, which can be specified instead of *parse_mode*.

show_caption_above_media (``bool``, *optional*):
Pass True, if the caption must be shown above the message media.

disable_notification (``bool``, *optional*):
Sends the message silently. Users will receive a notification with no sound.

protect_content (``bool``, *optional*):
Protects the contents of the sent message from forwarding and saving.

reply_parameters (:obj:`~pyrogram.types.ReplyParameters`, *optional*):
Description of the message to reply to

reply_markup (:obj:`~pyrogram.types.InlineKeyboardMarkup` | :obj:`~pyrogram.types.ReplyKeyboardMarkup` | :obj:`~pyrogram.types.ReplyKeyboardRemove` | :obj:`~pyrogram.types.ForceReply`, *optional*):
Additional interface options. An object for an inline keyboard, custom reply keyboard,
instructions to remove reply keyboard or to force a reply from the user.

schedule_date (:obj:`~datetime.datetime`, *optional*):
Date when the message will be automatically sent. Pass a :obj:`~datetime.datetime` object.

Returns:
:obj:`~pyrogram.types.Message`: On success, the sent message is returned.

"""
multi_media = []

peer = await self.resolve_peer(chat_id)
for i in media:
if isinstance(i, types.InputPaidMediaPhoto):
if isinstance(i.media, str):
if os.path.isfile(i.media):
media = await self.invoke(
raw.functions.messages.UploadMedia(
peer=await self.resolve_peer(chat_id),
media=raw.types.InputMediaUploadedPhoto(
file=await self.save_file(i.media)
)
)
)

media = raw.types.InputMediaPhoto(
id=raw.types.InputPhoto(
id=media.photo.id,
access_hash=media.photo.access_hash,
file_reference=media.photo.file_reference
)
)
elif re.match("^https?://", i.media):
media = await self.invoke(
raw.functions.messages.UploadMedia(
peer=await self.resolve_peer(chat_id),
media=raw.types.InputMediaPhotoExternal(
url=i.media
)
)
)

media = raw.types.InputMediaPhoto(
id=raw.types.InputPhoto(
id=media.photo.id,
access_hash=media.photo.access_hash,
file_reference=media.photo.file_reference
)
)
else:
media = utils.get_input_media_from_file_id(i.media, FileType.PHOTO)
else:
media = await self.invoke(
raw.functions.messages.UploadMedia(
peer=await self.resolve_peer(chat_id),
media=raw.types.InputMediaUploadedPhoto(
file=await self.save_file(i.media)
)
)
)

media = raw.types.InputMediaPhoto(
id=raw.types.InputPhoto(
id=media.photo.id,
access_hash=media.photo.access_hash,
file_reference=media.photo.file_reference
)
)
elif (
isinstance(i, types.InputPaidMediaVideo)
):
if isinstance(i.media, str):
if os.path.isfile(i.media):
attributes = [
raw.types.DocumentAttributeVideo(
supports_streaming=i.supports_streaming or None,
duration=i.duration,
w=i.width,
h=i.height
),
raw.types.DocumentAttributeFilename(file_name=os.path.basename(i.media))
]
media = await self.invoke(
raw.functions.messages.UploadMedia(
peer=await self.resolve_peer(chat_id),
media=raw.types.InputMediaUploadedDocument(
file=await self.save_file(i.media),
thumb=await self.save_file(i.thumbnail),
mime_type=self.guess_mime_type(i.media) or "video/mp4",
nosound_video=True,
attributes=attributes
)
)
)

media = raw.types.InputMediaDocument(
id=raw.types.InputDocument(
id=media.document.id,
access_hash=media.document.access_hash,
file_reference=media.document.file_reference
)
)
elif re.match("^https?://", i.media):
media = await self.invoke(
raw.functions.messages.UploadMedia(
peer=await self.resolve_peer(chat_id),
media=raw.types.InputMediaDocumentExternal(
url=i.media
)
)
)

media = raw.types.InputMediaDocument(
id=raw.types.InputDocument(
id=media.document.id,
access_hash=media.document.access_hash,
file_reference=media.document.file_reference
)
)
else:
media = utils.get_input_media_from_file_id(i.media, FileType.VIDEO)
else:
media = await self.invoke(
raw.functions.messages.UploadMedia(
peer=await self.resolve_peer(chat_id),
media=raw.types.InputMediaUploadedDocument(
file=await self.save_file(i.media),
thumb=await self.save_file(i.thumbnail),
mime_type=self.guess_mime_type(getattr(i.media, "name", "video.mp4")) or "video/mp4",
attributes=[
raw.types.DocumentAttributeVideo(
supports_streaming=i.supports_streaming or None,
duration=i.duration,
w=i.width,
h=i.height
),
raw.types.DocumentAttributeFilename(file_name=getattr(i.media, "name", "video.mp4"))
]
)
)
)

media = raw.types.InputMediaDocument(
id=raw.types.InputDocument(
id=media.document.id,
access_hash=media.document.access_hash,
file_reference=media.document.file_reference
)
)
else:
raise ValueError(f"{i.__class__.__name__} is not a supported type for send_paid_media")
multi_media.append(media)

rpc = raw.functions.messages.SendMedia(
peer=await self.resolve_peer(chat_id),
media=raw.types.InputMediaPaidMedia(
stars_amount=star_count,
extended_media=multi_media
),
silent=disable_notification or None,
random_id=self.rnd_id(),
schedule_date=utils.datetime_to_timestamp(schedule_date),
noforwards=protect_content,
invert_media=show_caption_above_media,
**await utils.parse_text_entities(self, caption, parse_mode, caption_entities)
)
r = await self.invoke(rpc, sleep_threshold=60)
for i in r.updates:
if isinstance(
i,
(
raw.types.UpdateNewMessage,
raw.types.UpdateNewChannelMessage,
raw.types.UpdateNewScheduledMessage
)
):
return await types.Message._parse(
self, i.message,
{i.id: i for i in r.users},
{i.id: i for i in r.chats},
is_scheduled=isinstance(i, raw.types.UpdateNewScheduledMessage),
replies=self.fetch_replies
)
1 change: 1 addition & 0 deletions pyrogram/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from .chat_topics import *
from .inline_mode import *
from .input_media import *
from .input_paid_media import *
from .input_message_content import *
from .messages_and_media import *
from .message_origin import *
Expand Down
Loading