import os
import requests

def send_message(chat_id: int, text: str):
    token = os.environ.get("TELEGRAM_BOT_TOKEN")
    if not token:
        print("Error: TELEGRAM_BOT_TOKEN is not set.")
        return
    
    url = f"https://api.telegram.org/bot{token}/sendMessage"
    payload = {
        "chat_id": chat_id,
        "text": text,
        "parse_mode": "Markdown"
    }
    
    try:
        response = requests.post(url, json=payload, timeout=10)
        response.raise_for_status()
    except Exception as e:
        print(f"Failed to send message to Telegram: {e}")

def set_webhook(webhook_url: str):
    token = os.environ.get("TELEGRAM_BOT_TOKEN")
    if not token:
        print("Error: TELEGRAM_BOT_TOKEN is not set.")
        return False
    
    url = f"https://api.telegram.org/bot{token}/setWebhook"
    payload = {
        "url": webhook_url
    }
    
    try:
        response = requests.post(url, json=payload, timeout=10)
        response.raise_for_status()
        print(f"Webhook set successfully to {webhook_url}")
        return True
    except Exception as e:
        print(f"Failed to set webhook: {e}")
        return False
