Merge pull request #27 from hatkidchan/26-media-spoilers-in-telegram

Added spoilers (closes #26)
This commit is contained in:
Casey 2023-01-20 13:03:28 +03:00 committed by GitHub
commit 80e845300f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 4 deletions

View File

@ -44,6 +44,13 @@ MEDIA_MAPPING: Mapping[str, str] = {
"audio": "audio", "audio": "audio",
"unknown": "document", "unknown": "document",
} }
MEDIA_SPOILER_SUPPORT: Mapping[str, bool] = {
"image": True,
"video": True,
"gifv": True,
"audio": False,
"unknown": False,
}
DEFAULT_TEMPLATE: str = """\ DEFAULT_TEMPLATE: str = """\
{% if status.reblog %}\ {% if status.reblog %}\
Boost from <a href="{{status.reblog.account.url}}">\ Boost from <a href="{{status.reblog.account.url}}">\
@ -110,7 +117,9 @@ class TelegramIntegration(BaseIntegration):
text=text, text=text,
) )
async def _post_media(self, text: str, media: Attachment) -> TGResponse: async def _post_media(
self, text: str, media: Attachment, spoiler: bool = False
) -> TGResponse:
# Just to be safe # Just to be safe
if media.type not in MEDIA_MAPPING: if media.type not in MEDIA_MAPPING:
logger.warning( logger.warning(
@ -126,10 +135,12 @@ class TelegramIntegration(BaseIntegration):
chat_id=self.chat_id, chat_id=self.chat_id,
caption=text, caption=text,
**{MEDIA_MAPPING[media.type]: media.url}, **{MEDIA_MAPPING[media.type]: media.url},
has_spoiler=MEDIA_SPOILER_SUPPORT.get(media.type, False)
and spoiler,
) )
async def _post_mediagroup( async def _post_mediagroup(
self, text: str, media: List[Attachment] self, text: str, media: List[Attachment], spoiler: bool = False
) -> TGResponse: ) -> TGResponse:
logger.debug("Sendind media group: %r (text=%r)", media, text) logger.debug("Sendind media group: %r (text=%r)", media, text)
media_list: List[dict] = [] media_list: List[dict] = []
@ -149,6 +160,11 @@ class TelegramIntegration(BaseIntegration):
{ {
"type": MEDIA_MAPPING[attachment.type], "type": MEDIA_MAPPING[attachment.type],
"media": attachment.url, "media": attachment.url,
**(
{"has_spoiler": spoiler}
if MEDIA_SPOILER_SUPPORT.get(attachment.type, False)
else {}
),
} }
) )
if len(media_list) == 1: if len(media_list) == 1:
@ -185,6 +201,7 @@ class TelegramIntegration(BaseIntegration):
async def __call__(self, status: Status) -> Optional[str]: async def __call__(self, status: Status) -> Optional[str]:
source = status.reblog or status source = status.reblog or status
has_spoiler = source.spoiler_text != ""
text = self.template.render({"status": status}) text = self.template.render({"status": status})
ids = [] ids = []
@ -197,14 +214,14 @@ class TelegramIntegration(BaseIntegration):
elif len(source.media_attachments) == 1: elif len(source.media_attachments) == 1:
if ( if (
res := await self._post_media( res := await self._post_media(
text, source.media_attachments[0] text, source.media_attachments[0], has_spoiler
) )
).ok and res.result is not None: ).ok and res.result is not None:
ids.append(res.result["message_id"]) ids.append(res.result["message_id"])
else: else:
if ( if (
res := await self._post_mediagroup( res := await self._post_mediagroup(
text, source.media_attachments text, source.media_attachments, has_spoiler
) )
).ok and res.result is not None: ).ok and res.result is not None:
ids.append(res.result["message_id"]) ids.append(res.result["message_id"])