|
33 | 33 | POLLING_INTERVAL = 60 |
34 | 34 | REMINDERS_ENABLED = config.getboolean('Reminders', 'EnableReminders', fallback=False) # Still try to read enable flag |
35 | 35 |
|
| 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 | + |
36 | 53 | # --- Corrected Function Signature --- |
37 | 54 | async def reminder_poller(application: Application): |
38 | 55 | """Periodically checks for due reminders and sends notifications.""" |
@@ -63,19 +80,24 @@ async def reminder_poller(application: Application): |
63 | 80 | reminder_id = r['reminder_id'] |
64 | 81 | user_id = r['user_id'] |
65 | 82 | 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) |
68 | 90 |
|
69 | | - # Attempt to send the reminder |
70 | 91 | 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 |
79 | 101 | db_utils.update_reminder_status(REMINDERS_DB_PATH, reminder_id, 'sent') |
80 | 102 | logger.info(f"Sent reminder {reminder_id} to chat {chat_id} for user {user_id}.") |
81 | 103 |
|
|
0 commit comments