25 lines
722 B
Python
25 lines
722 B
Python
from __future__ import annotations
|
|
import os
|
|
import urllib.parse
|
|
import urllib.request
|
|
|
|
|
|
def send_telegram(text: str) -> bool:
|
|
token = os.getenv('TELEGRAM_BOT_TOKEN', '').strip()
|
|
chat_id = os.getenv('TELEGRAM_CHAT_ID', '').strip()
|
|
if not token or not chat_id:
|
|
return False
|
|
|
|
url = f"https://api.telegram.org/bot{token}/sendMessage"
|
|
data = urllib.parse.urlencode({
|
|
'chat_id': chat_id,
|
|
'text': text,
|
|
'disable_web_page_preview': 'true'
|
|
}).encode()
|
|
req = urllib.request.Request(url, data=data, method='POST')
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=12) as resp:
|
|
return 200 <= resp.status < 300
|
|
except Exception:
|
|
return False
|