all repos — python-meme-bot @ 9c164c36cd723d573c0fc185ee2db9771701d8dc

Telegram Bot that uses PIL to compute light image processing.

main.py (view raw)

 1from PIL import Image
 2from Api import get_random_image
 3from Effects import tt_bt
 4
 5from dotenv import load_dotenv
 6load_dotenv()
 7import os, logging
 8logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
 9from io import BytesIO
10
11from telegram.ext import Updater, CallbackContext, CommandHandler, MessageHandler, Filters
12from telegram import Update
13
14sauce_str = "[Sauce 🍝]({})"
15
16def _ttbt_general(text):
17    image, url = get_random_image()
18    image = tt_bt(text, image)
19    bio = BytesIO()
20    bio.name = 'image.jpeg'
21    image.save(bio, 'JPEG')
22    bio.seek(0)
23    return bio, sauce_str.format(url)
24
25def _get_reply(input, fallback=""):
26    if input is None:
27        return fallback
28    return input.text
29
30def _get_image():
31    image, url = get_random_image()
32    bio = BytesIO()
33    bio.name = 'image.jpeg'
34    image.save(bio, 'JPEG')
35    bio.seek(0)
36    return bio, sauce_str.format(url)
37
38def start(update: Update, context: CallbackContext):
39    context.bot.send_message(chat_id=update.effective_chat.id, text="Welcome to PILuAnimeBot!")
40
41def pic(update: Update, context: CallbackContext):
42    image, url = _get_image()
43    
44    context.bot.send_photo(chat_id=update.effective_chat.id, photo=image, caption=url, parse_mode="markdown")
45
46def tt(update: Update, context: CallbackContext):
47    reply = _get_reply(update.message.reply_to_message)
48    content = ' '.join(context.args)
49    input_text = f"{reply} {content}".replace("\n", " ")
50    
51    image, url = _ttbt_general(input_text)
52    context.bot.send_photo(update.effective_chat.id, photo=image, caption=url, parse_mode="markdown")
53
54def bt(update: Update, context: CallbackContext):
55    reply = _get_reply(update.message.reply_to_message)
56    content = ' '.join(context.args)
57    input_text = f"{reply} {content}".replace("\n", " ")
58    
59    image, url =_ttbt_general(" \n" + input_text)
60    context.bot.send_photo(update.effective_chat.id, photo=image, caption=url, parse_mode="markdown")
61
62def ttbt(update: Update, context: CallbackContext):
63    reply = _get_reply(update.message.reply_to_message)
64    content = ' '.join(context.args)
65    input_text = f"{reply}\n{content}"
66    
67    image, url =_ttbt_general(input_text)
68    context.bot.send_photo(update.effective_chat.id, photo=image, caption=url, parse_mode="markdown")
69    
70def caps(update: Update, context: CallbackContext):
71    reply = _get_reply(update.message.reply_to_message, ' '.join(context.args))
72    context.bot.send_message(chat_id=update.effective_chat.id, text=reply.upper())
73    
74def unknown(update: Update, context: CallbackContext):
75    context.bot.send_message(chat_id=update.effective_chat.id, text="Sorry, I didn't understand that command.")
76
77def main():
78    
79    updater = Updater(token=os.getenv("token"))
80    dispatcher = updater.dispatcher
81    
82    dispatcher.add_handler(CommandHandler('start', start))
83    dispatcher.add_handler(CommandHandler('caps', caps))
84    dispatcher.add_handler(CommandHandler('pic', pic))
85    dispatcher.add_handler(CommandHandler('ttbt', ttbt))
86    dispatcher.add_handler(CommandHandler('tt', tt))
87    dispatcher.add_handler(CommandHandler('bt', bt))
88    
89    dispatcher.add_handler(MessageHandler(Filters.command, unknown))
90    
91    updater.start_polling()
92    updater.idle()
93
94if __name__ ==  "__main__":
95    main()