1
0
Fork 0

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 <a \
href="{{status.reblog.account.url}}">{{status.reblog.account.name}}</a>
{% if status.reblog.spoiler_text %}{{status.reblog.spoiler_text}}
<tg-spoiler>{% endif %}{{status.reblog.content_flathtml}}\
{% if status.reblog.spoiler_text %}</tg-spoiler>{% endif %}
{% else %}
{% if status.spoiler_text %}{{status.spoiler_text}}
<tg-spoiler>{% endif %}{{status.content_flathtml}}\
{% if status.spoiler_text %}</tg-spoiler>{% endif %}
{% endif %}

<a href="{{status.link}}">Link to post</a>

You asked for this :)
This commit is contained in:
Casey 2022-08-31 18:12:38 +03:00
parent 881b1e1532
commit 2006d489c3
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
4 changed files with 24 additions and 22 deletions

View File

@ -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 <a href="{{status.reblog.account.url}}">{{status.reblog.account.name}}</a>
{% endif %}{% if status.reblog_or_status.spoiler_text %}{{status.reblog_or_status.spoiler_text}}
<tg-spoiler>{% endif %}{{ status.reblog_or_status.content_flathtml }}{% if status.reblog_or_status.spoiler_text %}</tg-spoiler>{% endif %}
<a href="{{status.link}}">Link to post</a>
;# Boost filter. Only boosts will be matched by that one
;[filter/boost]

View File

@ -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():

View File

@ -45,9 +45,10 @@ DEFAULT_TEMPLATE: str = """\
Boost from <a href="{{status.reblog.account.url}}">\
{{status.reblog.account.name}}</a>\
{% endif %}\
{% if status.spoiler_text %}{{status.spoiler_text}}
<tg-spoiler>{% endif %}{{ status.content_flathtml }}\
{% if status.spoiler_text %}</tg-spoiler>{% endif %}
{% if status.reblog_or_status.spoiler_text %}\
{{status.reblog_or_status.spoiler_text}}
<tg-spoiler>{% endif %}{{ status.reblog_or_status.content_flathtml }}\
{% if status.reblog_or_status.spoiler_text %}</tg-spoiler>{% endif %}
<a href="{{status.link}}">Link to post</a>"""
@ -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 (
"<TelegramIntegration "
"chat_id={chat!r} "
"show_post_link={show_post_link!r} "
"show_boost_from={show_boost_from!r} "
"template={template!r} "
"silent={silent!r}>"
).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)

View File

@ -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)