Skip to content

Commit 2c5b97f

Browse files
committed
msg max length handling in notifications
1 parent c20a0c2 commit 2c5b97f

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

src/reminder_poller.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@
3333
POLLING_INTERVAL = 60
3434
REMINDERS_ENABLED = config.getboolean('Reminders', 'EnableReminders', fallback=False) # Still try to read enable flag
3535

36+
# split to fit to telegram's msg length
37+
MAX_TG_MSG_LENGTH = 4096
38+
39+
def split_long_message(message, max_length=MAX_TG_MSG_LENGTH):
40+
"""
41+
Splits a message into multiple parts, each up to max_length characters,
42+
and returns a list of parts.
43+
"""
44+
parts = []
45+
start_index = 0
46+
while start_index < len(message):
47+
# Slice out a chunk of up to 'max_length' characters
48+
part = message[start_index:start_index + max_length]
49+
parts.append(part)
50+
start_index += max_length
51+
return parts
52+
3653
# --- Corrected Function Signature ---
3754
async def reminder_poller(application: Application):
3855
"""Periodically checks for due reminders and sends notifications."""
@@ -63,19 +80,24 @@ async def reminder_poller(application: Application):
6380
reminder_id = r['reminder_id']
6481
user_id = r['user_id']
6582
chat_id = r['chat_id']
66-
text = r['reminder_text']
67-
msg = f"🔔 {text}" # Use a bell emoji
83+
raw_text = r['reminder_text']
84+
85+
# The text you'd like to send (with an optional emoji, etc.)
86+
msg = f"🔔 {raw_text}"
87+
88+
# 1) Split into multiple parts if over 4k
89+
msg_parts = split_long_message(msg)
6890

69-
# Attempt to send the reminder
7091
try:
71-
# --- Use application.bot to send ---
72-
await application.bot.send_message(
73-
chat_id=chat_id,
74-
text=msg,
75-
parse_mode=ParseMode.HTML
76-
# Consider adding parse_mode=ParseMode.HTML if needed later
77-
)
78-
# --- Update status using correct function and DB path ---
92+
# 2) Send each part in a separate message
93+
for part in msg_parts:
94+
await application.bot.send_message(
95+
chat_id=chat_id,
96+
text=part,
97+
parse_mode=ParseMode.HTML
98+
)
99+
100+
# 3) Mark the reminder as sent
79101
db_utils.update_reminder_status(REMINDERS_DB_PATH, reminder_id, 'sent')
80102
logger.info(f"Sent reminder {reminder_id} to chat {chat_id} for user {user_id}.")
81103

0 commit comments

Comments
 (0)