A simple integration of Telegram Bot API with Cloudflare Workers. This project allows you to proxy requests to Telegram's API via a Cloudflare Worker and send messages or files (such as documents, photos, etc.) using a Python script.
- Features
- Installation
- Cloudflare Worker Setup
- Usage
- Project Structure
- Dependencies
- License
- Acknowledgements
- Worker Script: A Cloudflare Worker script to proxy requests to Telegram's Bot API.
- Python Script: A Python class to send messages and files via the Worker proxy to Telegram chats.
git clone https://github.com/hamidrezafarzin/Telegram-Worker-Bot.git
cd Telegram-Worker-Bot
Create and activate a virtual environment (optional but recommended):
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
Install dependencies:
pip install -r requirements.txt
Deploy your Cloudflare Worker using the script in worker/worker.js
.
Make sure your Worker is configured to proxy requests to the Telegram Bot API.
-
Create a Cloudflare Worker:
- Log in to your Cloudflare dashboard.
- Select your domain, and navigate to "Workers" in the sidebar.
- Click "Create a Worker" and give it a name.
- Replace the default Worker script with the content from
worker/worker.js
in this repository.
-
Update the Worker script:
- Make sure that the Worker script proxies requests to Telegram's Bot API by adding the following code to handle the Telegram requests correctly.
-
Deploy the Worker:
- Click "Save and Deploy" in the Cloudflare dashboard.
- Once deployed, your Worker will have a unique URL (e.g.,
https://your-worker-id.your-domain.workers.dev
).
-
Update your Python script:
- In
src/telegram_bot.py
, set theworker_url
to the URL of your deployed Cloudflare Worker.
- In
You can use the send_message()
method from the Python script to send a text message to a chat.
from telegram_bot import TelegramBot
bot = TelegramBot(
worker_url="https://your-worker-id.your-domain.workers.dev",
bot_token="123456789:ABCDEF123456789ghijklmnopqrst",
)
response = bot.send_message("Hello, world!", chat_id="987654321")
print(response)
You can send a document from your local file system or a URL using the send_document()
method.
# Send a document from a local file
response = bot.send_document(file_path="path_to_file.pdf", chat_id="987654321")
print(response)
# Send a document from a URL
response = bot.send_document(file_url="https://example.com/path_to_file.pdf", chat_id="987654321")
print(response)
Send a photo using the send_photo()
method:
# Send a photo from a local file
response = bot.send_photo(photo_path="path_to_image.jpg", chat_id="987654321")
print(response)
# Send a photo from a URL
response = bot.send_photo(photo_url="https://example.com/path_to_image.jpg", chat_id="987654321")
print(response)
TelegramWorkerBot/
│
├── worker/
│ └── worker.js # Cloudflare Worker script for proxying requests
│
├── src/
│ └── telegram_bot.py # Python script to send messages and files via the worker
│
├── README.md # This documentation file
├── requirements.txt # Python dependencies
└── .gitignore # Ignoring unnecessary files
requests
: Used to handle HTTP requests. Install viapip install -r requirements.txt
.
This project is licensed under the MIT License - see the LICENSE file for details.
Thanks to Armin darabi for the helpful hints and guidance!