Skip to content

tehlug/tehlug_bot

Repository files navigation

Tehran Linux User Group Q&A Bot

A Telegram bot for managing questions and answers during Tehran Linux User Group (TehLUG) events. Users can register, select presenters, ask questions, and admins can view and filter questions.

Features

For Users

  • Registration: Users register with their name and email
  • Ask Questions: Select a presenter and submit questions
  • Simple Interface: Easy-to-use keyboard-based navigation

For Admins

  • View All Questions: See all submitted questions
  • Filter by Presenter: View questions for specific presenters
  • Filter by User: Search questions by user name
  • Manage Presenters: Add new presenters to the system
  • Manage Users: View all users, promote users to admin, or demote admins
  • Channel Forwarding: Automatically forward questions to a Telegram channel (optional)

Setup

1. Install Dependencies

pip install -r requirements.txt

Or if using uv:

uv pip install -e .

2. Setup PostgreSQL Database

Install PostgreSQL and create a database:

# Using psql
createdb tehlug_bot

# Or connect to PostgreSQL and run:
# CREATE DATABASE tehlug_bot;

3. Create Your Bot

  1. Open Telegram and search for @BotFather
  2. Send /newbot and follow the instructions
  3. Copy the bot token provided

4. Get Your Telegram ID (for first admin)

  1. Open Telegram and search for @userinfobot
  2. Start a chat with the bot
  3. Copy your Telegram ID

5. (Optional) Setup Channel for Question Forwarding

To automatically forward questions to a Telegram channel:

  1. Create a channel or use an existing one
  2. Add your bot as an admin to the channel
  3. Get the channel ID:
    • For public channels: Use the channel username (e.g., @mychannel)
    • For private channels:
      1. Forward any message from the channel to @JsonDumpBot
      2. Look for "forward_from_chat""id" in the JSON response
      3. Copy the numeric ID (e.g., -1001234567890)

6. Configure Environment Variables

Create a .env file (or set environment variables):

cp .env.example .env

Edit .env and add your values:

BOT_TOKEN=your_bot_token_from_botfather
FIRST_ADMIN_ID=your_telegram_id
DATABASE_URL=postgresql://username:password@localhost:5432/tehlug_bot
QUESTIONS_CHANNEL_ID=-1001234567890  # Optional: only if you want to forward questions to a channel

Required Variables:

  • BOT_TOKEN: Your bot token from BotFather
  • DATABASE_URL: PostgreSQL connection string

Optional Variables:

  • FIRST_ADMIN_ID: Only used to set the initial admin when they register. After that, use the "Manage Users" feature in the Admin Panel to promote other users to admin.
  • QUESTIONS_CHANNEL_ID: Channel ID to forward questions to. Leave empty if you don't want this feature.

7. Run the Bot

python main.py

The database tables will be created automatically on first run.

Usage

For Users

  1. Start the bot with /start
  2. Register with your name and email
  3. Click "Ask Question"
  4. Select a presenter
  5. Type your question

For Admins

  1. Click "Admin Panel" from the main menu
  2. Options available:
    • View All Questions: See all submitted questions
    • Filter by Presenter: Filter questions by presenter name
    • Filter by User: Search questions by user name
    • Add Presenter: Add new presenters (comma-separated for multiple)
    • Manage Users: View all registered users, promote users to admin, or demote admins

Project Structure

tehlug-tlg-bot/
|_ main.py           # Entry point - starts the bot
|_ bot.py            # Bot handlers and conversation flows
|_ database.py       # Database operations (PostgreSQL with SQLAlchemy)
|_ pyproject.toml    # Project dependencies
|_ .env.example      # Environment variable template

Database Schema

Users Table

  • telegram_id (BIGINT, PRIMARY KEY)
  • name (TEXT)
  • email (TEXT)
  • is_admin (BOOLEAN, default: FALSE)
  • registered_at (TIMESTAMP)

Questions Table

  • id (SERIAL, PRIMARY KEY)
  • telegram_id (BIGINT, FOREIGN KEY)
  • user_name (TEXT)
  • presenter_name (TEXT)
  • question (TEXT)
  • created_at (TIMESTAMP)

Presenters Table

  • id (SERIAL, PRIMARY KEY)
  • name (TEXT, UNIQUE)
  • title (TEXT)
  • start_time (TEXT)
  • end_time (TEXT)

Development

The bot uses:

  • python-telegram-bot for Telegram API
  • PostgreSQL for data storage
  • SQLAlchemy (async) with asyncpg driver for database operations
  • ConversationHandler for multi-step interactions

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages