Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add protect_content global default #917

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions aiogram/bot/base.py
Expand Up @@ -38,6 +38,7 @@ def __init__(
validate_token: Optional[base.Boolean] = True,
parse_mode: typing.Optional[base.String] = None,
disable_web_page_preview: Optional[base.Boolean] = None,
protect_content: Optional[base.Boolean] = None,
timeout: typing.Optional[typing.Union[base.Integer, base.Float, aiohttp.ClientTimeout]] = None,
server: TelegramAPIServer = TELEGRAM_PRODUCTION
):
Expand All @@ -60,6 +61,9 @@ def __init__(
:type parse_mode: :obj:`str`
:param disable_web_page_preview: You can set default disable web page preview parameter
:type disable_web_page_preview: :obj:`bool`
:param protect_content: Protects the contents of sent messages
from forwarding and saving
:type protect_content: :obj:`typing.Optional[base.Boolean]`
:param timeout: Request timeout
:type timeout: :obj:`typing.Optional[typing.Union[base.Integer, base.Float, aiohttp.ClientTimeout]]`
:param server: Telegram Bot API Server endpoint.
Expand Down Expand Up @@ -111,6 +115,7 @@ def __init__(
self.parse_mode = parse_mode

self.disable_web_page_preview = disable_web_page_preview
self.protect_content = protect_content

async def get_new_session(self) -> aiohttp.ClientSession:
return aiohttp.ClientSession(
Expand Down Expand Up @@ -361,5 +366,22 @@ def disable_web_page_preview(self, value):
def disable_web_page_preview(self):
self.disable_web_page_preview = None

@property
def protect_content(self):
return getattr(self, "_protect_content", None)

@protect_content.setter
def protect_content(self, value):
if value is None:
setattr(self, "_protect_content", None)
return
if not isinstance(value, bool):
raise TypeError(f"Protect content must be bool, not {type(value)}")
setattr(self, "_protect_content", value)

@protect_content.deleter
def protect_content(self):
self.protect_content = None

def check_auth_widget(self, data):
return check_integrity(self.__token, data)
38 changes: 38 additions & 0 deletions aiogram/bot/bot.py
Expand Up @@ -330,6 +330,8 @@ async def send_message(self,
payload.setdefault('parse_mode', self.parse_mode)
if self.disable_web_page_preview:
payload.setdefault('disable_web_page_preview', self.disable_web_page_preview)
if self.protect_content is not None:
payload.setdefault('protect_content', self.protect_content)

result = await self.request(api.Methods.SEND_MESSAGE, payload)
return types.Message(**result)
Expand Down Expand Up @@ -370,6 +372,8 @@ async def forward_message(self,
:rtype: :obj:`types.Message`
"""
payload = generate_payload(**locals())
if self.protect_content is not None:
payload.setdefault('protect_content', self.protect_content)

result = await self.request(api.Methods.FORWARD_MESSAGE, payload)
return types.Message(**result)
Expand Down Expand Up @@ -452,6 +456,8 @@ async def copy_message(self,
payload = generate_payload(**locals())
if self.parse_mode and caption_entities is None:
payload.setdefault('parse_mode', self.parse_mode)
if self.protect_content is not None:
payload.setdefault('protect_content', self.protect_content)

result = await self.request(api.Methods.COPY_MESSAGE, payload)
return types.MessageId(**result)
Expand Down Expand Up @@ -520,6 +526,8 @@ async def send_photo(self,
payload = generate_payload(**locals(), exclude=['photo'])
if self.parse_mode and caption_entities is None:
payload.setdefault('parse_mode', self.parse_mode)
if self.protect_content is not None:
payload.setdefault('protect_content', self.protect_content)

files = {}
prepare_file(payload, files, 'photo', photo)
Expand Down Expand Up @@ -610,6 +618,8 @@ async def send_audio(self,
payload = generate_payload(**locals(), exclude=['audio', 'thumb'])
if self.parse_mode and caption_entities is None:
payload.setdefault('parse_mode', self.parse_mode)
if self.protect_content is not None:
payload.setdefault('protect_content', self.protect_content)

files = {}
prepare_file(payload, files, 'audio', audio)
Expand Down Expand Up @@ -700,6 +710,8 @@ async def send_document(self,
payload = generate_payload(**locals(), exclude=['document'])
if self.parse_mode and caption_entities is None:
payload.setdefault('parse_mode', self.parse_mode)
if self.protect_content is not None:
payload.setdefault('protect_content', self.protect_content)

files = {}
prepare_file(payload, files, 'document', document)
Expand Down Expand Up @@ -792,6 +804,8 @@ async def send_video(self, chat_id: typing.Union[base.Integer, base.String],
payload = generate_payload(**locals(), exclude=['video', 'thumb'])
if self.parse_mode and caption_entities is None:
payload.setdefault('parse_mode', self.parse_mode)
if self.protect_content is not None:
payload.setdefault('protect_content', self.protect_content)

files = {}
prepare_file(payload, files, 'video', video)
Expand Down Expand Up @@ -887,6 +901,8 @@ async def send_animation(self,
payload = generate_payload(**locals(), exclude=["animation", "thumb"])
if self.parse_mode and caption_entities is None:
payload.setdefault('parse_mode', self.parse_mode)
if self.protect_content is not None:
payload.setdefault('protect_content', self.protect_content)

files = {}
prepare_file(payload, files, 'animation', animation)
Expand Down Expand Up @@ -967,6 +983,8 @@ async def send_voice(self,
payload = generate_payload(**locals(), exclude=['voice'])
if self.parse_mode and caption_entities is None:
payload.setdefault('parse_mode', self.parse_mode)
if self.protect_content is not None:
payload.setdefault('protect_content', self.protect_content)

files = {}
prepare_file(payload, files, 'voice', voice)
Expand Down Expand Up @@ -1033,6 +1051,8 @@ async def send_video_note(self, chat_id: typing.Union[base.Integer, base.String]
"""
reply_markup = prepare_arg(reply_markup)
payload = generate_payload(**locals(), exclude=['video_note'])
if self.protect_content is not None:
payload.setdefault('protect_content', self.protect_content)

files = {}
prepare_file(payload, files, 'video_note', video_note)
Expand Down Expand Up @@ -1096,6 +1116,8 @@ async def send_media_group(self,

media = prepare_arg(media)
payload = generate_payload(**locals(), exclude=['files'])
if self.protect_content is not None:
payload.setdefault('protect_content', self.protect_content)

result = await self.request(api.Methods.SEND_MEDIA_GROUP, payload, files)
return [types.Message(**message) for message in result]
Expand Down Expand Up @@ -1169,6 +1191,8 @@ async def send_location(self, chat_id: typing.Union[base.Integer, base.String],
"""
reply_markup = prepare_arg(reply_markup)
payload = generate_payload(**locals())
if self.protect_content is not None:
payload.setdefault('protect_content', self.protect_content)

result = await self.request(api.Methods.SEND_LOCATION, payload)
return types.Message(**result)
Expand Down Expand Up @@ -1347,6 +1371,8 @@ async def send_venue(self,
"""
reply_markup = prepare_arg(reply_markup)
payload = generate_payload(**locals())
if self.protect_content is not None:
payload.setdefault('protect_content', self.protect_content)

result = await self.request(api.Methods.SEND_VENUE, payload)
return types.Message(**result)
Expand Down Expand Up @@ -1410,6 +1436,8 @@ async def send_contact(self, chat_id: typing.Union[base.Integer, base.String],
payload = generate_payload(**locals())

result = await self.request(api.Methods.SEND_CONTACT, payload)
if self.protect_content is not None:
payload.setdefault('protect_content', self.protect_content)
return types.Message(**result)

async def send_poll(self,
Expand Down Expand Up @@ -1528,6 +1556,8 @@ async def send_poll(self,
payload = generate_payload(**locals())
if self.parse_mode and explanation_entities is None:
payload.setdefault('explanation_parse_mode', self.parse_mode)
if self.protect_content is not None:
payload.setdefault('protect_content', self.protect_content)

result = await self.request(api.Methods.SEND_POLL, payload)
return types.Message(**result)
Expand Down Expand Up @@ -1587,6 +1617,8 @@ async def send_dice(self,

reply_markup = prepare_arg(reply_markup)
payload = generate_payload(**locals())
if self.protect_content is not None:
payload.setdefault('protect_content', self.protect_content)

result = await self.request(api.Methods.SEND_DICE, payload)
return types.Message(**result)
Expand Down Expand Up @@ -2961,6 +2993,8 @@ async def send_sticker(self, chat_id: typing.Union[base.Integer, base.String],
"""
reply_markup = prepare_arg(reply_markup)
payload = generate_payload(**locals(), exclude=['sticker'])
if self.protect_content is not None:
payload.setdefault('protect_content', self.protect_content)

files = {}
prepare_file(payload, files, 'sticker', sticker)
Expand Down Expand Up @@ -3393,6 +3427,8 @@ async def send_invoice(self,
reply_markup = prepare_arg(reply_markup)
provider_data = prepare_arg(provider_data)
payload_ = generate_payload(**locals())
if self.protect_content is not None:
payload.setdefault('protect_content', self.protect_content)

result = await self.request(api.Methods.SEND_INVOICE, payload_)
return types.Message(**result)
Expand Down Expand Up @@ -3535,6 +3571,8 @@ async def send_game(self,
"""
reply_markup = prepare_arg(reply_markup)
payload = generate_payload(**locals())
if self.protect_content is not None:
payload.setdefault('protect_content', self.protect_content)

result = await self.request(api.Methods.SEND_GAME, payload)
return types.Message(**result)
Expand Down
52 changes: 52 additions & 0 deletions aiogram/dispatcher/webhook.py
Expand Up @@ -458,6 +458,18 @@ def protect_content(self):
setattr(self, "protect_content", True)
return self

@staticmethod
def _global_protect_content():
"""
Detect global protect content value

:return:
"""
from aiogram import Bot
bot = Bot.get_current()
if bot is not None:
return bot.protect_content


class ParseModeMixin:
def as_html(self):
Expand Down Expand Up @@ -536,6 +548,8 @@ def __init__(self, chat_id: Union[Integer, String] = None,
parse_mode = self._global_parse_mode()
if disable_web_page_preview is None:
disable_web_page_preview = self._global_disable_web_page_preview()
if protect_content is None:
protect_content = self._global_protect_content()

self.chat_id = chat_id
self.text = text
Expand Down Expand Up @@ -607,6 +621,9 @@ def __init__(self, chat_id: Union[Integer, String] = None,
from forwarding and saving
:param message_id: Integer - Message identifier in the chat specified in from_chat_id
"""
if protect_content is None:
protect_content = self._global_protect_content()

self.chat_id = chat_id
self.from_chat_id = from_chat_id
self.message_id = message_id
Expand Down Expand Up @@ -669,6 +686,9 @@ def __init__(self, chat_id: Union[Integer, String],
- Additional interface options. A JSON-serialized object for an inline keyboard,
custom reply keyboard, instructions to remove reply keyboard or to force a reply from the user.
"""
if protect_content is None:
protect_content = self._global_protect_content()

self.chat_id = chat_id
self.photo = photo
self.caption = caption
Expand Down Expand Up @@ -731,6 +751,9 @@ def __init__(self, chat_id: Union[Integer, String],
- Additional interface options. A JSON-serialized object for an inline keyboard,
custom reply keyboard, instructions to remove reply keyboard or to force a reply from the user.
"""
if protect_content is None:
protect_content = self._global_protect_content()

self.chat_id = chat_id
self.audio = audio
self.caption = caption
Expand Down Expand Up @@ -793,6 +816,9 @@ def __init__(self, chat_id: Union[Integer, String],
- Additional interface options. A JSON-serialized object for an inline keyboard,
custom reply keyboard, instructions to remove reply keyboard or to force a reply from the user.
"""
if protect_content is None:
protect_content = self._global_protect_content()

self.chat_id = chat_id
self.document = document
self.caption = caption
Expand Down Expand Up @@ -856,6 +882,9 @@ def __init__(self, chat_id: Union[Integer, String],
- Additional interface options. A JSON-serialized object for an inline keyboard,
custom reply keyboard, instructions to remove reply keyboard or to force a reply from the user.
"""
if protect_content is None:
protect_content = self._global_protect_content()

self.chat_id = chat_id
self.video = video
self.duration = duration
Expand Down Expand Up @@ -919,6 +948,9 @@ def __init__(self, chat_id: Union[Integer, String],
- Additional interface options. A JSON-serialized object for an inline keyboard, custom reply keyboard,
instructions to remove reply keyboard or to force a reply from the user.
"""
if protect_content is None:
protect_content = self._global_protect_content()

self.chat_id = chat_id
self.voice = voice
self.caption = caption
Expand Down Expand Up @@ -977,6 +1009,9 @@ def __init__(self, chat_id: Union[Integer, String],
- Additional interface options. A JSON-serialized object for an inline keyboard,
custom reply keyboard, instructions to remove reply keyboard or to force a reply from the user.
"""
if protect_content is None:
protect_content = self._global_protect_content()

self.chat_id = chat_id
self.video_note = video_note
self.duration = duration
Expand Down Expand Up @@ -1037,6 +1072,8 @@ def __init__(self, chat_id: Union[Integer, String],
elif isinstance(media, list):
# Convert list to MediaGroup
media = types.MediaGroup(media)
if protect_content is None:
protect_content = self._global_protect_content()

self.chat_id = chat_id
self.media = media
Expand Down Expand Up @@ -1117,6 +1154,9 @@ def __init__(self, chat_id: Union[Integer, String],
- Additional interface options. A JSON-serialized object for an inline keyboard,
custom reply keyboard, instructions to remove reply keyboard or to force a reply from the user.
"""
if protect_content is None:
protect_content = self._global_protect_content()

self.chat_id = chat_id
self.latitude = latitude
self.longitude = longitude
Expand Down Expand Up @@ -1177,6 +1217,9 @@ def __init__(self, chat_id: Union[Integer, String],
- Additional interface options. A JSON-serialized object for an inline keyboard,
custom reply keyboard, instructions to remove reply keyboard or to force a reply from the user.
"""
if protect_content is None:
protect_content = self._global_protect_content()

self.chat_id = chat_id
self.latitude = latitude
self.longitude = longitude
Expand Down Expand Up @@ -1237,6 +1280,9 @@ def __init__(self, chat_id: Union[Integer, String],
- Additional interface options. A JSON-serialized object for an inline keyboard,
custom reply keyboard, instructions to remove keyboard or to force a reply from the user.
"""
if protect_content is None:
protect_content = self._global_protect_content()

self.chat_id = chat_id
self.phone_number = phone_number
self.first_name = first_name
Expand Down Expand Up @@ -1845,6 +1891,9 @@ def __init__(self, chat_id: Union[Integer, String],
Additional interface options. A JSON-serialized object for an inline keyboard,
custom reply keyboard, instructions to remove reply keyboard or to force a reply from the user.
"""
if protect_content is None:
protect_content = self._global_protect_content()

self.chat_id = chat_id
self.sticker = sticker
self.disable_notification = disable_notification
Expand Down Expand Up @@ -2288,6 +2337,9 @@ def __init__(self, chat_id: Integer,
:param reply_markup: types.InlineKeyboardMarkup (Optional) - A JSON-serialized object for an inline keyboard.
If empty, one ‘Play game_title’ button will be shown. If not empty, the first button must launch the game.
"""
if protect_content is None:
protect_content = self._global_protect_content()

self.chat_id = chat_id
self.game_short_name = game_short_name
self.disable_notification = disable_notification
Expand Down