Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

Perplexity-Telegram-CLI

License: MIT Python Version

A Python-based command-line interface (CLI) tool that uses Telethon to interact with Telegram as a user client (not a bot), relaying messages to a target Telegram bot (e.g., 'askplexbot' for Perplexity AI queries). It allows sending queries from your terminal, fetching responses, and interactive sessions. Requires Telegram API credentials from my.telegram.org for user authentication.

Features

  • User Client Mode: Uses Telethon for full user account access (phone login, sessions), supporting 2FA and message history.
  • Bot Relaying: Sends messages to a target bot (default: 'askplexbot') and waits for/prints responses, including edits (e.g., "thinking" to final answer).
  • CLI Options: Send one-off messages, fetch last bot response, or enter interactive mode for ongoing queries.
  • Session Management: Stores session files securely for reuse without repeated logins.
  • Lightweight: Minimal dependencies (Telethon), runs on Linux, macOS, and Windows with Python 3.7+.
  • Verbose Logging: Optional verbosity for debugging login or message flows.

Prerequisites

  • Python 3.7 or higher installed.
  • Telethon library: pip install telethon.
  • Telegram API credentials:
    • App ID and API Hash from my.telegram.org.
    • Your phone number for login (supports 2FA).
  • A target bot username (e.g., 'askplexbot' for Perplexity integration).
  • Basic terminal access; no root required.

Installation

From Source (Recommended)

  1. Clone the repository:
cd perplexity-telegram-cli
  1. Install dependencies:

pip install -r requirements.txt # Or just: pip install telethon

  1. Make the script executable (Linux/macOS):

chmod +x perplexity-telegram-cli.py

Using pip (If Packaged)

If you publish to PyPI:

pip install perplexity-telegram-cli

Session Setup

On first run, the script creates a user_session.session file after login. It will prompt for your phone number and Telegram verification code (or 2FA password).

Configuration

Edit the script directly or use environment variables to avoid hardcoding credentials:

  • Set API_ID, API_HASH, and PHONE at the top of the script, or override via env:

    No config file needed—credentials are loaded from the script or env for simplicity. For production, consider a .env file with python-dotenv.

Usage

Run the script with Python 3:

📌 Basic usage: Send a message to the target bot and print response python perplexity-telegram-cli.py --message "What is the capital of France?"

📌 Fetch the last message from the bot python perplexity-telegram-cli.py --last

📌 Interactive mode: Chat with the bot via terminal python perplexity-telegram-cli.py --interactive

📌 Specify a different target (overrides default) python perplexity-telegram-cli.py --target anotherbot --message "Hello"

📌 Verbose output for debugging python perplexity-telegram-cli.py -v --message "Test query"

Command-Line Arguments

  • --message, -m TEXT: The query/message to send to the target bot.
  • --last, -l: Fetch and print the most recent message from the target.
  • --target BOT_USERNAME: Target bot username (default: 'askplexbot').
  • --interactive: Enter an interactive loop for multiple messages (type 'quit' to exit).
  • --phone PHONE: Override phone number for login (default from script/env).
  • --verbose, -v: Enable verbose logging (shows args and debug info).

The script handles login automatically on first run, creates a session file for future use, and waits for bot responses (including "thinking" edits up to 45 seconds).

Examples

  1. One-Off Query:

python perplexity-telegram-cli.py --message "Explain quantum entanglement simply" Output: Prints the bot's full response after sending.

  1. Check Recent Response:

python perplexity-telegram-cli.py --last Output: "[Date] Bot: Last message text"

  1. Interactive Session:

python perplexity-telegram-cli.py --interactive Terminal: "Message (quit to exit): " → Type queries, get responses printed.

  1. Custom Target:

python perplexity-telegram-cli.py --target myai_bot --message "Daily news summary"

For Perplexity integration, ensure 'askplexbot' is set up to relay to Perplexity AI—responses will include AI-generated answers with sources.

Contributing

Contributions are welcome! To get started:

  1. Fork the repository and clone your fork.
  2. Create a feature branch: git checkout -b feature/amazing-feature.
  3. Commit your changes: git commit -m 'Add amazing feature'.
  4. Push to the branch: git push origin feature/amazing-feature.
  5. Open a Pull Request on the original repo.

Please ensure code follows PEP 8 style guidelines and includes tests if applicable. For major changes, open an issue first to discuss.

License

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

Acknowledgments

  • Telethon library for robust Telegram client functionality.
  • Telegram API for enabling userbot interactions.
  • Inspired by CLI tools for AI integrations like Perplexity.

Support

If you encounter issues:

  • Check the Issues page for similar problems.
  • For login errors, verify your API credentials at my.telegram.org.
  • Ensure Telethon is up-to-date: pip install --upgrade telethon.

Report bugs with steps to reproduce, your Python/Telethon version, and error logs.


Built with Python and Telethon for seamless Telegram-AI workflows.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages