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

API 7.0 #4034

Merged
merged 151 commits into from Feb 8, 2024
Merged

API 7.0 #4034

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
151 commits
Select commit Hold shift + click to select a range
3b7e750
Feat: Introduce my take on MaybeInaccessibleMessage
Poolitzer Dec 28, 2023
fb09a3f
update readme and add new multiple message action methods
clot27 Dec 29, 2023
fdebe95
Update version number in readme
Bibo-Joshi Dec 29, 2023
d89dcad
Update `constants.BOT_API_VERSION(_INFO)`
Bibo-Joshi Dec 29, 2023
58357d4
add shortcuts for new multiple action methods
clot27 Dec 29, 2023
7364d74
Add classes `Giveaway{, Created, Winners, Completed}`
Bibo-Joshi Dec 29, 2023
cda7e47
Add `Message.giveaway{, winners, completed, created}`
Bibo-Joshi Dec 29, 2023
56543e5
Add filters for giveaways
Bibo-Joshi Dec 29, 2023
caf9052
Fix `telegram.constants.__all__`
Bibo-Joshi Dec 29, 2023
9dc37ca
Fix Docs build
Bibo-Joshi Dec 29, 2023
76510e4
Merge branch 'master' into api-7.0
Bibo-Joshi Dec 29, 2023
748918e
KeyboardButtonRequestUser → Users
Bibo-Joshi Dec 29, 2023
37f2820
`KeyboardButton.request_user` → `KeyboardButton.request_users`
Bibo-Joshi Dec 29, 2023
c6fe89c
`telegram.constants.KeyboardButtonRequestUsersLimit`, docs & a bit re…
Bibo-Joshi Dec 29, 2023
044b2e2
Slots for `TestKeyboardButtonRequestUser`
Bibo-Joshi Dec 29, 2023
96ef98b
Freeze `KeyboardButtonRequestUser`
Bibo-Joshi Dec 29, 2023
3424107
Add the `LinkPreviewOptions` class
harshil21 Dec 30, 2023
e6b61e9
Add `LinkPreviewOptions` to `Message`
harshil21 Dec 30, 2023
7fdea9f
Add `blockquote` message entity.
aelkheir Dec 30, 2023
cf7fc22
add tests for new methods and shortcuts
clot27 Dec 30, 2023
d4c1512
Add `UsersShared` and deprecate `UserShared`
Bibo-Joshi Dec 30, 2023
5ba99ef
Add missing doc fixe
Bibo-Joshi Dec 30, 2023
04a575f
* Add `Message.users_shared`
Bibo-Joshi Dec 30, 2023
35dbea5
Doc fixes
Bibo-Joshi Dec 30, 2023
497baa8
Add LPO to `Defaults`, bot methods, & adjust defaults handling
harshil21 Dec 31, 2023
f41e91c
Use unified transition warnings for `UserShared`, `KeyboardButtonRequ…
Bibo-Joshi Dec 31, 2023
7544564
Update test for `constants.MessageType` completeness for LPO
Bibo-Joshi Dec 31, 2023
2d42c5b
`filters.StatusUpdate.USERS_SHARED`
Bibo-Joshi Dec 31, 2023
bf635f7
Fix Type Completeness
Bibo-Joshi Dec 31, 2023
6ee210e
Add `constants.(Profile)?AccentColor`
Bibo-Joshi Dec 31, 2023
6f38f16
Add `Chat.{(profile_)?accent_color_id, (profile_)?background_custom_e…
Bibo-Joshi Dec 31, 2023
12c19aa
Merge remote-tracking branch 'upstream/api-7.0' into blockquote-api-7.0
aelkheir Dec 31, 2023
1d5c3b1
Update `Message._parse(html/markdown)` to account for 'blockquote'
aelkheir Dec 31, 2023
0af70b3
Adapt tests to include `blockquote` message entity
aelkheir Dec 31, 2023
f8514d7
Add LPO to InputTextMessageContent
harshil21 Jan 1, 2024
01db8a2
Added the ChatBoost classes
harshil21 Jan 1, 2024
b644d5c
Add ExternalReplyInfo, TextQuote and ReplyParameters classes
clot27 Jan 1, 2024
4018c4f
Add TextQuote, ExternalReplyInfo to telegram.Message and tests. Also …
clot27 Jan 1, 2024
4037697
Fix a bug regarding UserShared in `Message`
Bibo-Joshi Jan 1, 2024
d0f1ac6
Update docstring for `Chat.emoji_status_{custom_emoji_id, expiration_…
Bibo-Joshi Jan 1, 2024
e9e788c
Feat: ReactionType
Poolitzer Jan 2, 2024
c98e16e
Merge branch 'temp-api-7.0' into api-7.0
Poolitzer Jan 2, 2024
44a3b9f
Add `Chat.has_visible_history` & fix some pre-commit & merge problems
Bibo-Joshi Jan 2, 2024
6d7156b
add classes
clot27 Jan 3, 2024
479c2b5
Merge branch 'master' into api-7.0
Bibo-Joshi Jan 3, 2024
ba9cfcf
Add support for parsing multi-line blockquotes
aelkheir Jan 5, 2024
877d6d6
Merge remote-tracking branch 'upstream/api-7.0' into blockquote-api-7.0
aelkheir Jan 5, 2024
be206e6
Add chat boost to `Update`, docs, constants, & write tests
harshil21 Jan 6, 2024
0dd0529
Try fixing blockquote things and clean up `Message._parse_*` in the p…
Bibo-Joshi Jan 6, 2024
6cb2c38
review 1 and add new params in Message tests
clot27 Jan 7, 2024
3e8a95b
Apply suggestions from code review
Poolitzer Jan 7, 2024
ce80402
Missed this
Poolitzer Jan 7, 2024
9f2f8fa
Fix: Apply suggestions for MaybeInaccessible
Poolitzer Jan 7, 2024
cd77c9e
Add `Bot.get_user_chat_boosts` with shortcuts and tests.
harshil21 Jan 8, 2024
23b948a
Merge remote-tracking branch 'origin/api-7.0' into api-7.0
Poolitzer Jan 8, 2024
82ae8b3
Feat: Rest of status
Poolitzer Jan 8, 2024
f682d17
`blockquote` message entity api-7.0. (#4038)
aelkheir Jan 8, 2024
d255936
Merge master
Bibo-Joshi Jan 8, 2024
8aecdc5
Add new param, constants and review
clot27 Jan 9, 2024
1605a23
Do a bit of review addressing
Bibo-Joshi Jan 10, 2024
1c5ba85
add reply_parameters to extbot, change type of RP.aswr
clot27 Jan 12, 2024
a8a5f53
Try to deprecate rtm, aswr and forward_* params
clot27 Jan 13, 2024
9592a36
Reuse `tg._utils` warnings in `InputTextMessageContent`
Bibo-Joshi Jan 13, 2024
01a713d
Do fix
Bibo-Joshi Jan 13, 2024
dcb7e60
Make `MIM.is_accessible` a property, add `MIM.__bool__`, add tests fo…
Bibo-Joshi Jan 13, 2024
d20f748
Get pre-commit on track. Includes rewrite of `MRH.check_update`.
Bibo-Joshi Jan 14, 2024
9073949
Extract helper functions for parsing user_name and chat_id
Bibo-Joshi Jan 14, 2024
78f6619
Update docs of `Update.effective_user`
Bibo-Joshi Jan 14, 2024
b29eddc
`User.get_user_chat_boots` -> `User.get_chat_boots`
Bibo-Joshi Jan 14, 2024
482d431
Review comments on `_reaction.py`
Bibo-Joshi Jan 14, 2024
8fd53ef
Convert `MessageOrigin.type` to `constants.MessageOriginType`
Bibo-Joshi Jan 14, 2024
0235ae7
Address review comments on `MessageReaction*`
Bibo-Joshi Jan 14, 2024
0ea6cfa
`Message.set_message_reaction` -> `Message.set_reaction`
Bibo-Joshi Jan 14, 2024
b0c7f3b
Get Documentation build to (hopefully) pass and Rework `*Message.__bo…
Bibo-Joshi Jan 14, 2024
e8395a7
Resolve a todo
Bibo-Joshi Jan 14, 2024
a65c516
DeepSource + TypeCompleteness
Bibo-Joshi Jan 14, 2024
7d6c03c
Get started on updating `test_official`. Accordingly, fix some bugs a…
Bibo-Joshi Jan 14, 2024
d2629b9
Add warning about future behavior change to `Update.effective_message`
Bibo-Joshi Jan 14, 2024
37d86ea
Hopefull finish test_official except for `Defaults` stuff
Bibo-Joshi Jan 14, 2024
cca9b73
More DeepSource and Pre-Commit
Bibo-Joshi Jan 14, 2024
16f9633
Merge branch 'master' into api-7.0
Bibo-Joshi Jan 14, 2024
ad55105
Review commonts on `Bot.set_message_reaction`
Bibo-Joshi Jan 14, 2024
b667123
Two docstring updates
Bibo-Joshi Jan 14, 2024
5a4d27e
More docstring
Bibo-Joshi Jan 14, 2024
e63d5e9
Small fixes for `Chat.available_reactions`
Bibo-Joshi Jan 14, 2024
e424ee2
Introduce helper function for handling `dwpp` and `lpo`
Bibo-Joshi Jan 14, 2024
076539d
Update `filters` to use `Message.forward_origin`
Bibo-Joshi Jan 14, 2024
2e726a0
Fix an import error
Bibo-Joshi Jan 14, 2024
631be41
add send_media_group test, remove rtm aswr return check test for now
clot27 Jan 15, 2024
adf9867
Add ChatBoostHandler to docs and tests
harshil21 Jan 15, 2024
63d172e
address one review comment + add _log decorators
harshil21 Jan 15, 2024
66ac971
adjust the mutually exclusive dwpp and lpo test
harshil21 Jan 15, 2024
7765dc7
Review: add depr note to Defaults.DWPP
harshil21 Jan 15, 2024
873f13a
Add tests for ExternalReplyInfo, TextQuote and ReplyParameters
clot27 Jan 15, 2024
40834d2
Add a comment for future us
Bibo-Joshi Jan 14, 2024
e0fb47c
Merge master
Bibo-Joshi Jan 15, 2024
8a6046c
Change warning behavior for `Update.effective_message`
Bibo-Joshi Jan 15, 2024
f6f2622
Get started on tests
Bibo-Joshi Jan 15, 2024
a82e69f
Fix two more tests
Bibo-Joshi Jan 15, 2024
3f20a3b
Fix docs build
Bibo-Joshi Jan 15, 2024
13ff8ba
Merge branch 'master' into api-7.0
Bibo-Joshi Jan 17, 2024
821e9dc
Work a bit on failing tests - Mostly add new parameters to shortcut m…
Bibo-Joshi Jan 17, 2024
ec40c79
Add MessageOrigin tests
clot27 Jan 18, 2024
f47fcb4
Drive work on `Defaults`
Bibo-Joshi Jan 19, 2024
6d493ea
Get failing tests down to ~30. testing defaults handling is still WIP
Bibo-Joshi Jan 19, 2024
f3a4775
Get defaults checks to work and overhaul them greatly in the process …
Bibo-Joshi Jan 22, 2024
8e21be3
test_official now passes.
harshil21 Jan 24, 2024
3c71bcb
add a comment
harshil21 Jan 24, 2024
83608b0
Merge Master
Bibo-Joshi Jan 24, 2024
2aa1ae3
Add defaults handling for `RP.quote_parse_mode`
Bibo-Joshi Jan 24, 2024
749e4d1
Add explicit test cases for `quote_parse_mode`
Bibo-Joshi Jan 24, 2024
6f7aeb7
Elaborate docs on `Defautls.lpo`
Bibo-Joshi Jan 26, 2024
1c06639
Add explicit test for `edit_message_text.lpo`
Bibo-Joshi Jan 26, 2024
17653be
Fix 3 more tests - now only Message.reply_* fails :)
Bibo-Joshi Jan 26, 2024
1def5ef
DeepSource
Bibo-Joshi Jan 26, 2024
4abbc51
Temporarliy skip `check_defaults_handling` to make code coverage acce…
Bibo-Joshi Jan 26, 2024
3cdbcf9
Increase coverage in `TestUpdate`
Bibo-Joshi Jan 26, 2024
72d02e2
Increase coverage in `TestCallbackQuery`
Bibo-Joshi Jan 26, 2024
d74c90f
Fix tests for Update & fix ReactionCount.de_json in the progress
Bibo-Joshi Jan 26, 2024
d270ae8
Add internal helper method for `CallbackQuery`
Bibo-Joshi Jan 27, 2024
1fcfc74
Merge branch 'api-7.0' into blockquote-api-7.0-branched
Bibo-Joshi Jan 27, 2024
5a1e5c7
Update docstrings in `Message`
Bibo-Joshi Jan 27, 2024
f75fc09
Update tests
Bibo-Joshi Jan 27, 2024
f9e1548
Make unit tests run
Bibo-Joshi Jan 27, 2024
090b09b
Add reply_parameters for Message.reply_* methods
clot27 Jan 28, 2024
a1d4947
Fix docstring: cross references and substitutions
clot27 Jan 28, 2024
87043fd
Fix circular imports and add tests for `ReactionCount`
Bibo-Joshi Jan 28, 2024
7551915
Feat: Add rest of emojis
Poolitzer Jan 28, 2024
6ce89de
Get `Message.reply_*` shortcuts tests to work. Marked test for defaul…
Bibo-Joshi Jan 28, 2024
57a6f2b
Merge branch 'api-7.0' into blockquote-api-7.0-branched
Poolitzer Jan 29, 2024
eb5c138
Tests for `MessageReactionHandler`
Bibo-Joshi Jan 29, 2024
bbb714d
Increase coverage a bit
Bibo-Joshi Jan 29, 2024
7e0c922
Add defaults and fix docstrings
clot27 Feb 1, 2024
2384df8
Re-enable & improve `test_default_(do_)quote`
Bibo-Joshi Feb 2, 2024
e8a62dc
Mix message tests
Bibo-Joshi Feb 3, 2024
7fc535a
Work on `Message.{build_reply_arguments, compute_quote_position_and_e…
Bibo-Joshi Feb 3, 2024
459bbfe
Fix tests again
Bibo-Joshi Feb 3, 2024
fc6f516
Add first batch of tests for `compute_quote_…`
Bibo-Joshi Feb 3, 2024
7c7da9d
Work on `Message.{compute_quote…, build_reply_arguemnts}`: Tests & do…
Bibo-Joshi Feb 4, 2024
6950e48
Merge branch 'blockquote-api-7.0-branched' into api-7.0
Bibo-Joshi Feb 4, 2024
43c393f
Merge branch 'master' into api-7.0
Bibo-Joshi Feb 4, 2024
fdc4799
Finish tests on `Message` by adding tests on quote handling
Bibo-Joshi Feb 4, 2024
0593f45
Make `reply_to_message_id` and `reply_parameters` mut. exl. in `Messa…
Bibo-Joshi Feb 4, 2024
cb4d17a
Remove api-7.0 branch from unit tests workflow
Bibo-Joshi Feb 4, 2024
2c26df9
Review, mostly adding new shortcut methods to `User`
Bibo-Joshi Feb 4, 2024
bedb2ce
A bit more review & double-checking doc changes from TG
Bibo-Joshi Feb 4, 2024
bedef17
Merge master
Bibo-Joshi Feb 5, 2024
da32d4e
Fix a test
Bibo-Joshi Feb 5, 2024
048f3d4
Fix whatever I could find in 1st round of reviews
harshil21 Feb 6, 2024
34dc49c
Merge branch 'master' into api-7.0
clot27 Feb 8, 2024
ba009a8
Fix precommit
clot27 Feb 8, 2024
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
78 changes: 69 additions & 9 deletions telegram/_bot.py
Bibo-Joshi marked this conversation as resolved.
Show resolved Hide resolved
Expand Up @@ -79,6 +79,7 @@
from telegram._forumtopic import ForumTopic
from telegram._games.gamehighscore import GameHighScore
from telegram._inline.inlinequeryresultsbutton import InlineQueryResultsButton
from telegram._linkpreviewoptions import LinkPreviewOptions
from telegram._menubutton import MenuButton
from telegram._message import Message
from telegram._messageid import MessageId
Expand Down Expand Up @@ -670,7 +671,7 @@ async def _send_message(
caption: Optional[str] = None,
parse_mode: ODVInput[str] = DEFAULT_NONE,
caption_entities: Optional[Sequence["MessageEntity"]] = None,
disable_web_page_preview: ODVInput[bool] = DEFAULT_NONE,
link_preview_options: ODVInput["LinkPreviewOptions"] = DEFAULT_NONE,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
Expand All @@ -688,12 +689,12 @@ async def _send_message(
using `Any` instead saves us a lot of `type: ignore` comments
"""
# We don't check if (DEFAULT_)None here, so that _post is able to insert the defaults
# correctly, if necessary
# correctly, if necessary:
data["disable_notification"] = disable_notification
data["allow_sending_without_reply"] = allow_sending_without_reply
data["protect_content"] = protect_content
data["parse_mode"] = parse_mode
data["disable_web_page_preview"] = disable_web_page_preview
data["link_preview_options"] = link_preview_options

if reply_to_message_id is not None:
data["reply_to_message_id"] = reply_to_message_id
Expand Down Expand Up @@ -800,13 +801,17 @@ async def send_message(
text: str,
parse_mode: ODVInput[str] = DEFAULT_NONE,
entities: Optional[Sequence["MessageEntity"]] = None,
# Deprecated since Bot API 7.0 (to be made keyword arg):
# ---
disable_web_page_preview: ODVInput[bool] = DEFAULT_NONE,
# ---
disable_notification: ODVInput[bool] = DEFAULT_NONE,
protect_content: ODVInput[bool] = DEFAULT_NONE,
reply_to_message_id: Optional[int] = None,
allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE,
reply_markup: Optional[ReplyMarkup] = None,
message_thread_id: Optional[int] = None,
link_preview_options: ODVInput["LinkPreviewOptions"] = DEFAULT_NONE,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
Expand All @@ -828,8 +833,23 @@ async def send_message(

.. versionchanged:: 20.0
|sequenceargs|
link_preview_options (:obj:`LinkPreviewOptions`, optional): Link preview generation
options for the message. Mutually exclusive with
:paramref:`disable_web_page_preview`.

.. versionadded:: NEXT.VERSION

disable_web_page_preview (:obj:`bool`, optional): Disables link previews for links in
this message.
this message. Mutually exclusive with :paramref:`link_preview_options`.

.. versionchanged:: NEXT.VERSION
Bot API 7.0 introduced :paramref:`link_preview_options` replacing this
argument. PTB will automatically convert this argument to that one, but
you should update your code to use the new argument.
Bibo-Joshi marked this conversation as resolved.
Show resolved Hide resolved

.. deprecated:: NEXT.VERSION
In future versions, this argument will become keyword only.
harshil21 marked this conversation as resolved.
Show resolved Hide resolved

disable_notification (:obj:`bool`, optional): |disable_notification|
protect_content (:obj:`bool`, optional): |protect_content|

Expand All @@ -848,11 +868,21 @@ async def send_message(
:class:`telegram.Message`: On success, the sent message is returned.

Raises:
:class:`telegram.error.TelegramError`
:exc:`ValueError`: If both :paramref:`disable_web_page_preview` and
:paramref:`link_preview_options` are passed.
:class:`telegram.error.TelegramError`: For other errors.

"""
data: JSONDict = {"chat_id": chat_id, "text": text, "entities": entities}

if disable_web_page_preview and link_preview_options:
raise ValueError(
"`disable_web_page_preview` and `link_preview_options` are mutually exclusive."
)
# Convert to LinkPreviewOptions:
if not isinstance(disable_web_page_preview, DefaultValue):
link_preview_options = LinkPreviewOptions(is_disabled=disable_web_page_preview)

return await self._send_message(
"sendMessage",
data,
Expand All @@ -863,7 +893,7 @@ async def send_message(
protect_content=protect_content,
message_thread_id=message_thread_id,
parse_mode=parse_mode,
disable_web_page_preview=disable_web_page_preview,
link_preview_options=link_preview_options,
read_timeout=read_timeout,
write_timeout=write_timeout,
connect_timeout=connect_timeout,
Expand Down Expand Up @@ -3339,9 +3369,13 @@ async def edit_message_text(
message_id: Optional[int] = None,
inline_message_id: Optional[str] = None,
parse_mode: ODVInput[str] = DEFAULT_NONE,
# Deprecated since Bot API 7.0 (to be keyword only):
# ---
disable_web_page_preview: ODVInput[bool] = DEFAULT_NONE,
# ---
reply_markup: Optional["InlineKeyboardMarkup"] = None,
entities: Optional[Sequence["MessageEntity"]] = None,
link_preview_options: ODVInput["LinkPreviewOptions"] = DEFAULT_NONE,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
Expand Down Expand Up @@ -3375,8 +3409,24 @@ async def edit_message_text(

.. versionchanged:: 20.0
|sequenceargs|

link_preview_options (:obj:`LinkPreviewOptions`, optional): Link preview generation
options for the message. Mutually exclusive with
:paramref:`disable_web_page_preview`.

.. versionadded:: NEXT.VERSION

disable_web_page_preview (:obj:`bool`, optional): Disables link previews for links in
this message.
this message. Mutually exclusive with :paramref:`link_preview_options`.

.. versionchanged:: NEXT.VERSION
Bot API 7.0 introduced :paramref:`link_preview_options` replacing this
argument. PTB will automatically convert this argument to that one, but
you should update your code to use the new argument.

.. deprecated:: NEXT.VERSION
In future versions, this argument will become keyword only.

reply_markup (:class:`telegram.InlineKeyboardMarkup`, optional): An object for an
inline keyboard.

Expand All @@ -3385,7 +3435,9 @@ async def edit_message_text(
edited message is returned, otherwise :obj:`True` is returned.

Raises:
:class:`telegram.error.TelegramError`
:exc:`ValueError`: If both :paramref:`disable_web_page_preview` and
:paramref:`link_preview_options` are passed.
:class:`telegram.error.TelegramError`: For other errors.

"""
data: JSONDict = {
Expand All @@ -3396,12 +3448,20 @@ async def edit_message_text(
"entities": entities,
}

if disable_web_page_preview and link_preview_options:
raise ValueError(
"`disable_web_page_preview` and `link_preview_options` are mutually exclusive."
)
# Convert to LinkPreviewOptions:
if not isinstance(disable_web_page_preview, DefaultValue):
link_preview_options = LinkPreviewOptions(is_disabled=disable_web_page_preview)

return await self._send_message(
"editMessageText",
data,
reply_markup=reply_markup,
parse_mode=parse_mode,
disable_web_page_preview=disable_web_page_preview,
link_preview_options=link_preview_options,
read_timeout=read_timeout,
write_timeout=write_timeout,
connect_timeout=connect_timeout,
Expand Down
58 changes: 49 additions & 9 deletions telegram/ext/_defaults.py
Expand Up @@ -20,7 +20,11 @@
import datetime
from typing import Any, Dict, NoReturn, Optional, final

from telegram import LinkPreviewOptions
from telegram._utils.datetime import UTC
from telegram._utils.types import ODVInput
from telegram._utils.warnings import warn
from telegram.warnings import PTBDeprecationWarning


@final
Expand All @@ -38,7 +42,12 @@ class Defaults:
parse_mode (:obj:`str`, optional): |parse_mode|
disable_notification (:obj:`bool`, optional): |disable_notification|
disable_web_page_preview (:obj:`bool`, optional): Disables link previews for links in this
message.
message. Mutually exclusive with :paramref:`link_preview_options`.

.. deprecated:: NEXT.VERSION
Use :paramref:`link_preview_options` instead. This parameter will be removed in
future versions.

allow_sending_without_reply (:obj:`bool`, optional): |allow_sending_without_reply|
quote (:obj:`bool`, optional): If set to :obj:`True`, the reply is sent as an actual reply
to the message. If ``reply_to_message_id`` is passed, this parameter will
Expand All @@ -56,18 +65,23 @@ class Defaults:
protect_content (:obj:`bool`, optional): |protect_content|

.. versionadded:: 20.0
link_preview_options (:class:`telegram.LinkPreviewOptions`, optional):
Link preview generation options for all outgoing messages. Mutually exclusive with
:paramref:`disable_web_page_preview`.

.. versionadded:: NEXT.VERSION
"""

__slots__ = (
"_tzinfo",
"_disable_web_page_preview",
"_block",
"_quote",
"_disable_notification",
"_allow_sending_without_reply",
"_parse_mode",
"_api_defaults",
"_protect_content",
"_link_preview_options",
)

def __init__(
Expand All @@ -80,25 +94,42 @@ def __init__(
block: bool = True,
allow_sending_without_reply: Optional[bool] = None,
protect_content: Optional[bool] = None,
link_preview_options: Optional["LinkPreviewOptions"] = None,
):
self._parse_mode: Optional[str] = parse_mode
self._disable_notification: Optional[bool] = disable_notification
self._disable_web_page_preview: Optional[bool] = disable_web_page_preview
self._allow_sending_without_reply: Optional[bool] = allow_sending_without_reply
self._quote: Optional[bool] = quote
self._tzinfo: datetime.tzinfo = tzinfo
self._block: bool = block
self._protect_content: Optional[bool] = protect_content

if disable_web_page_preview is not None and link_preview_options is not None:
raise ValueError(
"`disable_web_page_preview` and `link_preview_options` are mutually exclusive."
)

if disable_web_page_preview is not None:
warn(
"`Defaults.disable_web_page_preview` is deprecated. Use "
"`Defaults.link_preview_options` instead.",
category=PTBDeprecationWarning,
stacklevel=2,
Bibo-Joshi marked this conversation as resolved.
Show resolved Hide resolved
)
self._link_preview_options: Optional[LinkPreviewOptions] = LinkPreviewOptions(
is_disabled=disable_web_page_preview
)
else:
self._link_preview_options = link_preview_options
# Gather all defaults that actually have a default value
self._api_defaults = {}
for kwarg in (
"parse_mode",
"explanation_parse_mode",
"disable_notification",
"disable_web_page_preview",
"allow_sending_without_reply",
"protect_content",
"link_preview_options",
):
value = getattr(self, kwarg)
if value is not None:
Expand All @@ -115,7 +146,7 @@ def __hash__(self) -> int:
(
self._parse_mode,
self._disable_notification,
self._disable_web_page_preview,
self.disable_web_page_preview,
self._allow_sending_without_reply,
self._quote,
self._tzinfo,
Expand Down Expand Up @@ -178,11 +209,11 @@ def disable_notification(self, value: object) -> NoReturn:
)

@property
def disable_web_page_preview(self) -> Optional[bool]:
""":obj:`bool`: Optional. Disables link previews for links in this
message.
def disable_web_page_preview(self) -> ODVInput[bool]:
""":obj:`bool`: Optional. Disables link previews for links in all outgoing
messages.
Bibo-Joshi marked this conversation as resolved.
Show resolved Hide resolved
"""
return self._disable_web_page_preview
return self._link_preview_options.is_disabled if self._link_preview_options else None

@disable_web_page_preview.setter
def disable_web_page_preview(self, value: object) -> NoReturn:
Expand Down Expand Up @@ -252,3 +283,12 @@ def protect_content(self, value: object) -> NoReturn:
raise AttributeError(
"You can't assign a new value to protect_content after initialization."
)

@property
def link_preview_options(self) -> Optional["LinkPreviewOptions"]:
""":class:`telegram.LinkPreviewOptions`: Optional. Link preview generation options for all
outgoing messages.

.. versionadded:: NEXT.VERSION
"""
return self._link_preview_options