Skip to main content

my-logo

Python Telegram Broadcast

This is a simple Python package that build on top of python-telegram-bot to make broadcasting easier.

Features

  • Broadcast message to multiple users
  • Support media type:
    • Text
    • Photo
    • Document
    • Video
  • Support broadcast strategy:
    • Asynchronous Sequentially
    • Asynchronous Process Pool
    • Asynchronous Multiprocessing Pool

Dependencies

Installation

pip3 install python_telegram_broadcast

Example

Get File ID from Telegram

import asyncio
from python_telegram_broadcast import (
    get_file_id, select_broadcast_method, BroadcastMethodType
)


def wrapper(token, method, target_id, payload):
  caption = ""
  seconds = 0.0 # 0.0 means no timeout
  max_retry = 5 # Maximum retry
  
  loop = asyncio.get_event_loop()
  return loop.run_until_complete(
      get_file_id(
          token, method, target_id, payload, caption, seconds, max_retry
      )
  )
  

if __name__ == "__main__":
  bot_token: str = "TELEGRAM_BOT_TOKEN"   # << Change to your bot token
  user_telegram_id: int = 123456789       # << Change to your telegram id
  file_path: str = ""                     # << Change to your file path or the URL of your file
  
  broadcast_method = select_broadcast_method(BroadcastMethodType.PHOTO)
  
  # If file_path is a URL
  file_id = wrapper(bot_token, broadcast_method, user_telegram_id, file_path)
  
  print(file_id)

Broadcast Photo

from typing import Tuple
from python_telegram_broadcast import (
    handle_broadcast,
    select_broadcast_strategy, BroadcastStrategyType,
    write_sent_result
)

def broadcast_wrapper(token, method, stg, slist, payload):
    loop = asyncio.get_event_loop()
    return loop.run_until_complete(
        handle_broadcast(
            slist, token, method, stg, payload, "...", 
            use_multiproc=False, use_nproc=1, seconds=0.0, max_retry=5
        )
    )

# Use bot_token, broadcast_method, file_path from the previous example!!!
export_path = "./asset"
broadcast_strategy = select_broadcast_strategy(BroadcastStrategyType.ASYNCIO_SEQUENTIAL)
# Read subscriber list from file, but you can also read from database of your choice
subscriber_list: list[Tuple[int, str]] = []
with open("./asset/subscriber.txt", "r") as file:
    header = file.readline()
    while True:
        line = file.readline()
        if not line:
            break
        telegram_id, username = line.strip().split(",")
        subscriber_list.append((int(telegram_id), username))

s, f = broadcast_wrapper(
    bot_token, broadcast_method, broadcast_strategy,
    subscriber_list,
    file_id
)
for S in s:
    print(f"Success: {s}")

for F in f:
    print(f"Failed: {f}")

if len(f) > 0:
    write_sent_result(f"{export_path}/result_{file_path}.txt", f, file_path)

Tests

TODO

Source Code

Metadata

Release files for python-telegram-broadcast 0.9.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-telegram-broadcast 0.9.7
File Size Uploaded
python_telegram_broadcast-0.9.7.tar.gz 12.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-telegram-broadcast 0.9.7
File Interpreter ABI Platform
python_telegram_broadcast-0.9.7-py3-none-any.whl Python 3 none any Details

Total release size: 31.5 kB

Release files / python_telegram_broadcast-0.9.7.tar.gz

Download URL python_telegram_broadcast-0.9.7.tar.gz
Size 12.6 kB
Tags Source
SHA-256 checksum
How to use checksums
9a2fffc3b0a7b88f3801ff1671107a8fba412db5125d30039876353b697a76c9
BLAKE2b-256 checksum
How to use checksums
da52cc8c77102aee737a177ad4ebf739f5557841ed54fd492ccf0427ddfe4106
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.10.6

Release files / python_telegram_broadcast-0.9.7-py3-none-any.whl

Download URL python_telegram_broadcast-0.9.7-py3-none-any.whl
Size 18.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
18157c2b29e5c471ff5928cada50d53331d883d9bafecf661e58a7841e58a568
BLAKE2b-256 checksum
How to use checksums
62cb6a979f7fd2ea21bc6fc7422e6856ab472cf2efd0103b9a334c3a3f3e5485
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.10.6

Release history Release notifications | RSS feed

This release

0.9.7 This release

2 release files

0.9.6

2 release files

0.9.5

2 release files

0.9.4

2 release files

0.9.3

2 release files

0.9.2

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.3

2 release files

0.6.2

2 release files

0.6

2 release files

0.5

2 release files

0.4

2 release files

0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page