Add tg_print.py

This commit is contained in:
Clarth 2023-11-14 17:26:34 -05:00
parent 6ca1b074a7
commit 1e22a218cb
1 changed files with 55 additions and 0 deletions

55
tg_print.py Normal file
View File

@ -0,0 +1,55 @@
import sys
import os
import telebot
import subprocess
# Telegram API token
TOKEN = 'TOKEN'
# Telegram chat ID for the channel to send messages to
CHAT_ID = 'CHAT ID' #starts with a -
# Path to directory where downloaded files will be saved
DOWNLOAD_DIR = '/PATH/TO/DOWNLOAD/AND/PRINT/FILES/FROM'
# Printer name
printer = "PRINTER NAME"
# Create a bot instance with the specified token
bot = telebot.TeleBot(TOKEN)
# Send a message to the channel when the bot is online
@bot.message_handler(commands=['start'])
def start(message):
bot.send_message(CHAT_ID, 'Bot is now online!')
# Handle incoming files
@bot.message_handler(content_types=['document', 'audio', 'photo'])
def handle_file(message):
if message.photo:
# Handle photos
bot.send_message(CHAT_ID, 'Printing Images isn\'t supported')
elif message.audio:
# Handle audio
bot.send_message(CHAT_ID, "I can't print sounds!")
else:
# Handle other file types
# Download the file
file_info = bot.get_file(message.document.file_id)
file_name = message.document.file_name
downloaded_file = bot.download_file(file_info.file_path)
# Save the file to the download directory
file_path = os.path.join(DOWNLOAD_DIR, file_name)
with open(file_path, 'wb') as new_file:
new_file.write(downloaded_file)
# Print the file
print_command = ["/usr/bin/lp", "-o", "fit-to-page", "-d", printer, file_path]
subprocess.run(print_command)
# Send a message to the channel indicating that a file has been downloaded and printed
bot.send_message(CHAT_ID, f'File "{file_name}" has been downloaded and printed!')
# Start the bot
bot.polling()