A Telegram bot built on Cloudflare Workers for automated financial analysis and portfolio management. Uses artificial intelligence (Anthropic Claude) to generate investment reports and interpret trading commands, integrating real-time market data from Financial Modeling Prep (FMP).
The project is developed in Node.js and deployed on the Cloudflare ecosystem:
- Cloudflare Workers: Serverless handling of Telegram webhook requests.
- Durable Objects: Persistent state management (SQLite), background queues (Alarms), and user data consistency.
- External Integrations:
- Telegram Bot API: Main user interface.
- Anthropic API (Claude 3.5 Sonnet): Financial analysis engine and natural language parsing.
- Financial Modeling Prep (FMP) API: Data source for real-time quotes, price history, and company profiles.
The /analyze [TICKER] command initiates a multi-step analysis process:
- The Worker delegates the task to the Durable Object via the Alarm system to handle long-running executions.
- The AI collects market data (price, changes, market cap, P/E) and historical data (30-day OHLC) via Tool Use.
- A structured report is generated including:
- Fundamental analysis (Business model, financial health).
- Technical analysis (Trend, support/resistance levels).
- Weighted score (Technical 35%, Fundamentals 30%, Sector 20%, Sentiment 15%).
- Operational recommendation (BUY/PASS/WATCHLIST) with execution strategy and risk management.
The system tracks positions and calculates P&L in real-time.
- Order Entry: Supports structured commands (
/buy AAPL 150 10) and natural language (I bought 10 Apple shares at 150 dollars). The AI performs parsing if pattern matching fails. - Portfolio Tracker: The
/portfoliocommand returns a table with open positions, calculating profit/loss based on current market prices retrieved from FMP. - Budgeting: The
/setbudgetcommand sets available capital, influencing the AI's position sizing suggestions.
- Authentication: Optional support for access password (
BOT_PASSWORD) to restrict bot usage. - Rate Limiting: SQLite-based system within the Durable Object to limit daily AI analyses per user.
The project requires the following environment variables (configurable in wrangler.toml or Cloudflare dashboard):
| Variable | Description |
|---|---|
TELEGRAM_BOT_TOKEN |
Token obtained from @BotFather |
ANTHROPIC_API_KEY |
API key for accessing Claude models |
FMP_API_KEY |
Financial Modeling Prep API key |
BOT_PASSWORD |
(Optional) Password to protect bot access |
- Node.js
- Cloudflare account
npm installDeployment to Cloudflare Workers is done via Wrangler:
npx wrangler deploysrc/index.js: Main router and webhook handling.src/user-storage.js: Durable Object implementation. Manages SQLite, background queues, and persistent business logic.src/ai.js: Interface with Anthropic API. Handles prompt engineering, response streaming, and tool execution.src/fmp.js: HTTP client for Financial Modeling Prep APIs.src/telegram.js: Utilities for message formatting and Telegram API calls.