Skip to main content

Advanced Telegram Bot API Connector

Project description

TeleConnector

Python Version License PyPI Version

A robust Python package for seamless Telegram bot interactions with advanced file handling capabilities.

Features

  • Send messages and files to Telegram chats
  • Listen for incoming messages and files in real-time
  • Automatic file saving with custom paths via captions
  • Comprehensive error handling and validation
  • Configurable timeouts for all operations
  • Supports both individual chats and groups

Installation

pip install teleconnector

Quick Start

1. Sending Messages

from teleconnector import send_message

bot_token = "YOUR_BOT_TOKEN"
chat_id = "TARGET_CHAT_ID"

try:
    result = send_message("Hello World!", bot_token, chat_id)
    if result:
        print("Message sent successfully!")
    else:
        print("Failed to send message!")
except Exception as e:
    print(f"Error while sending: {e}")

Listen For Messages

from teleconnector import listen_for_messages

bot_token = "YOUR_BOT_TOKEN"
chat_id = "YOUR_GROUP_CHAT_ID"

try:
    print("📡 Listening for new messages... (Press CTRL+C to stop)")
    for message in listen_for_messages(bot_token, chat_id):
        if "text" in message:
            print(f"💬 New message: {message['text']}")
        else:
            print("⚠️ Received a message without text content:", message)
except KeyboardInterrupt:
    print("\n🛑 Listener stopped by user.")
except Exception as e:
    print(f"❌ Error while listening: {e}")

3. Sending File

from teleconnector import send_file

bot_token = "YOUR_BOT_TOKEN"
chat_id = "TARGET_CHAT_ID"

file_path = "/path/to/file.txt"

try:
    result = send_file(file_path, bot_token, chat_id)
    if result:
        print(f"File sent successfully: {file_path}")
    else:
        print(f"Failed to send file: {file_path}")
except Exception as e:
    print(f"Error while sending file: {e}")

4. Receiving and Auto-Saving Files

from teleconnector import listen_for_files

bot_token = "YOUR_BOT_TOKEN"

try:
    print("📡 Listening for incoming files... (Press CTRL+C to stop)")

    for file_info in listen_for_files(bot_token, save_dir=True):
        if 'saved_path' in file_info:
            print(f"File automatically saved to: {file_info['saved_path']}")
        if 'file_name' in file_info:
            print(f"Received file: {file_info['file_name']}")
        else:
            print(f"Received file data without a name: {file_info}")

except KeyboardInterrupt:
    print("\nListener stopped by user.")
except Exception as e:
    print(f"Error while listening for files: {e}")

Error Handling

The package raises specific exceptions you can catch:

from teleconnector import TelegramAPIError

try:
    send_message("Test", bot_token, chat_id)
except TelegramAPIError as e:
    print(f"Telegram error: {e}")
except ValueError as e:
    print(f"Invalid parameters: {e}")
except Exception as e:
    print(f"Unexpected error: {e}")

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

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

teleconnector-1.0.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

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

teleconnector-1.0.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file teleconnector-1.0.0.tar.gz.

File metadata

  • Download URL: teleconnector-1.0.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for teleconnector-1.0.0.tar.gz
Algorithm Hash digest
SHA256 4afbded8e37adad8d372b7edb085b9779f66de7797ae17bd36937c62c56366ee
MD5 1ea7a5f69d54fdba067af285618757d2
BLAKE2b-256 7d0eb3eab60496d341106f1b97f390a9e69da9b5e0ccbabd52763c1df9c3643d

See more details on using hashes here.

File details

Details for the file teleconnector-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: teleconnector-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for teleconnector-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 edc81eb2f8cbd010a8103e25dae16450dba656dbab4446b13efd9e365d37d573
MD5 ddfb122c9b37d6e09ff37e1c00c1168d
BLAKE2b-256 77e02d7cd4bee97b9c0c188704e762197fc931e478839a7115c8bef74923384c

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