https://github.com/ad/telegram-prediction-market
https://github.com/ad/telegram-prediction-market
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/ad/telegram-prediction-market
- Owner: ad
- License: mit
- Created: 2025-12-04T20:11:44.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2025-12-22T06:51:07.000Z (5 months ago)
- Last Synced: 2025-12-23T18:08:17.437Z (5 months ago)
- Language: Go
- Size: 913 KB
- Stars: 3
- Watchers: 0
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.en.md
- License: LICENSE
Awesome Lists containing this project
README
# ๐ฏ Telegram Prediction Market Bot
**Create your prediction market right in Telegram**
[](https://go.dev/)
[](LICENSE)
[](https://telegram.org/)
English | [ะ ัััะบะธะน](README.md)
[Features](#-features) โข [Quick Start](#-quick-start) โข [Usage](#-usage) โข [Architecture](#-architecture)
---
## ๐ About
Telegram Prediction Market Bot is a full-featured bot for creating prediction markets where users can make forecasts on various events and compete in accuracy. Perfect for teams, communities, and friend groups who want to add a competitive element to their discussions.
### ๐ก Why Use It?
- **For Teams**: Predict sprint outcomes, releases, metrics
- **For Communities**: Create prediction tournaments on any topic
- **For Friends**: Compete in forecasts on sports events, weather, politics
- **For Learning**: Develop critical thinking and probability assessment skills
---
## โจ Features
### ๐ข Multi-Group Architecture
- **Complete data isolation** between groups
- **Deep-link invitations** for easy joining
- **Unlimited participation** โ users can be in multiple groups simultaneously
- **Independent ratings** and achievements in each group
- **๐ Telegram Forums support** โ send events to specific forum topics
### ๐ฒ Flexible Event Types
- **Binary** (Yes/No) โ classic predictions
- **Multiple Choice** (2-6 options) โ for complex scenarios
- **Probabilistic** (ranges 0-25%, 25-50%, 50-75%, 75-100%) โ for confidence calibration
### ๐ฏ Smart Scoring System
```
โ
Correct Prediction:
โข Binary event: +10 points
โข Multiple choice: +15 points
โข Probabilistic: +15 points
๐ Bonuses:
โข Minority (<40% votes): +5 points
โข Early vote (first 12 hours): +3 points
โข Participation: +1 point
โ Penalties:
โข Wrong prediction: -3 points
```
### ๐ Achievement System
- ๐ฏ **Sharpshooter** โ 3 correct predictions in a row
- ๐ฎ **Oracle** โ 10 correct predictions in a row
- ๐ฒ **Risk Taker** โ 3 correct minority predictions in a row
- ๐ **Analyst of the Week** โ most points in a week
- ๐ **Veteran** โ participated in 50 events
### ๐ FSM-based Event Creation
- **Interactive step-by-step process** with validation at each step
- **Automatic message cleanup** for clean chat
- **Persistent sessions** โ continue after bot restart
- **Conflict protection** โ multiple admins can create events simultaneously
### ๐ Smart Notifications
- Reminders 24 hours before deadline
- New event announcements
- Achievement notifications
### ๐ฌ Telegram Forums Support (NEW!)
- **Send events to topics** โ create events in specific forum topics
- **Default topic** โ configure group to automatically send to a specific topic
- **Flexibility** โ each event can be sent to its own topic
- **Backward compatibility** โ regular groups continue to work as before
---
## ๐ Quick Start
### Requirements
- Go 1.25.5+
- SQLite (embedded via modernc.org/sqlite)
- Telegram Bot Token (get from [@BotFather](https://t.me/BotFather))
### Installation
```bash
# Clone the repository
git clone https://github.com/ad/telegram-prediction-market.git
cd telegram-prediction-market
# Install dependencies
go mod download
# Build the bot
go build -o bin/bot ./cmd/bot
```
### Configuration
Create a `.env` file based on `.env.example`:
```bash
# Required parameters
TELEGRAM_TOKEN="your-bot-token-here"
ADMIN_USER_IDS="123456789,987654321"
# Optional parameters
DATABASE_PATH="./data/bot.db"
LOG_LEVEL="INFO"
DEFAULT_GROUP_NAME="Default Group"
MAX_GROUPS_PER_ADMIN="10"
MAX_MEMBERSHIPS_PER_USER="20"
```
### Running
```bash
# Run the bot
./bin/bot
# Or directly via Go
go run ./cmd/bot
```
---
## ๐ Usage
### For Users
```
/start โ Start working with the bot
/help โ Show help
/groups โ List your groups
/rating โ Top 10 participants
/my โ Your statistics
/events โ Active events
```
### For Administrators
#### 1. Create a Group
```
/create_group
```
The bot will guide you through the creation process and provide an invitation link.
#### 2. Invite Participants
Share the deep-link:
```
https://t.me/your_bot?start=group_abc123
```
#### 3. Create an Event
```
/create_event
```
Interactive process:
1. Select group (if you have multiple)
2. Enter question
3. Choose event type
4. Specify options (for multiple choice)
5. Set deadline
6. Confirm
#### 4. Resolve Event
```
/resolve_event
```
Select the correct answer, and the bot will automatically calculate points and update ratings.
### Additional Admin Commands
```
/list_groups โ List all groups with links
/group_members โ Group members
/remove_member โ Remove member
/edit_event โ Edit event (only without votes)
```
---
## ๐ Architecture
### Technology Stack
- **Language**: Go 1.25.5
- **Database**: SQLite with WAL mode
- **Telegram API**: [go-telegram/bot](https://github.com/go-telegram/bot)
- **FSM**: Custom implementation with persistence
- **ID Encoding**: Custom Base-N encoder for short deep-links
### Project Structure
```
.
โโโ cmd/bot/ # Application entry point
โโโ internal/
โ โโโ bot/ # Telegram handlers and FSM
โ โ โโโ handler.go # Main handler
โ โ โโโ event_creation_fsm.go # Event creation FSM
โ โ โโโ event_resolution_fsm.go # Event resolution FSM
โ โ โโโ group_creation_fsm.go # Group creation FSM
โ โ โโโ message_deletion.go # Cleanup utilities
โ โโโ config/ # Configuration management
โ โโโ domain/ # Business logic
โ โ โโโ event_manager.go # Event management
โ โ โโโ rating_calculator.go # Rating calculation
โ โ โโโ achievement_tracker.go # Achievement tracking
โ โ โโโ deeplink_service.go # Deep-link generation
โ โ โโโ group_context_resolver.go # Group context resolution
โ โโโ encoding/ # Base-N encoding for IDs
โ โโโ logger/ # Structured logging
โ โโโ storage/ # Repositories and migrations
โ โโโ fsm_storage.go # FSM persistence
โ โโโ group_repository.go # Group operations
โ โโโ group_membership_repository.go # Membership management
โ โโโ migrations.go # DB migrations
โโโ data/ # SQLite database
```
### Key Implementation Features
#### ๐ FSM with Persistence
All interactive processes (event creation, group creation, event resolution) are implemented through finite state machines with state persistence in DB. This allows:
- Continue process after bot restart
- Avoid conflicts between sessions
- Automatically clean up stale sessions (>30 minutes)
#### ๐ Data Isolation
Each group is a completely isolated space:
- Events visible only to group members
- Ratings maintained separately
- Achievements earned independently
- Users can have different positions in different groups
#### ๐ Smart Rating Calculation
The system considers:
- Event complexity (type)
- Choice popularity (minority bonus)
- Reaction speed (early vote bonus)
- History of correct predictions (streaks)
#### ๐ Short Deep-links
Uses custom Base-N encoding to create short and readable invitation links instead of long numeric IDs.
---
## ๐งช Testing
```bash
# Run all tests
go test ./...
# With verbose output
go test ./... -v
# Only specific package
go test ./internal/bot -v
# With coverage
go test ./... -cover
```
The project includes:
- Unit tests for all components
- Integration tests for FSM
- Property-based tests (gopter) for encoding
- Tests for multi-group scenarios
---
## ๐ค Contributing
We welcome contributions! Here's how you can help:
1. ๐ **Report bugs** via Issues
2. ๐ก **Suggest new features** via Discussions
3. ๐ง **Submit Pull Requests**
4. ๐ **Improve documentation**
5. โญ **Star** the project
---
## ๐ License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
---
## ๐ Acknowledgments
- [go-telegram/bot](https://github.com/go-telegram/bot) โ excellent library for Telegram Bot API
- [modernc.org/sqlite](https://gitlab.com/cznic/sqlite) โ pure Go SQLite driver
- Go community for amazing tools and libraries
---
**Made with โค๏ธ for communities who love predicting the future**
[โฌ Back to Top](#-telegram-prediction-market-bot)