-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #447 from Tishka17/feature/link_preview
Link preview
- Loading branch information
Showing
17 changed files
with
304 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from aiogram.filters.state import State, StatesGroup | ||
|
||
from aiogram_dialog import Window | ||
from aiogram_dialog.widgets.link_preview import LinkPreview | ||
from aiogram_dialog.widgets.text import Const | ||
|
||
|
||
class SG(StatesGroup): | ||
MAIN = State() | ||
SECOND = State() | ||
|
||
|
||
window = Window( | ||
Const("https://nplus1.ru/news/2024/05/23/voyager-1-science-data"), | ||
LinkPreview(is_disabled=True), | ||
state=SG.MAIN, | ||
) | ||
|
||
second_window = Window( | ||
Const("some text"), | ||
LinkPreview( | ||
url=Const("https://nplus1.ru/news/2024/05/23/voyager-1-science-data"), | ||
prefer_small_media=True, | ||
show_above_text=True, | ||
), | ||
state=SG.MAIN, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
.. _link_preview: | ||
|
||
LinkPreview | ||
************* | ||
|
||
The **LinkPreview** widget is used to manage link previews in messages. | ||
|
||
Parameters: | ||
|
||
* ``url``: A ``TextWidget`` with URL to be used in the link preview. If not provided, the first URL found in the message will be used. | ||
* ``is_disabled``: that controls whether the link preview is displayed. If ``True``, the preview will be disabled. | ||
* ``prefer_small_media``: that controls if the media in the link preview should be displayed in a smaller size. Ignored if media size change is not supported. | ||
* ``prefer_large_media``: that controls if the media in the link preview should be enlarged. Ignored if media size change is not supported. | ||
* ``show_above_text``: that specifies whether the link preview should be displayed above the message text. If ``True``, link preview be displayed above the message text. | ||
|
||
|
||
Code example: | ||
|
||
.. literalinclude:: ./example.py | ||
|
||
.. autoclass:: aiogram_dialog.widgets.link_preview.LinkPreview | ||
:special-members: __init__ | ||
:members: render_link_preview, _render_link_preview |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from aiogram_dialog import ( | ||
Dialog, | ||
Window, | ||
) | ||
from aiogram_dialog.widgets.kbd import SwitchTo | ||
from aiogram_dialog.widgets.link_preview import LinkPreview | ||
from aiogram_dialog.widgets.text import Const, Format | ||
|
||
from . import states | ||
from .common import MAIN_MENU_BUTTON | ||
|
||
|
||
async def links_getter(**_): | ||
return { | ||
"main": "https://en.wikipedia.org/wiki/HTML_element", | ||
"photo": "https://en.wikipedia.org/wiki/Hyperlink", | ||
} | ||
|
||
|
||
LinkPreview_MAIN_MENU_BUTTON = SwitchTo( | ||
text=Const("Back"), id="back", state=states.LinkPreview.MAIN, | ||
) | ||
COMMON_TEXT = Format( | ||
"This is demo of different link preview options.\n" | ||
"Link in text: {main}\n" | ||
"Link in preview can be different\n\n" | ||
"Current mode is:", | ||
) | ||
|
||
BACK = SwitchTo(Const("back"), "_back", states.LinkPreview.MAIN) | ||
|
||
link_preview_dialog = Dialog( | ||
Window( | ||
COMMON_TEXT, | ||
Format("Default"), | ||
SwitchTo( | ||
Const("disable"), "_disable", states.LinkPreview.IS_DISABLED, | ||
), | ||
SwitchTo( | ||
Const("prefer small media"), "_prefer_small_media", | ||
states.LinkPreview.SMALL_MEDIA, | ||
), | ||
SwitchTo( | ||
Const("prefer large media"), "_prefer_large_media", | ||
states.LinkPreview.LARGE_MEDIA, | ||
), | ||
SwitchTo( | ||
Const("show above text"), "_show_above_text", | ||
states.LinkPreview.SHOW_ABOVE_TEXT, | ||
), | ||
MAIN_MENU_BUTTON, | ||
state=states.LinkPreview.MAIN, | ||
), | ||
Window( | ||
COMMON_TEXT, | ||
Const("is_disabled=True"), | ||
LinkPreview(is_disabled=True), | ||
LinkPreview_MAIN_MENU_BUTTON, | ||
state=states.LinkPreview.IS_DISABLED, | ||
), | ||
Window( | ||
COMMON_TEXT, | ||
Const("prefer_small_media=True"), | ||
LinkPreview(Format("{photo}"), prefer_small_media=True), | ||
LinkPreview_MAIN_MENU_BUTTON, | ||
state=states.LinkPreview.SMALL_MEDIA, | ||
), | ||
Window( | ||
COMMON_TEXT, | ||
Const("prefer_large_media=True"), | ||
LinkPreview(Format("{photo}"), prefer_large_media=True), | ||
LinkPreview_MAIN_MENU_BUTTON, | ||
state=states.LinkPreview.LARGE_MEDIA, | ||
), | ||
Window( | ||
COMMON_TEXT, | ||
Const("show_above_text=True"), | ||
LinkPreview(Format("{photo}"), show_above_text=True), | ||
LinkPreview_MAIN_MENU_BUTTON, | ||
state=states.LinkPreview.SHOW_ABOVE_TEXT, | ||
), | ||
getter=links_getter, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
__all__ = ["LinkPreviewBase", "LinkPreview"] | ||
|
||
from .base import LinkPreview, LinkPreviewBase |
Oops, something went wrong.