![my-logo](https://jonahtzuchi.github.io/python-telegram-broadcast/logo-mini.jpg "Python-Telegram-Broadcast-Logo")
# Python Telegram Broadcast
This is a simple Python package that build on top of [python-telegram-bot](https://pypi.org/project/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
- [python-telegram-bot](https://pypi.org/project/python-telegram-bot/)=21.1.1
- [mypy](https://pypi.org/project/mypy/)
## Installation
```bash
pip3 install python_telegram_broadcast
```
## Example
### Get File ID from Telegram
```python
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
```python
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
- https://github.com/JonahTzuChi/python-telegram-broadcast
Raw data
{
"_id": null,
"home_page": null,
"name": "python-telegram-broadcast",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "python, telegram, broadcast, bot",
"author": "jonah_whaler_2348",
"author_email": "jk_saga@proton.me",
"download_url": "https://files.pythonhosted.org/packages/da/52/cc8c77102aee737a177ad4ebf739f5557841ed54fd492ccf0427ddfe4106/python_telegram_broadcast-0.9.7.tar.gz",
"platform": null,
"description": "![my-logo](https://jonahtzuchi.github.io/python-telegram-broadcast/logo-mini.jpg \"Python-Telegram-Broadcast-Logo\")\r\n\r\n# Python Telegram Broadcast\r\n\r\nThis is a simple Python package that build on top of [python-telegram-bot](https://pypi.org/project/python-telegram-bot/) to make broadcasting easier.\r\n\r\n\r\n## Features\r\n- Broadcast message to multiple users\r\n- Support media type:\r\n - Text\r\n - Photo\r\n - Document\r\n - Video\r\n- Support broadcast strategy:\r\n - Asynchronous Sequentially\r\n - Asynchronous Process Pool\r\n - Asynchronous Multiprocessing Pool\r\n\r\n## Dependencies\r\n- [python-telegram-bot](https://pypi.org/project/python-telegram-bot/)=21.1.1\r\n- [mypy](https://pypi.org/project/mypy/)\r\n\r\n## Installation\r\n ```bash\r\n pip3 install python_telegram_broadcast\r\n ```\r\n\r\n## Example\r\n\r\n### Get File ID from Telegram\r\n```python\r\nimport asyncio\r\nfrom python_telegram_broadcast import (\r\n get_file_id, select_broadcast_method, BroadcastMethodType\r\n)\r\n\r\n\r\ndef wrapper(token, method, target_id, payload):\r\n caption = \"\"\r\n seconds = 0.0 # 0.0 means no timeout\r\n max_retry = 5 # Maximum retry\r\n \r\n loop = asyncio.get_event_loop()\r\n return loop.run_until_complete(\r\n get_file_id(\r\n token, method, target_id, payload, caption, seconds, max_retry\r\n )\r\n )\r\n \r\n\r\nif __name__ == \"__main__\":\r\n bot_token: str = \"TELEGRAM_BOT_TOKEN\" # << Change to your bot token\r\n user_telegram_id: int = 123456789 # << Change to your telegram id\r\n file_path: str = \"\" # << Change to your file path or the URL of your file\r\n \r\n broadcast_method = select_broadcast_method(BroadcastMethodType.PHOTO)\r\n \r\n # If file_path is a URL\r\n file_id = wrapper(bot_token, broadcast_method, user_telegram_id, file_path)\r\n \r\n print(file_id)\r\n```\r\n \r\n\r\n### Broadcast Photo\r\n```python\r\nfrom typing import Tuple\r\nfrom python_telegram_broadcast import (\r\n handle_broadcast,\r\n select_broadcast_strategy, BroadcastStrategyType,\r\n write_sent_result\r\n)\r\n\r\ndef broadcast_wrapper(token, method, stg, slist, payload):\r\n loop = asyncio.get_event_loop()\r\n return loop.run_until_complete(\r\n handle_broadcast(\r\n slist, token, method, stg, payload, \"...\", \r\n use_multiproc=False, use_nproc=1, seconds=0.0, max_retry=5\r\n )\r\n )\r\n\r\n# Use bot_token, broadcast_method, file_path from the previous example!!!\r\nexport_path = \"./asset\"\r\nbroadcast_strategy = select_broadcast_strategy(BroadcastStrategyType.ASYNCIO_SEQUENTIAL)\r\n# Read subscriber list from file, but you can also read from database of your choice\r\nsubscriber_list: list[Tuple[int, str]] = []\r\nwith open(\"./asset/subscriber.txt\", \"r\") as file:\r\n header = file.readline()\r\n while True:\r\n line = file.readline()\r\n if not line:\r\n break\r\n telegram_id, username = line.strip().split(\",\")\r\n subscriber_list.append((int(telegram_id), username))\r\n\r\ns, f = broadcast_wrapper(\r\n bot_token, broadcast_method, broadcast_strategy,\r\n subscriber_list,\r\n file_id\r\n)\r\nfor S in s:\r\n print(f\"Success: {s}\")\r\n\r\nfor F in f:\r\n print(f\"Failed: {f}\")\r\n\r\nif len(f) > 0:\r\n write_sent_result(f\"{export_path}/result_{file_path}.txt\", f, file_path)\r\n\r\n```\r\n\r\n## Tests\r\nTODO\r\n\r\n## Source Code\r\n- https://github.com/JonahTzuChi/python-telegram-broadcast\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Package that wraps the python-telegram-bot library to make broadcasting easier.",
"version": "0.9.7",
"project_urls": null,
"split_keywords": [
"python",
" telegram",
" broadcast",
" bot"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "62cb6a979f7fd2ea21bc6fc7422e6856ab472cf2efd0103b9a334c3a3f3e5485",
"md5": "0bc1b39f947f827bf7ffdc76fdab9c7d",
"sha256": "18157c2b29e5c471ff5928cada50d53331d883d9bafecf661e58a7841e58a568"
},
"downloads": -1,
"filename": "python_telegram_broadcast-0.9.7-py3-none-any.whl",
"has_sig": false,
"md5_digest": "0bc1b39f947f827bf7ffdc76fdab9c7d",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 18870,
"upload_time": "2024-05-11T14:27:46",
"upload_time_iso_8601": "2024-05-11T14:27:46.483877Z",
"url": "https://files.pythonhosted.org/packages/62/cb/6a979f7fd2ea21bc6fc7422e6856ab472cf2efd0103b9a334c3a3f3e5485/python_telegram_broadcast-0.9.7-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "da52cc8c77102aee737a177ad4ebf739f5557841ed54fd492ccf0427ddfe4106",
"md5": "ea46249d0366d44a7fc453b457dca2cc",
"sha256": "9a2fffc3b0a7b88f3801ff1671107a8fba412db5125d30039876353b697a76c9"
},
"downloads": -1,
"filename": "python_telegram_broadcast-0.9.7.tar.gz",
"has_sig": false,
"md5_digest": "ea46249d0366d44a7fc453b457dca2cc",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 12645,
"upload_time": "2024-05-11T14:27:47",
"upload_time_iso_8601": "2024-05-11T14:27:47.802792Z",
"url": "https://files.pythonhosted.org/packages/da/52/cc8c77102aee737a177ad4ebf739f5557841ed54fd492ccf0427ddfe4106/python_telegram_broadcast-0.9.7.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-11 14:27:47",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "python-telegram-broadcast"
}