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.
- 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.
- 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.
- Clone the repository:
cd perplexity-telegram-cli
- Install dependencies:
pip install -r requirements.txt # Or just: pip install telethon
- Make the script executable (Linux/macOS):
chmod +x perplexity-telegram-cli.py
If you publish to PyPI:
pip install perplexity-telegram-cli
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).
Edit the script directly or use environment variables to avoid hardcoding credentials:
-
Set
API_ID,API_HASH, andPHONEat 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
.envfile withpython-dotenv.
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"
--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).
- One-Off Query:
python perplexity-telegram-cli.py --message "Explain quantum entanglement simply"
Output: Prints the bot's full response after sending.
- Check Recent Response:
python perplexity-telegram-cli.py --last
Output: "[Date] Bot: Last message text"
- Interactive Session:
python perplexity-telegram-cli.py --interactive
Terminal: "Message (quit to exit): " → Type queries, get responses printed.
- 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.
Contributions are welcome! To get started:
- Fork the repository and clone your fork.
- Create a feature branch:
git checkout -b feature/amazing-feature. - Commit your changes:
git commit -m 'Add amazing feature'. - Push to the branch:
git push origin feature/amazing-feature. - 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.
This project is licensed under the MIT License - see the LICENSE file for details.
- Telethon library for robust Telegram client functionality.
- Telegram API for enabling userbot interactions.
- Inspired by CLI tools for AI integrations like Perplexity.
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.