Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
409 changes: 405 additions & 4 deletions main.py

Large diffs are not rendered by default.

192 changes: 192 additions & 0 deletions plugins/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
# Telegram LLM Bot - Plugin System

**Author:** Marco Baturan

## Overview

This directory contains the plugin system for the Telegram LLM Bot. Plugins are modular extensions that enhance the bot's functionality by adding specialized capabilities such as content summarization, multimodal input validation, and web scraping.

## Architecture

The plugin system is designed with the following principles:

### Dynamic Loading
- Plugins are automatically discovered and loaded from subdirectories at startup
- Each plugin must be in its own directory with a `main.py` file
- Plugins can be enabled/disabled via configuration without code changes

### Plugin Interface
Each plugin must implement two functions in `main.py`:

1. **`is_plugin_applicable(messages, provider)`**
- Determines if the plugin should process the current message
- Receives the message history and the active AI provider
- Returns `True` if the plugin should handle this message, `False` otherwise

2. **`process_messages(messages, provider)`**
- Modifies the message content before sending to the LLM
- Can replace user input with processed content (e.g., transcripts, summaries)
- Returns the modified messages list

### Provider Awareness
Plugins receive the active AI provider (e.g., "openai", "anthropic", "gemini") and can:
- Check if the provider supports required capabilities (vision, audio, video)
- Block unsupported content with helpful error messages
- Adapt behavior based on provider features

## Available Plugins

### Content Processing

#### 📺 `summarize_youtube_video`
- **Purpose:** Automatically summarizes YouTube videos
- **Trigger:** Detects YouTube URLs in messages
- **Process:** Fetches transcript and generates executive summary
- **Requirements:** `youtube-transcript-api`
- **Compatibility:** All text-based providers

#### 🌐 `web_reader`
- **Purpose:** Summarizes web page content
- **Trigger:** Detects HTTP/HTTPS URLs (excluding YouTube)
- **Process:** Scrapes page text and generates summary
- **Requirements:** `requests`, `beautifulsoup4`
- **Compatibility:** All text-based providers

### Multimodal Input Validation

#### 🎬 `watch_video`
- **Purpose:** Validates video upload compatibility
- **Trigger:** User uploads a video file
- **Process:** Checks if provider supports native video analysis
- **Supported Providers:** Gemini, OpenAI (GPT-4o)
- **Action:** Blocks video if provider doesn't support it

#### 🖼️ `watch_picture`
- **Purpose:** Validates image upload compatibility
- **Trigger:** User uploads an image
- **Process:** Checks if provider supports vision
- **Supported Providers:** Gemini, OpenAI, Anthropic
- **Action:** Blocks image if provider doesn't support it

#### 🎵 `listen_audio`
- **Purpose:** Validates audio upload compatibility
- **Trigger:** User uploads audio or voice note
- **Process:** Checks if provider supports native audio
- **Supported Providers:** Gemini, OpenAI (GPT-4o)
- **Action:** Blocks audio if provider doesn't support it

### Content Generation

#### 🎨 `generate_picture`
- **Purpose:** Validates image generation requests
- **Trigger:** Keywords like "generate image", "draw", "dibuja"
- **Process:** Checks if provider supports image generation
- **Supported Providers:** Gemini, OpenAI
- **Action:** Blocks request if provider doesn't support generation

## Plugin Management

### Configuration File
`config_plugins.py` controls which plugins are active:

```python
PLUGIN_STATUS = {
"summarize_youtube_video": True,
"web_reader": True,
"watch_video": True,
"watch_picture": True,
"listen_audio": True,
"generate_picture": True,
}
```

### Telegram Commands

Manage plugins directly from Telegram:

- **`/plugins`** - Show status of all plugins
- **`/enable_plugin <name>`** - Enable a specific plugin
- Example: `/enable_plugin summarize_youtube_video`
- **`/disable_plugin <name>`** - Disable a specific plugin
- Example: `/disable_plugin web_reader`
- **`/enable_all_plugins`** - Enable all plugins
- **`/disable_all_plugins`** - Disable all plugins (useful for cost savings)

### Cost Management
Disable expensive plugins during low-budget periods:
```
/disable_plugin summarize_youtube_video
/disable_plugin web_reader
```

## Creating a New Plugin

### Directory Structure
```
plugins/
├── your_plugin_name/
│ ├── main.py # Required: Plugin logic
│ ├── requirements.txt # Required: Dependencies
│ ├── license.md # Required: License (Public Domain)
│ └── README.md # Required: Documentation
```

### Template (`main.py`)
```python
def is_plugin_applicable(messages, provider):
"""
Check if this plugin should process the message.

Args:
messages: List of message dicts with 'role' and 'content'
provider: Active AI provider (e.g., 'openai', 'anthropic')

Returns:
bool: True if plugin should handle this message
"""
if not messages:
return False

last_message = messages[-1]
if last_message.get("role") != "user":
return False

# Your detection logic here
return False

def process_messages(messages, provider):
"""
Process and modify the messages.

Args:
messages: List of message dicts
provider: Active AI provider

Returns:
list: Modified messages
"""
# Your processing logic here
return messages
```

### Registration
1. Add plugin name to `config_plugins.py` `PLUGIN_STATUS`
2. Set to `True` to enable by default
3. Plugin will be auto-loaded on next bot restart

## Plugin Execution Order

Plugins are processed in directory listing order. The **first** plugin that returns `True` from `is_plugin_applicable()` will process the message. Subsequent plugins are skipped.

## Licensing

All plugins in this directory are released into the **Public Domain** under the Unlicense. See individual `license.md` files for details.

## Dependencies

Install all plugin dependencies:
```bash
pip install -r requirements.txt
```

Individual plugin requirements are listed in their respective `requirements.txt` files.
57 changes: 57 additions & 0 deletions plugins/config_plugins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
Plugin Configuration Manager

This file controls which plugins are active.
Set a plugin to False to deactivate it and save on API costs.
"""

# Plugin activation status
PLUGIN_STATUS = {
"summarize_youtube_video": True,
"web_reader": True,
"watch_video": True,
"watch_picture": True,
"listen_audio": True,
"generate_picture": True,
"reaction_tracker": True, # Tracks message reactions for learning from user feedback
}

def is_plugin_enabled(plugin_name):
"""
Check if a plugin is enabled.

Args:
plugin_name: Name of the plugin (e.g., 'summarize_youtube_video')

Returns:
bool: True if enabled, False otherwise
"""
return PLUGIN_STATUS.get(plugin_name, False)

def enable_plugin(plugin_name):
"""Enable a specific plugin."""
if plugin_name in PLUGIN_STATUS:
PLUGIN_STATUS[plugin_name] = True
return True
return False

def disable_plugin(plugin_name):
"""Disable a specific plugin."""
if plugin_name in PLUGIN_STATUS:
PLUGIN_STATUS[plugin_name] = False
return True
return False

def enable_all_plugins():
"""Enable all plugins."""
for plugin_name in PLUGIN_STATUS:
PLUGIN_STATUS[plugin_name] = True

def disable_all_plugins():
"""Disable all plugins."""
for plugin_name in PLUGIN_STATUS:
PLUGIN_STATUS[plugin_name] = False

def get_plugin_status():
"""Get the current status of all plugins."""
return PLUGIN_STATUS.copy()
21 changes: 21 additions & 0 deletions plugins/generate_picture/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generate Picture Plugin

## Description
This plugin detects user requests to generate images (e.g., "create an image of...", "draw a cat"). It ensures that such requests are only processed by AI providers that support image generation.

## How it Works
1. **Detection:** The plugin scans user messages for keywords indicating an intent to generate images (e.g., "generate image", "create picture", "draw", "dibuja", "crea una imagen").
2. **Provider Check:** It checks the currently active AI provider.
- **Supported:** Gemini (via Imagen), OpenAI (via DALL-E).
- **Unsupported:** Anthropic (Claude 3.5), Grok, Llama 3.2 (Text-only or Vision-input only).
3. **Action:**
- If supported, the request is passed to the model.
- If unsupported, the request is blocked with a warning.

## Requirements
- None.

## Compatibility
- **Gemini:** Supported.
- **OpenAI:** Supported.
- **Others:** Not supported (Request will be blocked).
24 changes: 24 additions & 0 deletions plugins/generate_picture/license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <https://unlicense.org>
Loading