Skip to content

Commit

Permalink
Allow configuring sender text through feeds YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
Electronic-Mango committed Nov 17, 2024
1 parent a526a2b commit 5b98c60
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ Feed name 1:
filters:
- some string
- some other string
# String format used when creating update text to identify where the update comes from.
# Elements "{name}" and "{type}" are replaced by specific feed name and feed type
# (in this case "Feed name 1"). Both are optional.
# This string is also converted to inline URL pointing to actual update.
# Final string will be appended to the end of update description.
# This string can contain basic HTML format tags. List of supported tags by Telegram bots:
# https://core.telegram.org/bots/api#html-style
# The field is optional, if absent a default text format is used:
# "By <b>{name}</b> on <b>{type}</b>"
sender_text_format: Posted by <b>{name}</b> in <i>{type}</i>!
```

Out of all parameters only `url` is required, others are optional.
Expand Down
10 changes: 10 additions & 0 deletions feed_links.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ Feed name 1:
filters:
- some string
- some other string
# String format used when creating update text to identify where the update comes from.
# Elements "{name}" and "{type}" are replaced by specific feed name and feed type
# (in this case "Feed name 1"). Both are optional.
# This string is also converted to inline URL pointing to actual update.
# Final string will be appended to the end of update description.
# This string can contain basic HTML format tags. List of supported tags by Telegram bots:
# https://core.telegram.org/bots/api#html-style
# The field is optional, if absent a default text format is used:
# "By <b>{name}</b> on <b>{type}</b>"
sender_text_format: Posted by <b>{name}</b> in <i>{type}</i>!
Feed name 2:
url: https://{source_pattern}.feedlink2.com/rss
show_title: true
Expand Down
10 changes: 8 additions & 2 deletions src/main/bot/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
from telegram import Bot, InputMediaPhoto, InputMediaVideo

from db.wrapper import store_pinned_message
from settings import MAX_MEDIA_ITEMS_PER_MESSAGE, MAX_MESSAGE_SIZE, PIN_VIDEOS
from settings import MAX_MEDIA_ITEMS_PER_MESSAGE, MAX_MESSAGE_SIZE, PIN_VIDEOS, RSS_FEEDS

DEFAULT_SENDER_TEXT_FORMAT = "By <b>{name}</b> on <b>{type}</b>"
MAX_IMAGE_SIZE = 10_000_000
MAX_IMAGE_DIMENSIONS = 10_000
MAX_IMAGE_THUMBNAIL = (MAX_IMAGE_DIMENSIONS // 2, MAX_IMAGE_DIMENSIONS // 2)
Expand Down Expand Up @@ -60,12 +61,17 @@ def _format_message(
message_text = f"{title}" if title else ""
message_text += f"\n\n{description}" if description else ""
sender_text = "\n\n" if len(message_text) else ""
sender_text += f"<a href='{link}'>By <b>{feed_name}</b> on {feed_type}</a>"
sender_text += f"<a href='{link}'>{_prepare_sender_text(feed_type, feed_name)}</a>"
message_text = _trim_message(chat_id, message_text, len(sender_text))
message_text += sender_text
return message_text


def _prepare_sender_text(feed_type: str, feed_name: str) -> str:
text_format = RSS_FEEDS[feed_type].get("sender_text_format", None) or DEFAULT_SENDER_TEXT_FORMAT
return str(text_format).format(name=feed_name, type=feed_type)


def _trim_message(chat_id: int, message: str, appended_size: int) -> str:
effective_max_message_size = MAX_MESSAGE_SIZE - appended_size
if len(message) > effective_max_message_size:
Expand Down

0 comments on commit 5b98c60

Please sign in to comment.