Spaces:
Sleeping
Sleeping
Download telegram_notifier.py from upandit/mood-app-api-version: direct link, hf CLI and curl.
- Browser
- Download file 3.69 kB
-
https://huggingface.co/spaces/upandit/mood-app-api-version/resolve/main/telegram_notifier.py
- Command line
-
hf download hf://spaces/upandit/mood-app-api-version/telegram_notifier.py
-
curl -L -o telegram_notifier.py https://huggingface.co/spaces/upandit/mood-app-api-version/resolve/main/telegram_notifier.py
3.69 kB
| #!/usr/bin/env python3 | |
| """ | |
| Telegram Bot Notifier for GitHub Actions | |
| Sends notifications when GitHub Actions complete | |
| """ | |
| import requests | |
| import json | |
| import os | |
| import sys | |
| from datetime import datetime | |
| # Your bot token (keep this secure!) | |
| BOT_TOKEN = "8119497602:AAEGqaZ_HJjX_asIu43rZMqoDFnz_jmq3dY" | |
| # Get chat ID from environment variable | |
| CHAT_ID = os.getenv("TELEGRAM_CHAT_ID") | |
| def send_telegram_message(message, parse_mode="HTML"): | |
| """ | |
| Send a message to Telegram bot | |
| """ | |
| if not CHAT_ID: | |
| print("β TELEGRAM_CHAT_ID not set. Please set your chat ID.") | |
| return False | |
| url = f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage" | |
| payload = { | |
| "chat_id": CHAT_ID, | |
| "text": message, | |
| "parse_mode": parse_mode | |
| } | |
| try: | |
| response = requests.post(url, json=payload) | |
| response.raise_for_status() | |
| print("β Telegram message sent successfully!") | |
| return True | |
| except requests.exceptions.RequestException as e: | |
| print(f"β Failed to send Telegram message: {e}") | |
| return False | |
| def get_chat_id(): | |
| """ | |
| Helper function to get your chat ID | |
| Run this first to get your chat ID | |
| """ | |
| url = f"https://api.telegram.org/bot{BOT_TOKEN}/getUpdates" | |
| try: | |
| response = requests.get(url) | |
| response.raise_for_status() | |
| data = response.json() | |
| if data["ok"] and data["result"]: | |
| print("π± Recent messages from your bot:") | |
| for update in data["result"][-5:]: # Show last 5 messages | |
| if "message" in update: | |
| chat_id = update["message"]["chat"]["id"] | |
| text = update["message"].get("text", "") | |
| print(f" Chat ID: {chat_id} | Message: {text}") | |
| print(f"\nπ‘ Use this Chat ID: {data['result'][-1]['message']['chat']['id']}") | |
| else: | |
| print("β No messages found. Send a message to your bot first!") | |
| print(f" Bot URL: https://t.me/Dual_Mode_Mood_bot") | |
| except requests.exceptions.RequestException as e: | |
| print(f"β Failed to get chat ID: {e}") | |
| def send_github_action_notification(action_status, workflow_name, commit_message="", actor="", repo=""): | |
| """ | |
| Send a formatted notification about GitHub Action completion | |
| """ | |
| timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S UTC") | |
| # Status emoji | |
| status_emoji = "β " if action_status == "success" else "β" if action_status == "failure" else "π" | |
| # Status text | |
| status_text = { | |
| "success": "SUCCESS", | |
| "failure": "FAILED", | |
| "cancelled": "CANCELLED", | |
| "in_progress": "IN PROGRESS" | |
| }.get(action_status, action_status.upper()) | |
| message = f""" | |
| π <b>GitHub Action Update</b> | |
| {status_emoji} <b>Status:</b> {status_text} | |
| π <b>Workflow:</b> {workflow_name} | |
| β° <b>Time:</b> {timestamp} | |
| π€ <b>Actor:</b> {actor} | |
| π <b>Commit:</b> {commit_message} | |
| π <b>Repository:</b> {repo} | |
| <i>MLOps Case Study - Mood Analysis App</i> | |
| """.strip() | |
| return send_telegram_message(message) | |
| if __name__ == "__main__": | |
| if len(sys.argv) > 1 and sys.argv[1] == "get-chat-id": | |
| print("π Getting your Telegram Chat ID...") | |
| print("Make sure you've sent at least one message to your bot first!") | |
| print(f"Bot URL: https://t.me/Dual_Mode_Mood_bot") | |
| get_chat_id() | |
| else: | |
| print("π€ Telegram Bot Notifier") | |
| print("Usage:") | |
| print(" python telegram_notifier.py get-chat-id # Get your chat ID") | |
| print(" python telegram_notifier.py # Show this help") | |