-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
84 lines (69 loc) · 2.79 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from fastapi import FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
import logging
from ibkr.ibkr_client import IBKRClient
from telegram.telegram_client import TelegramClient
from util.models import TradeRequest, FetchPortfolioRequest
from util.helpers import (
format_telegram_trade_notification,
format_telegram_error_notification,
)
from config import TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID
logging.basicConfig(level=logging.INFO)
ibkr_client = IBKRClient()
telegram_client = TelegramClient(TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID)
def lifespan(app: FastAPI):
yield
ibkr_client.disconnect()
app = FastAPI(lifespan=lifespan)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def health_check():
try:
return {"status": "Backend server is healthy and running :)"}
except Exception as e:
logging.error(f"Failed to perform health check: {e}")
raise HTTPException(status_code=500, detail="Failed to perform health check")
@app.post("/webhook/fetch-portfolio")
async def fetch_portfolio(request: FetchPortfolioRequest):
if request.key != ibkr_client.secret_key:
logging.warning("Unauthorized webhook request for fetch-portfolio.")
raise HTTPException(status_code=403, detail="Unauthorized")
try:
portfolio = ibkr_client.fetch_portfolio()
telegram_client.send_message(portfolio)
return {"status": "Portfolio sent to Telegram"}
except Exception as e:
logging.error(f"Failed to fetch portfolio: {e}")
error_message = format_telegram_error_notification(
"/webhook/fetch-portfolio", str(e)
)
telegram_client.send_message(error_message)
raise HTTPException(status_code=500, detail="Failed to fetch portfolio")
@app.post("/webhook/sell-market-order")
async def sell_market_order(trade_request: TradeRequest):
if trade_request.key != ibkr_client.secret_key:
raise HTTPException(status_code=403, detail="Unauthorized")
try:
response = ibkr_client.place_market_order(
trade_request.symbol, trade_request.quantity
)
notification_message = format_telegram_trade_notification(
trade_request.symbol,
trade_request.quantity,
response["status"],
)
telegram_client.send_message(notification_message)
return {"status": "Order placed", "details": response}
except Exception as e:
logging.error(f"Failed to place order: {e}")
error_message = format_telegram_error_notification(
"/webhook/sell-market-order", str(e)
)
telegram_client.send_message(error_message)
raise HTTPException(status_code=500, detail="Failed to place order")