Skip to content

Commit

Permalink
add protect content global default (#917)
Browse files Browse the repository at this point in the history
  • Loading branch information
bomzheg committed Aug 13, 2022
1 parent 65475ca commit a852b95
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
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 @@ -335,6 +335,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 @@ -375,6 +377,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 @@ -457,6 +461,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 @@ -525,6 +531,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 @@ -615,6 +623,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 @@ -705,6 +715,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 @@ -797,6 +809,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 @@ -892,6 +906,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 @@ -972,6 +988,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 @@ -1038,6 +1056,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 @@ -1101,6 +1121,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 @@ -1174,6 +1196,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 @@ -1352,6 +1376,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 @@ -1415,6 +1441,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 @@ -1533,6 +1561,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 @@ -1592,6 +1622,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 @@ -2966,6 +2998,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 @@ -3426,6 +3460,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 @@ -3631,6 +3667,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

0 comments on commit a852b95

Please sign in to comment.