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"

message = "Hello World"  

try:
    result = send_message(message, bot_token, chat_id)
    print("Message sent successfully!")
    print("Message ID:", result['result']['message_id'])
except Exception as e:
    print(f"Error while sending message: {e}")

Listen For Messages

from teleconnector import listen_for_messages

bot_token = "YOUR_BOT_TOKEN"
chat_id = "TARGET_CHAT_ID"

try:
    print("Listening for new messages... (Press CTRL+C to stop)")
    for message in listen_for_messages(bot_token, chat_id):
        from_user = message.get('from', {})
        name = from_user.get('first_name', 'Unknown')
        text = message.get('text', '')
        print(f"New message from {name}: {text}")
except KeyboardInterrupt:
    print("\nListener stopped by user.")
except Exception as e:
    print(f"Error while listening for messages: {e}")

3. Sending File

from teleconnector import send_file

bot_token = "YOUR_BOT_TOKEN"
chat_id = "TARGET_CHAT_ID"
file_path = "/path/to/your/file.pdf"
caption = "Here's the document"  

try:
    result = send_file(file_path, bot_token, chat_id, caption=caption)
    print("File sent successfully!")
    print("File ID:", result['result']['document']['file_id'])
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"
chat_id = "TARGET_CHAT_ID"
save_directory = "./downloads" 

try:
    print("📡 Listening for incoming files... (Press CTRL+C to stop)")
    for file_info in listen_for_files(bot_token, chat_id, save_dir=save_directory):
        file_name = file_info.get('file_name', 'Unknown')
        file_size = file_info.get('file_size', 0)
        saved_path = file_info.get('saved_path', 'Not saved')
        print(f"Received file: {file_name} ({file_size} bytes)")
        print(f"Saved to: {saved_path}")
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 send_message, TelegramAPIError

bot_token = "YOUR_BOT_TOKEN"
chat_id = "TARGET_CHAT_ID"

try:
    response = send_message("Test message", bot_token, chat_id)
    print("Success:", response)
except TelegramAPIError as e:
    print("Telegram API Error:", e)
except ValueError as e:
    print("Invalid parameters:", e)
except Exception as e:
    print("Unexpected error:", e)

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub. 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.1.3.tar.gz (4.8 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.1.3-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for teleconnector-1.1.3.tar.gz
Algorithm Hash digest
SHA256 0877bc2c6d4c1ada09c5f2428762b5d41d63ff932db8c9630837a47f952415c2
MD5 a3297524f521910b305315814eb6b36a
BLAKE2b-256 dbe8ab8415dc33fd88fbd7a3cca7e6dd4eb810b9286ae37a8ab8e66b53e0ebc9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: teleconnector-1.1.3-py3-none-any.whl
  • Upload date:
  • Size: 6.0 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.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 34b298964cd07fba8144e207a898de2b4acb1484e984db1190fbb0b0e3b27eba
MD5 cbee793848345bbeb656f631a7ab8359
BLAKE2b-256 ac9698a6c2d904f88e673b9392242cc90cfe6e3b8e186f7309bc953adbe23992

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