AI-powered system that tracks Politics and Crypto narratives impacting PolyMarket prediction markets.
- Real-time Data Ingestion: Continuously fetches news articles and social media posts
- AI-Powered Narrative Clustering: Uses OpenAI embeddings to group related content into coherent narratives
- Market Correlation: Links narratives to active PolyMarket markets with semantic similarity
- Sentiment Analysis: Tracks sentiment trends over time
- Telegram Alerts: Real-time notifications for narrative shifts and market movements
- Interactive Dashboard: Beautiful Next.js dashboard with live data visualization
- Node.js 18+
- PostgreSQL database (or Supabase account)
- OpenAI API key
- News API key
- Telegram Bot Token
- Vercel account (for deployment)
git clone <your-repo>
cd polymarket-narrative-tracker
npm install# Initialize Prisma
npx prisma generate
# Push schema to database
npx prisma db push
# (Optional) Open Prisma Studio
npx prisma studioCreate a .env file in the root:
DATABASE_URL="postgresql://user:password@localhost:5432/polymarket"
OPENAI_API_KEY="sk-..."
NEWS_API_KEY="your-news-api-key"
TELEGRAM_BOT_TOKEN="your-bot-token"
TELEGRAM_CHAT_ID="your-chat-id"
CRON_SECRET="random-secret-string"npm run devVisit http://localhost:3000
- Message @BotFather on Telegram
- Send
/newbotand follow instructions - Copy the bot token to
.env
- Add your bot to a channel/group
- Send a message
- Visit:
https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates - Find
chat.idin the response
npm run telegram:startGET /api/narratives?topic=Politics&limit=10- Get narratives by topicGET /api/markets?category=Crypto- Get markets by categoryGET /api/alerts?limit=20- Get recent alertsGET /api/links?limit=20- Get top narrative-market links
GET /api/cron/fetch-data- Fetch PolyMarket & news dataGET /api/cron/process-narratives- Generate narratives & link to marketsGET /api/cron/send-alerts- Check conditions & send Telegram alerts
Authentication: Include header Authorization: Bearer <CRON_SECRET>
# Install Vercel CLI
npm i -g vercel
# Deploy
vercel
# Set environment variables in Vercel dashboardAdd vercel.json:
{
"crons": [
{
"path": "/api/cron/fetch-data",
"schedule": "0 */4 * * *"
},
{
"path": "/api/cron/process-narratives",
"schedule": "0 */6 * * *"
},
{
"path": "/api/cron/send-alerts",
"schedule": "0 * * * *"
}
]
}- Every 4 hours: Fetch PolyMarket markets & news articles
- Every 6 hours: Generate narrative clusters & link to markets
- Every hour: Detect significant changes & send Telegram alerts
/start- Welcome message and command list/trending- View top trending narratives/crypto- View crypto narratives/politics- View political narratives/market <query>- Search for markets/subscribe- Subscribe to alerts/unsubscribe- Unsubscribe from alerts
curl -X GET http://localhost:3000/api/cron/fetch-data \
-H "Authorization: Bearer your-cron-secret"curl -X GET http://localhost:3000/api/cron/process-narratives \
-H "Authorization: Bearer your-cron-secret"curl -X GET http://localhost:3000/api/cron/send-alerts \
-H "Authorization: Bearer your-cron-secret"polymarket-narrative-tracker/
βββ app/
β βββ api/
β β βββ narratives/route.ts
β β βββ markets/route.ts
β β βββ alerts/route.ts
β β βββ cron/
β β βββ fetch-data/route.ts
β β βββ process-narratives/route.ts
β β βββ send-alerts/route.ts
β βββ page.tsx (Dashboard)
β βββ layout.tsx
βββ lib/
β βββ services/
β β βββ polymarket.service.ts
β β βββ news.service.ts
β β βββ narrative.service.ts
β β βββ linking.service.ts
β β βββ telegram.service.ts
β β βββ alert.service.ts
β βββ utils/
β βββ sentiment.ts
β βββ embeddings.ts
βββ prisma/
β βββ schema.prisma
βββ scripts/
β βββ telegram-bot.ts
βββ .env
βββ package.json
βββ vercel.json
- Never commit
.envfile - Use environment variables for all secrets
- Protect cron endpoints with
CRON_SECRET - Rate limit external API calls
- Implement proper error handling
# Test connection
npx prisma db push
# Reset database (β οΈ deletes all data)
npx prisma migrate reset- Add delays between embedding requests
- Use
batchGetEmbeddingswith proper rate limiting - Consider caching embeddings in database
- Check bot token is correct
- Ensure bot has proper permissions in group/channel
- Verify chat ID is correct
- Check bot is not blocked
- Database: Add indexes for frequently queried fields
- API Calls: Implement caching layer (Redis)
- Embeddings: Store and reuse embeddings
- Background Jobs: Move to dedicated queue (Bull/BullMQ)
- Rate Limiting: Implement per-user rate limits
- Fork the repository
- Create feature branch (
git checkout -b feature/AmazingFeature) - Commit changes (
git commit -m 'Add some AmazingFeature') - Push to branch (
git push origin feature/AmazingFeature) - Open a Pull Request
MIT License - see LICENSE file for details
- PolyMarket for the prediction market data
- OpenAI for embeddings and GPT models
- Telegram for the bot platform
- Vercel for hosting and cron jobs
Built with β€οΈ for the prediction market community