Skip to main content

AI-powered Telegram Chat Bot and Library using LLMs

Project description

manolo-bot: AI-powered Telegram Chat Bot and Library

Documentation Status

manolo-bot is an AI-powered Telegram Chat Bot and Library built with Python, leveraging modern LLM frameworks and the Model Context Protocol (MCP). It is designed to be both a standalone application and a reusable library for building your own AI-powered bots.

Documentation

Full documentation is available at https://manolo-bot.readthedocs.io/

Installation

From PyPI (Recommended)

You can install manolo-bot directly from PyPI:

pip install manolo-bot

From Source

If you want to run the bot from the source code, you need to install the required packages using uv:

uv sync --no-dev

Configuration

You can copy and rename the provided env.example to .env and edit the file according to your data

You can create a bot on Telegram and get its API token by following the official instructions.

To use the bot in a group, you have to use the @BotFather bot to set the Group Privacy off. This allows the bot to access all group messages.

Required environment variables.

You can use the GOOGLE_API_KEY, OPENAI_API_KEY, OPENAI_API_BASE_URL or OLLAMA_MODEL for selecting the required LLM provider. The OPENAI_API_BASE_URL will look for an OpenAI API like, as the LM Studio API

  • Note: When GOOGLE_API_KEY option is selected, the default model will be Gemini 2.0 Flash.

TELEGRAM_BOT_NAME: Your Telegram bot name

TELEGRAM_BOT_USERNAME: Your Telegram bot username

TELEGRAM_BOT_TOKEN: Your Telegram bot token

Advanced Identity Settings

BOT_UUID: A unique identifier for this bot instance (default: default-bot-uuid). Used to isolate conversation history in storage.

USER_ID: Your Telegram User ID (default: 0). Used for internal tracking and metadata.

Selecting OpenAI Model.

OPENAI_API_MODEL: LLM to use for OpenAI or OpenAI-like API; if not provided, the default model will be used.

Selecting Google API Model.

GOOGLE_API_MODEL: LLM to use for Google API; if not provided, the default model will be used.

Enabling Agent Mode

AGENT_MODE: Enable agent mode (True, False). Default is False. When agent mode is enabled, the bot will use agentic capabilities. This means the bot will use the LLM as a reasoning engine, allowing it to iterate through multiple steps (like searching the internet and analyzing results) to complete complex tasks.

AGENT_INSTRUCTIONS: (Optional) Custom instructions to guide the agent's behavior and reasoning when in agent mode.

Enabling image Generation with Stable Diffusion

WEBUI_SD_API_URL: you can define a Stable Diffusion Web UI API URL for image generation. If this option is enabled the bot will answer image generation requests using Stable Diffusion generated images.

WEBUI_SD_API_PARAMS: A JSON string containing Stable Diffusion Web UI API params. If not provided, default parameters for the SDXL Turbo model will be used.

Setting custom bot character instructions

TELEGRAM_BOT_INSTRUCTIONS_CHARACTER: You can define a custom character for the bot instructions. This will override the default bot character. For example: You are a software engineer, geek and nerd, user of linux and free software technologies.

Setting extra bot instructions

TELEGRAM_BOT_INSTRUCTIONS_EXTRA: You can include extra LLM system instructions using this variable.

Setting custom bot instructions

TELEGRAM_BOT_INSTRUCTIONS: You can define custom LLM system instructions using this variable. This will override the default instructions, and the custom bot character instructions.

Limiting Bot interaction

TELEGRAM_ALLOWED_CHATS: You can use a comma-separated list of allowed chat IDs to limit bot interaction to those chats.

ALLOW_PRIVATE_CHATS: Enable or disable direct bot interaction in private chats (True, False). Default is True.

ADD_NO_ANSWER: If True, the bot will reply with "NO_ANSWER" if it doesn't understand a message or isn't sure if it should respond (True, False). Default is False.

Enable multimodal capabilities

IMAGE_MULTIMODAL: Enable multimodal capabilities for images (True, False). The selected model must support multimodal capabilities.

AUDIO_MULTIMODAL: (Experimental) Enable multimodal capabilities for audio/voice messages (True, False). Currently, this feature only works with the Gemini API.

DOCUMENT_MULTIMODAL: Enable document analysis for PDF, DOCX, and TXT files (True, False). Default is False. When enabled, the bot can extract text from uploaded documents and use it as context. For large documents, it is recommended to use a model with a large context window (like Gemini) and increase CONTEXT_MAX_TOKENS. The document text is stored separately from chat history to keep memory clean.

Enable group assistant

ENABLE_GROUP_ASSISTANT: Enable group assistant for group chats (True, False). The bot will respond to group chats with a question mark. The default value is False.

Document processing configuration

MAX_DOCUMENT_SIZE_BYTES: Maximum size of documents the bot will process (default: 2MB). DOCUMENT_STORAGE_PATH: Directory where extracted document text is stored. Defaults to a system temporary directory.

Enable rate limiting

RATE_LIMITER_REQUESTS_PER_SECOND: The number of requests per second allowed by the bot. RATE_LIMITER_CHECK_EVERY_N_SECONDS: The number of seconds between rate limit checks. RATE_LIMITER_MAX_BUCKET_SIZE: The maximum bucket size for rate limiting.

Set preferred language

PREFERRED_LANGUAGE: The preferred language for the bot. (English, Spanish, etc.)

Set context max tokens

CONTEXT_MAX_TOKENS: The maximum number of tokens allowed for the bot's context.

Web Content Retrieval Configuration

WEB_CONTENT_REQUEST_TIMEOUT_SECONDS: Timeout in seconds for HTTP requests when retrieving web content. Default is 10 seconds.

Simulate typing human behavior

SIMULATE_TYPING: Enable simulating human typing behavior. The default is False. This typing simulation will influence the bot's response time in all chats.

SIMULATE_TYPING_WPM: The words per minute for simulating human typing behavior. Default is 100.

SIMULATE_TYPING_MAX_TIME: The maximum time in seconds for simulating human typing behavior. Default is 10 seconds (we usually don't want to wait too long).

Tools usage

USE_TOOLS: Enable tool usage (True, False). Default is False. When tool usage is enabled, the bot will use the LLM's tools capabilities. When tool usage is disabled, the bot will use the prompt-based pseudo-tools implementation.

Search Configuration

The bot uses DuckDuckGo by default for web searches. You can optionally enable Tavily Search for more advanced search capabilities.

USE_TAVILY_SEARCH: Enable Tavily Search (True, False). Default is False.

TAVILY_SEARCH_KEY: Your Tavily API key. Required if USE_TAVILY_SEARCH is set to True.

Storage Configuration

STORAGE_TYPE: Sets the storage type for conversation context (memory, redis). Default is memory.

REDIS_URL: The Redis URL for storage when STORAGE_TYPE is set to redis. Default is redis://localhost:6379/0.

MCP (Model Context Protocol) Support

manolo_bot supports the Model Context Protocol for connecting to external tool servers.

Enabling MCP

Set the following environment variables:

ENABLE_MCP: Enable MCP support (True, False). Default is False.

MCP_SERVERS_CONFIG: MCP server configuration in JSON format.

MCP Server Configuration

MCP servers are configured via the MCP_SERVERS_CONFIG environment variable, which accepts a JSON object mapping server names to their configurations.

stdio transport example:

{
  "math": {
    "command": "python",
    "args": [
      "/path/to/math_server.py"
    ],
    "transport": "stdio"
  }
}

streamable_http transport example:

{
  "weather": {
    "url": "http://localhost:8000/mcp/",
    "transport": "streamable_http"
  }
}

Multiple servers:

{
  "math": {
    "command": "python",
    "args": [
      "/path/to/math_server.py"
    ],
    "transport": "stdio"
  },
  "weather": {
    "url": "http://localhost:8000/mcp/",
    "transport": "streamable_http"
  }
}

Notes:

  • MCP tools are loaded alongside custom tools defined in ai/tools.py
  • If tool name conflicts occur, MCP tools will override custom tools (a warning is logged)
  • The bot will start successfully even if MCP initialization fails (graceful degradation)
  • MCP is only loaded when both ENABLE_MCP=True and valid MCP_SERVERS_CONFIG are provided

Logging Level

LOGGING_LEVEL: Sets the logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL), defaulting to INFO.

Available Commands

The bot supports the following commands:

  • /flushcontext - Clears the conversation context for the current chat. In group chats, only admins can use this command. The bot will respond with a confirmation message in the configured language.

Running the Bot

You can run the bot using the following command:

uv run manolo-bot

Developers information

Use uv sync --dev to install the development dependencies.

Pre-commit hooks

After installing the development dependencies, to install pre-commit scripts, including ruff checks, you can run the following command:

pre-commit install

Running tests

You can run the tests using the following command:

uv run python -m unittest discover tests

Library Usage

manolo-bot can also be used as a library to build your own AI assistants. It provides a clean abstraction for LLM providers, message storage, and agentic capabilities.

Using LLMAgent (Recommended)

The LLMAgent is the most powerful way to use the library. It allows the bot to use tools and iterate through multiple steps to solve complex queries.

import asyncio
from manolo_bot.ai.llmagent import LLMAgent
from manolo_bot.ai.llmbot import LLMBuilder
from manolo_bot.ai.config import BotConfig, LLMConfig
from manolo_bot.storage.messages.memory import MemoryMessagesStorage
from manolo_bot.ai.tools import get_tools


async def main():
    # 1. Configure LLM (Google, OpenAI, or Ollama)
    llm_config = LLMConfig(google_api_key="your_api_key")
    llm = LLMBuilder(llm_config).get_llm()

    # 2. Define Bot identity
    bot_config = BotConfig(bot_uuid="my-bot", bot_name="Assistant")

    # 3. Setup Storage for a specific conversation
    storage = MemoryMessagesStorage(bot_uuid="my-bot", chat_id=123)
    await storage.refresh_messages()

    # 4. Initialize Agent with tools
    tools = get_tools()
    agent = LLMAgent(
        llm=llm,
        config=bot_config,
        system_instructions="You are a helpful assistant.",
        storage=storage,
        tools=tools
    )

    # 5. Interact
    # The agent can search the web, analyze content, etc.
    response = await agent.answer_message(chat_id=123, message="What is the current price of Bitcoin?")
    print(f"Agent: {response.content}")

    # 6. Persistent changes (if using Redis)
    await storage.commit()


if __name__ == "__main__":
    asyncio.run(main())

Custom Tools

You can provide your own tools when initializing LLMAgent or LLMBot:

from langchain_core.tools import tool
from manolo_bot.ai.llmagent import LLMAgent


@tool
def my_tool(query: str) -> str:
    """Description of my tool."""
    return "Result"


agent = LLMAgent(..., tools=[my_tool])

Using LLMBot (Simple)

For simpler use cases or when using models that don't support tool calling, you can use the basic LLMBot. It provides a direct chat interface without iterative reasoning.

from manolo_bot.ai.llmbot import LLMBot

# ... (same setup as above)

bot = LLMBot(
    llm=llm,
    config=bot_config,
    system_instructions="You are a simple assistant.",
    storage=storage
)

response = await bot.answer_message(chat_id=123, message="Hello!")

For more advanced usage and full API details, please refer to the Full Documentation.

Contributing

If you'd like to contribute to this project, feel free to submit a pull request. We're always open to new ideas or improvements to the code.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

manolo_bot-0.2.0.tar.gz (39.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

manolo_bot-0.2.0-py3-none-any.whl (40.9 kB view details)

Uploaded Python 3

File details

Details for the file manolo_bot-0.2.0.tar.gz.

File metadata

  • Download URL: manolo_bot-0.2.0.tar.gz
  • Upload date:
  • Size: 39.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for manolo_bot-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0437b3e4bbd4188e7c1715b214f46b2c35d87d97eda685cd41280327f8475201
MD5 02fb9dde5a67c1bc5bee931ebe973a2c
BLAKE2b-256 829138e7a3ed715c434eb3e2bc299c8c65492fae00a2189a736fee774a7a3c05

See more details on using hashes here.

File details

Details for the file manolo_bot-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: manolo_bot-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 40.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for manolo_bot-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b092ef93d59a202e9fccfea0f7b402c0b9e21e98086d0eb3d8f167a17e3073a9
MD5 f7475bf0ae28149b44d9be02cfa380b2
BLAKE2b-256 3da0fe26a79b99d34722ec4287db791642c81aef895581830794b82e0176fabb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page