Lets you submit and vote on images
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

98 lines
3.2 KiB

  1. from telegram.ext import Updater, MessageHandler, Filters, CallbackContext, CommandHandler
  2. from telegram import Update, InputMediaPhoto
  3. import json
  4. import sys
  5. import io
  6. import logging
  7. from PIL import Image
  8. import random
  9. submissions = {}
  10. names = {}
  11. status = {}
  12. def ensure_chat(func):
  13. def wrapper(update, *args, **kwargs):
  14. chat_id = update.effective_chat.id
  15. if chat_id not in submissions:
  16. submissions[chat_id] = {}
  17. names[chat_id] = {}
  18. status[chat_id] = {}
  19. print("Added chat: ", chat_id)
  20. return func(update, *args, **kwargs)
  21. return wrapper
  22. def ensure_user(func):
  23. def wrapper(update, *args, **kwargs):
  24. chat_id = update.effective_chat.id
  25. user_id = update.effective_user.id
  26. if user_id not in submissions[chat_id]:
  27. names[chat_id][user_id] = update.effective_user.first_name
  28. print("Added user: ", user_id, " - ", update.effective_user.first_name)
  29. return func(update, *args, **kwargs)
  30. return wrapper
  31. def update_name(func):
  32. def wrapper(update, *args, **kwargs):
  33. chat_id = update.effective_chat.id
  34. user_id = update.effective_user.id
  35. if user_id not in submissions[update.effective_chat.id]:
  36. names[chat_id][user_id] = update.effective_user.first_name
  37. print("Got name for ", user_id, ": ", update.effective_user.first_name)
  38. return func(update, *args, **kwargs)
  39. return wrapper
  40. def ensure_open(func):
  41. def wrapper(update, *args, **kwargs):
  42. chat_id = update.effective_chat.id
  43. if status[chat_id] == "open":
  44. return func(update, *args, **kwargs)
  45. return wrapper
  46. @ensure_chat
  47. @ensure_user
  48. @update_name
  49. @ensure_open
  50. def accept_photo(update: Update, context: CallbackContext):
  51. chat_id = update.effective_chat.id
  52. user_id = update.effective_user.id
  53. file_id = update.message.photo[-1].file_id
  54. submissions[chat_id][user_id] = file_id
  55. print(file_id)
  56. @ensure_chat
  57. @ensure_user
  58. def echo(update: Update, context: CallbackContext):
  59. context.bot.send_message(chat_id=update.effective_chat.id, text="Hi.")
  60. updater.stop()
  61. @ensure_chat
  62. def open_submissions(update: Update, context: CallbackContext):
  63. chat_id = update.effective_chat.id
  64. context.bot.send_message(chat_id=update.effective_chat.id, text="Send me your images pls.")
  65. status[chat_id] = "open"
  66. @ensure_chat
  67. def show_submissions(update: Update, context: CallbackContext):
  68. chat_id = update.effective_chat.id
  69. options = list(map(lambda file_id: InputMediaPhoto(file_id), submissions[chat_id].values()))
  70. chosen = random.choices(options, k=min(len(options), 10))
  71. context.bot.send_media_group(chat_id=chat_id, media=[chosen[0]])
  72. if __name__ == "__main__":
  73. try:
  74. config = json.load(open("config.json", "r", encoding="utf-8"))
  75. except:
  76. logging.error("Couldn't read the config!")
  77. sys.exit(1)
  78. updater = Updater(token=config["token"], use_context=True)
  79. dispatcher = updater.dispatcher
  80. dispatcher.add_handler(MessageHandler(Filters.photo, accept_photo))
  81. dispatcher.add_handler(CommandHandler("open", open_submissions))
  82. dispatcher.add_handler(CommandHandler("show", show_submissions))
  83. updater.start_polling()
  84. updater.idle()