|
- from telegram.ext import Updater, MessageHandler, Filters, CallbackContext, CommandHandler
- from telegram import Update, InputMediaPhoto
- import json
- import sys
- import io
- import logging
- from PIL import Image
- import random
-
- submissions = {}
- names = {}
- status = {}
-
- def ensure_chat(func):
- def wrapper(update, *args, **kwargs):
- chat_id = update.effective_chat.id
- if chat_id not in submissions:
- submissions[chat_id] = {}
- names[chat_id] = {}
- status[chat_id] = {}
- print("Added chat: ", chat_id)
- return func(update, *args, **kwargs)
- return wrapper
-
- def ensure_user(func):
- def wrapper(update, *args, **kwargs):
- chat_id = update.effective_chat.id
- user_id = update.effective_user.id
- if user_id not in submissions[chat_id]:
- names[chat_id][user_id] = update.effective_user.first_name
- print("Added user: ", user_id, " - ", update.effective_user.first_name)
- return func(update, *args, **kwargs)
- return wrapper
-
- def update_name(func):
- def wrapper(update, *args, **kwargs):
- chat_id = update.effective_chat.id
- user_id = update.effective_user.id
- if user_id not in submissions[update.effective_chat.id]:
- names[chat_id][user_id] = update.effective_user.first_name
- print("Got name for ", user_id, ": ", update.effective_user.first_name)
- return func(update, *args, **kwargs)
- return wrapper
-
- def ensure_open(func):
- def wrapper(update, *args, **kwargs):
- chat_id = update.effective_chat.id
- if status[chat_id] == "open":
- return func(update, *args, **kwargs)
- return wrapper
-
- @ensure_chat
- @ensure_user
- @update_name
- @ensure_open
- def accept_photo(update: Update, context: CallbackContext):
- chat_id = update.effective_chat.id
- user_id = update.effective_user.id
- file_id = update.message.photo[-1].file_id
- submissions[chat_id][user_id] = file_id
- print(file_id)
-
- @ensure_chat
- @ensure_user
- def echo(update: Update, context: CallbackContext):
- context.bot.send_message(chat_id=update.effective_chat.id, text="Hi.")
- updater.stop()
-
- @ensure_chat
- def open_submissions(update: Update, context: CallbackContext):
- chat_id = update.effective_chat.id
- context.bot.send_message(chat_id=update.effective_chat.id, text="Send me your images pls.")
- status[chat_id] = "open"
-
- @ensure_chat
- def show_submissions(update: Update, context: CallbackContext):
- chat_id = update.effective_chat.id
- options = list(map(lambda file_id: InputMediaPhoto(file_id), submissions[chat_id].values()))
- chosen = random.choices(options, k=min(len(options), 10))
- context.bot.send_media_group(chat_id=chat_id, media=[chosen[0]])
-
-
- if __name__ == "__main__":
- try:
- config = json.load(open("config.json", "r", encoding="utf-8"))
- except:
- logging.error("Couldn't read the config!")
- sys.exit(1)
-
- updater = Updater(token=config["token"], use_context=True)
- dispatcher = updater.dispatcher
-
- dispatcher.add_handler(MessageHandler(Filters.photo, accept_photo))
- dispatcher.add_handler(CommandHandler("open", open_submissions))
- dispatcher.add_handler(CommandHandler("show", show_submissions))
-
- updater.start_polling()
- updater.idle()
|