import httpx
import logging
import sys
import os
from mcp_app import mcp
# Setup Logging
logging.basicConfig(
stream=sys.stderr, level=logging.INFO, format="[LOG] %(message)s", force=True
)
@mcp.tool()
async def send_telegram_message(message: str) -> str:
"""
Send a notification to the configured Telegram Chat.
Args:
message: The text to send to your phone.
"""
token = os.getenv("TELEGRAM_BOT_TOKEN")
chat_id = os.getenv("TELEGRAM_CHAT_ID")
if not token or not chat_id:
return "Error: Missing TELEGRAM_BOT_TOKEN or TELEGRAM_CHAT_ID in .env file."
url = f"https://api.telegram.org/bot{token}/sendMessage"
payload = {"chat_id": chat_id, "text": message, "parse_mode": "Markdown"}
logging.info(f"Sending Telegram message to {chat_id}...")
async with httpx.AsyncClient() as client:
try:
response = await client.post(url, json=payload)
if response.status_code == 200:
return "Message sent successfully!"
else:
return f"Telegram Error: {response.status_code} - {response.text}"
except Exception as e:
return f"Connection Error: {str(e)}"