-
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.
- Loading branch information
Showing
8 changed files
with
122 additions
and
8 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
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 |
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,61 @@ | ||
from typing import Optional | ||
|
||
from aiogram.types import LinkPreviewOptions | ||
|
||
from aiogram_dialog import DialogManager | ||
from aiogram_dialog.api.internal import LinkPreviewWidget, TextWidget | ||
from aiogram_dialog.widgets.common import BaseWidget, Whenable, WhenCondition | ||
|
||
|
||
class LinkPreviewBase(Whenable, BaseWidget, LinkPreviewWidget): | ||
def __init__(self, when: WhenCondition = None): | ||
super().__init__(when=when) | ||
|
||
async def render_link_preview( | ||
self, data: dict, manager: DialogManager, | ||
) -> Optional[LinkPreviewOptions]: | ||
if not self.is_(data, manager): | ||
return None | ||
return await self._render_link_preview(data, manager) | ||
|
||
async def _render_link_preview( | ||
self, data: dict, manager: DialogManager, | ||
) -> Optional[LinkPreviewOptions]: | ||
return None | ||
|
||
|
||
class LinkPreview(LinkPreviewBase): | ||
def __init__( | ||
self, | ||
url: TextWidget, | ||
is_disabled: bool = False, | ||
prefer_small_media: bool = False, | ||
prefer_large_media: bool = False, | ||
show_above_text: bool = False, | ||
when: WhenCondition = None, | ||
): | ||
super().__init__(when=when) | ||
self.url = url | ||
self.is_disabled = is_disabled | ||
self.prefer_small_media = prefer_small_media | ||
self.prefer_large_media = prefer_large_media | ||
self.show_above_text = show_above_text | ||
|
||
async def render_link_preview( | ||
self, data: dict, manager: DialogManager, | ||
) -> Optional[LinkPreviewOptions]: | ||
if not self.is_(data, manager): | ||
return None | ||
return await self._render_link_preview(data, manager) | ||
|
||
async def _render_link_preview( | ||
self, data: dict, manager: DialogManager, | ||
) -> Optional[LinkPreviewOptions]: | ||
url = await self.url.render_text(data, manager) | ||
return LinkPreviewOptions( | ||
url=url, | ||
is_disabled=self.is_disabled, | ||
prefer_small_media=self.prefer_small_media, | ||
prefer_large_media=self.prefer_large_media, | ||
show_above_text=self.show_above_text, | ||
) |
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