From 2006d489c37e7074de3eaf5f058b12b710b00718 Mon Sep 17 00:00:00 2001 From: hkc Date: Wed, 31 Aug 2022 18:12:38 +0300 Subject: [PATCH] I think formatting works properly now? When boosting something, status contents are not stored in status itself, but rather in the `status.reblog`. Since it'd be really awkward to check for boosts too, I just decided to add separate `@property` that returns either reblog or status itself, depending on the value of `status.reblog`. That required a bit of tweaking. Also now we're using ExtendedInterpolation, so you can do the `${tokens.telegram}` and add separate section for your secrets. TODO: add support for environment variables (maybe) For those who's wondering how manual check for boost would look in the template, here ya go :) {% if status.reblog %}Boost from {{status.reblog.account.name}} {% if status.reblog.spoiler_text %}{{status.reblog.spoiler_text}} {% endif %}{{status.reblog.content_flathtml}}\ {% if status.reblog.spoiler_text %}{% endif %} {% else %} {% if status.spoiler_text %}{{status.spoiler_text}} {% endif %}{{status.content_flathtml}}\ {% if status.spoiler_text %}{% endif %} {% endif %} Link to post You asked for this :) --- config.ini | 15 +++++++++------ mastoposter/__main__.py | 4 ++-- mastoposter/integrations/telegram.py | 23 +++++++++-------------- mastoposter/types.py | 4 ++++ 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/config.ini b/config.ini index 279b0e4..ba875c4 100644 --- a/config.ini +++ b/config.ini @@ -46,12 +46,6 @@ token = 12345:blahblah # username, if it is public chat = @username -# Should we show link to post as a link after post content? -show-post-link = yes - -# Should we show link to original author before post content? -show-boost-from = yes - # Should we make posts silent? # https://core.telegram.org/bots/api#sendmessage `disable_notification` silent = true @@ -63,6 +57,15 @@ type = discord # Webhook URL with the `?wait=true` webhook = url +# Jinja2 template string for the post. Works only in Telegram. +# This is the default template, not specifying that property at all will result +# in this string (probably) +# Pay attention to 4 spaces in the empty line, I think it's required +template = {% if status.reblog %}Boost from {{status.reblog.account.name}} + {% endif %}{% if status.reblog_or_status.spoiler_text %}{{status.reblog_or_status.spoiler_text}} + {% endif %}{{ status.reblog_or_status.content_flathtml }}{% if status.reblog_or_status.spoiler_text %}{% endif %} + + Link to post ;# Boost filter. Only boosts will be matched by that one ;[filter/boost] diff --git a/mastoposter/__main__.py b/mastoposter/__main__.py index 15eb5cb..ea645e5 100644 --- a/mastoposter/__main__.py +++ b/mastoposter/__main__.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 from asyncio import run -from configparser import ConfigParser +from configparser import ConfigParser, ExtendedInterpolation from mastoposter import execute_integrations, load_integrations_from from mastoposter.integrations import FilteredIntegration from mastoposter.sources import websocket_source @@ -34,7 +34,7 @@ async def listen( def main(config_path: str): - conf = ConfigParser() + conf = ConfigParser(interpolation=ExtendedInterpolation()) conf.read(config_path) for section in conf.sections(): diff --git a/mastoposter/integrations/telegram.py b/mastoposter/integrations/telegram.py index 7e7b879..a916684 100644 --- a/mastoposter/integrations/telegram.py +++ b/mastoposter/integrations/telegram.py @@ -45,9 +45,10 @@ DEFAULT_TEMPLATE: str = """\ Boost from \ {{status.reblog.account.name}}\ {% endif %}\ -{% if status.spoiler_text %}{{status.spoiler_text}} -{% endif %}{{ status.content_flathtml }}\ -{% if status.spoiler_text %}{% endif %} +{% if status.reblog_or_status.spoiler_text %}\ +{{status.reblog_or_status.spoiler_text}} +{% endif %}{{ status.reblog_or_status.content_flathtml }}\ +{% if status.reblog_or_status.spoiler_text %}{% endif %} Link to post""" @@ -56,10 +57,10 @@ class TelegramIntegration(BaseIntegration): def __init__(self, sect: SectionProxy): self.token = sect.get("token", "") self.chat_id = sect.get("chat", "") - self.show_post_link = sect.getboolean("show_post_link", True) - self.show_boost_from = sect.getboolean("show_boost_from", True) self.silent = sect.getboolean("silent", True) - self.template = Template(sect.get("template", DEFAULT_TEMPLATE)) + self.template: Template = Template( + sect.get("template", DEFAULT_TEMPLATE) + ) async def _tg_request(self, method: str, **kwargs) -> TGResponse: url = API_URL.format(self.token, method) @@ -181,12 +182,6 @@ class TelegramIntegration(BaseIntegration): return ( "" - ).format( - chat=self.chat_id, - show_post_link=self.show_post_link, - show_boost_from=self.show_boost_from, - silent=self.silent, - ) + ).format(chat=self.chat_id, silent=self.silent, template=self.template) diff --git a/mastoposter/types.py b/mastoposter/types.py index 8010285..462e08e 100644 --- a/mastoposter/types.py +++ b/mastoposter/types.py @@ -312,6 +312,10 @@ class Status: tags=[Tag.from_dict(m) for m in data.get("tags", [])], ) + @property + def reblog_or_status(self) -> "Status": + return self.reblog or self + @property def link(self) -> str: return self.account.url + "/" + str(self.id)