Created
June 14, 2024 19:39
-
-
Save ioxorg/6aee042710e348ea08f00d0d5ddbf5f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import requests | |
import logging | |
import time | |
# Replace with your own values | |
MP3_FOLDER_PATH = '' | |
TELEGRAM_TOKEN = '' | |
TELEGRAM_CHANNEL_ID = '' | |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s') | |
def send_files_to_telegram_channel(mp3_folder_path, telegram_token, telegram_channel_id): | |
url = f"https://api.telegram.org/bot{telegram_token}/sendAudio" | |
for root, dirs, files in os.walk(mp3_folder_path): | |
files = sorted(files) | |
for file in files: | |
if file.endswith('.mp3'): | |
file_path = os.path.join(root, file) | |
with open(file_path, 'rb') as f: | |
title = os.path.splitext(file)[0] | |
data = {'chat_id': telegram_channel_id, 'title': title} | |
files = {'audio': (file, f, 'audio/mpeg')} | |
response = requests.post(url, data=data, files=files) | |
logging.info(f'Sent {file_path}. Status code: {response.status_code}') | |
if response.status_code != 200: | |
logging.error(f'Failed to send {file_path}. Response: {response.text}') | |
time.sleep(3) # Bypass ratelimit | |
def main(): | |
send_files_to_telegram_channel(MP3_FOLDER_PATH, TELEGRAM_TOKEN, TELEGRAM_CHANNEL_ID) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment