Now prints image files.

Added image handling.
Can't print inline images, but will remind the user to send the image as a file.
This commit is contained in:
Clarth 2024-01-02 17:20:51 -05:00
parent 4ca6b82761
commit 3cd1137b84
1 changed files with 63 additions and 55 deletions

View File

@ -1,55 +1,63 @@
import sys import sys
import os import os
import telebot import telebot
import subprocess import subprocess
from pathlib import Path
# Telegram API token
TOKEN = 'TOKEN' # Telegram API token
TOKEN = 'TOKEN'
# Telegram chat ID for the channel to send messages to
CHAT_ID = 'CHAT ID' #starts with a - # 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' # Path to directory where downloaded files will be saved
DOWNLOAD_DIR = '/PATH/TO/DOWNLOAD/AND/PRINT/FILES/FROM'
# Printer name
printer = "PRINTER NAME" # Printer name
printer = "PRINTER NAME"
# Create a bot instance with the specified token
bot = telebot.TeleBot(TOKEN) # 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']) # Send a message to the channel when the bot is online
def start(message): @bot.message_handler(commands=['start'])
bot.send_message(CHAT_ID, 'Bot is now online!') def start(message):
bot.send_message(CHAT_ID, 'Bot is now online!')
# Handle incoming files
@bot.message_handler(content_types=['document', 'audio', 'photo']) # Handle incoming files
def handle_file(message): @bot.message_handler(content_types=['document', 'audio', 'photo'])
if message.photo: def handle_file(message):
# Handle photos try:
bot.send_message(CHAT_ID, 'Printing Images isn\'t supported') if message.photo:
elif message.audio: # Handle photos
# Handle audio file_info = bot.get_file(message.photo[-1].file_id)
bot.send_message(CHAT_ID, "I can't print sounds!") file_name = f"photo_{message.photo[-1].file_id}{Path(file_info.file_path).suffix}"
else: bot.send_message(CHAT_ID, 'Printing Images isn\'t supported. Please send the image as a file. Like this --> https://i.imgur.com/JcTtCvR.png')
# Handle other file types elif message.audio:
# Download the file # Handle audio
file_info = bot.get_file(message.document.file_id) bot.send_message(CHAT_ID, "I can't print sounds!")
file_name = message.document.file_name elif message.document:
downloaded_file = bot.download_file(file_info.file_path) # Handle other file types
# Download the file
# Save the file to the download directory file_info = bot.get_file(message.document.file_id)
file_path = os.path.join(DOWNLOAD_DIR, file_name) file_name = message.document.file_name
with open(file_path, 'wb') as new_file: downloaded_file = bot.download_file(file_info.file_path)
new_file.write(downloaded_file)
# Save the file to the download directory
# Print the file file_path = os.path.join(DOWNLOAD_DIR, file_name)
print_command = ["/usr/bin/lp", "-o", "fit-to-page", "-d", printer, file_path] with open(file_path, 'wb') as new_file:
subprocess.run(print_command) new_file.write(downloaded_file)
# Send a message to the channel indicating that a file has been downloaded and printed # Print the file
bot.send_message(CHAT_ID, f'File "{file_name}" has been downloaded and printed!') print_command = ["/usr/bin/lp", "-o", "fit-to-page", "-d", printer, file_path]
subprocess.run(print_command)
# Start the bot
bot.polling() # 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!')
except Exception as e:
# Handle any other exceptions
print(f"Error: {e}")
bot.send_message(CHAT_ID, 'An error occurred while processing the file. Please try again.')
# Start the bot
bot.polling()