https://github.com/rezataheri01/telegram-store-bot
A Telegram bot that serve as a digital store
https://github.com/rezataheri01/telegram-store-bot
digital-store django orm polling python python-telegram-bot telegram-bot
Last synced: 4 months ago
JSON representation
A Telegram bot that serve as a digital store
- Host: GitHub
- URL: https://github.com/rezataheri01/telegram-store-bot
- Owner: RezaTaheri01
- License: gpl-3.0
- Created: 2024-12-15T22:15:35.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-02T13:50:47.000Z (over 1 year ago)
- Last Synced: 2025-03-28T23:43:25.352Z (about 1 year ago)
- Topics: digital-store, django, orm, polling, python, python-telegram-bot, telegram-bot
- Language: Python
- Homepage: https://t.me/gameStorePersiaBot
- Size: 668 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Telegram Bot with Django Integration π’π»
This bot is a Python-based Telegram bot seamlessly integrated with a Django backend. It supports database management, payment handling, digital product selling, and dynamic user interactions.
---
## Features π’
### Telegram Bot π²
- **Local Timezone**:
- Use **/timezone_set** to change your timezone.
- **Multi-language Support**:
- Currently supports three languages, with the ability to add more.(There is a help comment in bot_settings.py)
- Users can change the language via the main menu.
- **User Account Management**:
- Automatically creates user accounts if they donβt exist.
- Retrieves and displays user balance. π³
- Displays user transaction history. π
- **Interactive Menu**:
- Provides options like **My Account**, **My Balance**, **Deposit**, and **Product Categories**.
- Includes inline keyboards for seamless navigation. π
- **Payment Handling**:
- Generates unique payment links. π‘
- Processes payments via the Django backend.
- Updates user balance upon successful payment. βοΈ
- **Product Management**:
- Dynamically displays categories and products. π
- Supports product purchases via balance deduction. πΈ
- **Customization**:
- In `bot_settings.py`, you can customize:
- Number of categories and products displayed per row.
- All text messages. ποΈ
- Primary and secondary languages.
- Inline button text and callback data.
- Payment link time limits. β³
### Django Backend π
- **Database Models**:
- `UserData`: Manages user account information. By default, the language is set to English (`en`). If your primary language isnβt English, update this in `bot_settings.py`.
- `Transaction`: Tracks payment transactions. π
- `Category`, `Product`, `ProductDetail`: Manages products and their details. π οΈ
- `ProductDetail`: The field detail that contain product info is encrypted π by ([django-encrypted-json-fields](https://pypi.org/project/django-encrypted-json-fields/))
---
## Installation βοΈ
### Prerequisites π
1. Python 3.8 or higher πΎ
2. Django (latest version recommended) β¬οΈ
3. PostgreSQL or any preferred database system configured in Django ([Learn More](https://docs.djangoproject.com/en/5.1/ref/databases/))
4. Required libraries: `python-telegram-bot`, `asgiref`, `python-decouple`, etc.
5. Dependencies are listed in `req.txt`
### Setup Instructions π§
1. **Clone the Repository**:
```bash
git clone https://github.com/RezaTaheri01/telegram-store-bot.git
```
```bash
cd telegram-store-bot/telegram_store
```
2. **Install Dependencies**([virtual environment](https://realpython.com/python-virtual-environments-a-primer/#create-it) recommended):
```bash
pip install -r req.txt
```
3. **Configure the `.env` File**:
Create a `.env` file in the project root and populate it with the following sample values:
```env
TOKEN=your-telegram-api-token
SECRET_KEY=django-insecure-$kp!7e*2sv#%i%=qq(-#pspemkli#ruf_5i04(2q+eeoae_+2h
# Encrypting detail field with this key
ENCRYPTION_KEYS=6-QgONW6TUl5rt4Xq8u-wBwPcb15sIYS2CN6d69zueM=
DEBUG=True
ALLOWED_HOSTS=localhost,127.0.0.1,your-domain.com
PAYMENT_DOMAIN=http://127.0.0.1:8000
```
4. **Migrate the Database** and **Create Super User**:
```bash
python manage.py makemigrations users payment products
```
```bash
python manage.py migrate
```
```bash
python manage.py createsuperuser
```
5. **Run the Django Development Server**:
```bash
python manage.py runserver
```
6. **Run the Telegram Bot**:
```bash
python bot.py
```
---
## Code Overview π οΈ
### Telegram Bot π²
#### Key Features
- **Core Functions**:
- `start_menu`: Displays the main menu. π
- `change_language`: Allows users to change their language and updates the `UserData` language field. π
- `check_create_account`: Automatically creates user accounts if they donβt exist. π§
- `user_balance`: Fetches and displays the userβs balance. π³
- `deposit_money`: Initiates the deposit process and generates payment links. π΅
- `charge_account`: Updates the userβs balance upon successful payment. βοΈ
- `get_name`: Retrieves names based on user language. π
- `product_categories`: Dynamically displays product categories. π
- `products`: Lists products under a selected category. πΎ
- `product_payment_detail`: Displays payment details for a product. πΈ
- `payment`: Handles product purchases and balance deductions. π°
- `get_user_language`: Retrieves the userβs language from a language cache or the database. π
#### Conversation States
- `ENTER_AMOUNT`: Captures the deposit amount entered by the user. π΅
#### Error Handling π οΈ
- Logs all errors to `bot_logs.log`.
- Notifies users of issues without disrupting the bot experience. β οΈ
---
### Payment Link Workflow π
- **Link Format**:
```
PAYMENT_URL + /payment/confirm/?chat_id={chat_id}&user_id={user_id}&amount={amount}&bot_link={bot_link}&transaction={transaction}
```
- Redirects users to a Django view for payment processing.
---
## Usage π
1. **Start the Bot**:
- Send `/start` to the bot. π’
- Explore options like **My Account**, **My Balance**, **Deposit**, or **Product Categories**.
2. **Deposit Money**:
- Select the **Deposit** option from the menu. π΅
- Enter the desired amount.
- Click the payment link to complete the transaction. βοΈ
- Verify the updated balance in your account. π³
3. **Browse Products**:
- Select **Product Categories** from the menu. π
- Choose a category and view available products. πΎ
- Purchase a product using your balance. π°
---
## Logs π
- All errors are logged in `bot_logs.log` with detailed messages. π
---
## Notes π
- Ensure the Django server is running for smooth payment processing. βοΈ
- Update the `PAYMENT_URL`, `ALLOWED_HOSTS` in the .env to match your serverβs address when deploying. π
---
## License π
This project is licensed under the MIT License. See the [`LICENSE`](https://github.com/RezaTaheri01/telegram-store-bot/blob/main/LICENSE) file for more details. π