Skip to content

Commit 766a52d

Browse files
committed
(feat) helper method
1 parent be74426 commit 766a52d

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

main.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,20 @@ async def restore_agent_routines(application: Application) -> int:
542542
return restored
543543

544544

545+
async def send_to_telegram(self, chat_id: int, message: str, parse_mode: str = "Markdown"):
546+
"""Sends a message to a specific Telegram chat."""
547+
await self.bot.send_message(chat_id=chat_id, text=message, parse_mode=parse_mode)
548+
549+
550+
async def send_to_all(self, message: str, parse_mode: str = "Markdown"):
551+
"""Sends a message to all users who have started the bot."""
552+
for chat_id in self.user_data:
553+
try:
554+
await self.bot.send_message(chat_id=chat_id, text=message, parse_mode=parse_mode)
555+
except Exception as e:
556+
logger.warning(f"Failed to send message to chat {chat_id}: {e}")
557+
558+
545559
def main() -> None:
546560
"""Run the bot."""
547561
# Setup persistence to save user data, chat data, and bot data
@@ -579,4 +593,7 @@ async def post_shutdown(application: Application) -> None:
579593

580594

581595
if __name__ == "__main__":
596+
# Add custom methods to the application object
597+
Application.send_to_telegram = send_to_telegram
598+
Application.send_to_all = send_to_all
582599
main()

0 commit comments

Comments
 (0)