#!/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"""
š GitHub Action Update
{status_emoji} Status: {status_text}
š Workflow: {workflow_name}
ā° Time: {timestamp}
š¤ Actor: {actor}
š Commit: {commit_message}
š Repository: {repo}
MLOps Case Study - Mood Analysis App
""".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")