import os
from datetime import datetime
from dotenv import load_dotenv
import requests
import yfinance as yf
.env 파일 로드
load_dotenv()
TELEGRAM_TOKEN = os.getenv(“TELEGRAM_TOKEN”)
CHAT_ID = os.getenv(“CHAT_ID”)
def send_telegram_message(text):
if not TELEGRAM_TOKEN or not CHAT_ID:
print(
“❌ 오류: .env 파일에 TELEGRAM_TOKEN 또는 CHAT_ID가 설정되지 않았습니다.”
)
return
url = f"https://api.telegram.org/bot{TELEGRAM_TOKEN}/sendMessage"
payload = {"chat_id": CHAT_ID, "text": text, "parse_mode": "Markdown"}
try:
response = requests.post(url, json=payload)
if response.status_code == 200:
print("✅ 텔레그램 메시지 전송 성공!")
else:
print(
f"❌ 텔레그램 전송 실패 ({response.status_code}): {response.text}"
)
except Exception as e:
print(f"❌ 텔레그램 전송 중 오류 발생: {e}")
def get_us_market_briefing():
indices = {
“S&P 500”: “^GSPC”,
“나스닥”: “^IXIC”,
“다우 존스”: “^DJI”,
“러셀 2000”: “^RUT”,
}
top_stocks = {
"엔비디아 (NVDA)": "NVDA",
"애플 (AAPL)": "AAPL",
"마이크로소프트 (MSFT)": "MSFT",
"알파벳 (GOOGL)": "GOOGL",
"아마존 (AMZN)": "AMZN",
"테슬라 (TSLA)": "TSLA",
}
today_str = datetime.now().strftime("%Y-%m-%d")
msg = [
"📈 **미국 증시 마감 시황 브리핑**",
f"📅 기준일: {today_str}\n",
"🏛️ **주요 지수 동향**",
]
for name, ticker in indices.items():
data = yf.Ticker(ticker).history(period="2d")
if len(data) >= 2:
prev_close = data["Close"].iloc[-2]
curr_close = data["Close"].iloc[-1]
change = curr_close - prev_close
pct_change = (change / prev_close) * 100
symbol = "🔺" if change > 0 else "🔻" if change < 0 else "➖"
msg.append(
f"• {name}: `{curr_close:,.2f}` ({symbol} `{change:+,.2f}`, `{pct_change:+.2f}%`)"
)
msg.append("\n💻 **주요 종목 동향**")
for name, ticker in top_stocks.items():
data = yf.Ticker(ticker).history(period="2d")
if len(data) >= 2:
prev_close = data["Close"].iloc[-2]
curr_close = data["Close"].iloc[-1]
change = curr_close - prev_close
pct_change = (change / prev_close) * 100
symbol = "🔺" if change > 0 else "🔻" if change < 0 else "➖"
msg.append(
f"• {name}: `${curr_close:,.2f}` ({symbol} `${change:+,.2f}`, `{pct_change:+.2f}%`)"
)
full_message = "\n".join(msg)
send_telegram_message(full_message)
if name == “main“:
get_us_market_briefing()
Leave a Reply