Notify Manager
Section titled “Notify Manager”Following is an upgrade to the d:\FSS\Software\Utils\PythonUtils\notify_manager.py utility to provide robust texting for URGENT notifications (like website or web app offline).
Source: Gemini
=> Before using Telegram solution for cell notification
- still prefer “normal” cell carrier solution, not requiring additional app
- research better solution: Twilio free usage sufficient for minimal texts?
Telegram “Later” Checklist
Section titled “Telegram “Later” Checklist”-
App: Install Telegram on your phone and register.
-
Bot: Message
@BotFather, type/newbot, and save the API Token. -
ID: Message
@userinfobotto get your numeric Chat ID. -
Verification: Run the
test_telegram.pyscript provided above. -
Integration: Update
notify_manager.pywith the newurlliblogic.
🛡️ Current Security Status
Section titled “🛡️ Current Security Status”-
.env: Now ignored globally by Git via yourC:\Users\Admin\.gitignore_global. -
History: All existing
.gitrepos underD:\FSShave been purged of.envfiles using your newpurge-git.ps1. -
Remotes: Remember to run
git push origin --force --allonly if you eventually decide to sync those folders to a remote server.
Since you have never used Telegram, think of it as a professional-grade notification service for developers. It is much more reliable than SMS because it bypasses carrier spam filters entirely.
Here are the 5 steps to go from zero to receiving a notification on your phone.
1. Install the App
Section titled “1. Install the App”Telegram is a messaging app available for all platforms. You need it installed to receive the “text” alerts.
-
On your Phone: Search for “Telegram Messenger” in the Google Play Store (Android) or App Store (iPhone).
-
On your Desktop (Optional): You can also get it at desktop.telegram.org to copy-paste tokens more easily.
-
Signup: Register with your phone number.
2. Create your “Bot” (The Sender)
Section titled “2. Create your “Bot” (The Sender)”In Telegram, bots are created by talking to a master bot called the “BotFather.”
-
In the Telegram app, tap the Search icon and type
@BotFather. -
Tap on the one with the blue checkmark.
-
Press Start (or type
/start). -
Type
/newbotand follow the prompts:-
Display Name: Give it a name (e.g.,
FSS Alerts). -
Username: Give it a unique username ending in “bot” (e.g.,
tstevens_alerts_bot).
-
-
The Result: It will give you an API Token (a long string of numbers and letters). Copy this down. This is your
TELEGRAM_BOT_TOKEN.
3. Get your “Chat ID” (The Receiver)
Section titled “3. Get your “Chat ID” (The Receiver)”Your bot needs to know who to send the message to. In Telegram, every user has a numeric ID.
-
Search for your newly created bot username (e.g.,
@tstevens_alerts_bot) in Telegram and press Start. -
Search for the bot
@userinfobotin Telegram and press Start. -
It will immediately reply with your Id (a 9 or 10-digit number). Copy this. This is your
chat_id.
4. Update your Config Files
Section titled “4. Update your Config Files”Now, update the two files you sent me earlier with these new values.
In D:\FSS\Software\Utils\PythonUtils\.env: Add your token at the bottom.
Plaintext
TELEGRAM_BOT_TOKEN=12345678:ABCDefghIJKLmnop_ExampleTokenIn D:\FSS\Software\Utils\PythonUtils\Backups\my_backup.yaml.txt: Replace the old sms section with a telegram section.
YAML
notifications: enabled: true email: # ... (keep your email settings as they are)
telegram: enabled: true chat_id: "123456789" # Put the number from @userinfobot here5. Update your Python Script
Section titled “5. Update your Python Script”I have streamlined the logic from your notify_manager.py to use the Telegram API.
Replace your NotificationManager class in notify_manager.py with this:
Python
import smtplibimport osimport urllib.parseimport urllib.requestimport jsonfrom email.message import EmailMessage
class NotificationManager: def __init__(self, config): self.config = config self.enabled = config.get('enabled', False) self.email_conf = config.get('email', {}) self.telegram_conf = config.get('telegram', {})
# Secrets from .env self.email_password = os.getenv('EMAIL_PASSWORD') self.tg_token = os.getenv('TELEGRAM_BOT_TOKEN')
def send_failure_alert(self, tool_name, errors): if not self.enabled: return
subject = f"FAILURE: {tool_name}" error_list = "\n".join([f"- {e}" for e in errors])
# 1. Send Email if self.email_password: self._send_email(subject, error_list)
# 2. Send Telegram (if enabled) if self.telegram_conf.get('enabled') and self.tg_token: msg = f"🚨 <b>{subject}</b>\n\n{error_list}" self._send_telegram(msg)
def _send_email(self, subject, body): try: msg = EmailMessage() msg.set_content(body) msg['Subject'] = f"🚨 {subject}" msg['From'] = f"FSS Alert <{self.email_conf['user']}>" msg['To'] = self.email_conf['to_email'] with smtplib.SMTP(self.email_conf['smtp_server'], self.email_conf['smtp_port']) as server: server.starttls() server.login(self.email_conf['user'], self.email_password) server.send_message(msg) except Exception as e: print(f"[Error] Email failed: {e}")
def _send_telegram(self, message): chat_id = self.telegram_conf.get('chat_id') url = f"https://api.telegram.org/bot{self.tg_token}/sendMessage" payload = { 'chat_id': chat_id, 'text': message, 'parse_mode': 'HTML' } try: data = urllib.parse.urlencode(payload).encode('utf-8') req = urllib.request.Request(url, data=data) with urllib.request.urlopen(req) as response: return json.loads(response.read().decode()) except Exception as e: print(f"[Error] Telegram failed: {e}")Would you like me to provide a 5-line “test_telegram.py” script so you can verify the notification arrives before you integrate it into your backup workflow?