Skip to content

Instantly share code, notes, and snippets.

@Cyanoxygen
Last active March 27, 2020 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cyanoxygen/f64c388b875800d25509057891f31f9d to your computer and use it in GitHub Desktop.
Save Cyanoxygen/f64c388b875800d25509057891f31f9d to your computer and use it in GitHub Desktop.
Simple ImageMagick Bot
#!/usr/bin/env python3
# Credit: leedagee
# License: GPLv2 (https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt)
from telegram.ext import Updater, CommandHandler
from telegram import InputTextMessageContent, UserProfilePhotos
import subprocess, io
def handleConvert(bot, updater):
if updater.message.reply_to_message.photo != []:
orig = updater.message.reply_to_message.photo[-1].get_file().download_as_bytearray()
print('normal image')
elif updater.message.reply_to_message.sticker != None:
if updater.message.reply_to_message.sticker.is_animated:
updater.message.reply_text("Animated stickers are NOT supported.")
print('animated sticker')
return
orig = updater.message.reply_to_message.sticker.get_file().download_as_bytearray()
print('sticker')
elif updater.message.reply_to_message.photo == []:
# trying to get user's profile photo
try:
photo = updater.message.reply_to_message.from_user.get_profile_photos().photos
print(f'Length of the list is {len(photo)}')
orig = photo[0][-1].get_file().download_as_bytearray()
except Exception as e:
print(e)
updater.message.reply_text('This user does not have a profile photo, or it is hidden by the user')
orig = None
if orig == None:
# updater.message.reply_text('Reply to an image or sticker')
return
opts = updater.message.text_markdown.split()[1:]
print(opts)
p = subprocess.Popen(['convert', '-'] + opts + ['-'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate(input=orig)
if p.returncode != 0:
updater.message.reply_text("Error: \n%s"%err.decode('utf-8'))
else:
updater.message.reply_photo(io.BytesIO(out))
def initBot():
token = open('token', 'r').read().strip()
updater = Updater(token)
updater.dispatcher.add_handler(CommandHandler(['convert'], handleConvert))
updater.start_polling()
updater.idle()
initBot()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment