From 393bd636b2c1f8bbd0d1f18ede54ee6d2cc9ce57 Mon Sep 17 00:00:00 2001 From: bakatrouble Date: Sat, 1 Jun 2024 12:00:35 +0300 Subject: [PATCH] add delete button to images uploaded via rpc --- bots/modules/channel_helper.py | 16 ++++++++++++++-- bots/tasks.py | 8 +++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/bots/modules/channel_helper.py b/bots/modules/channel_helper.py index 9b78dee..6946a70 100644 --- a/bots/modules/channel_helper.py +++ b/bots/modules/channel_helper.py @@ -14,7 +14,7 @@ from PIL import Image from django.db import models from django.db.models import Q from telegram import Update, Bot, InputMediaPhoto -from telegram.ext import Dispatcher, CallbackContext, MessageHandler, Filters, CommandHandler +from telegram.ext import Dispatcher, CallbackContext, MessageHandler, Filters, CommandHandler, CallbackQueryHandler from jsonrpc import Dispatcher as RPCDispatcher from djconfig import config @@ -60,7 +60,7 @@ class ChannelHelperBotModuleConfig(TelegramBotModuleConfig): width, height = im.size if width > 2000 or height > 2000: - im.thumbnail((2000, 2000)) + im.thumbnail((2000, 2000), Image.LANCZOS) im = im.convert('RGB') fpath = os.path.join(tempfile.gettempdir(), '{}.jpg'.format(uuid4())) im.save(fpath) @@ -193,6 +193,17 @@ class ChannelHelperBotModuleConfig(TelegramBotModuleConfig): except QueuedItem.DoesNotExist: update.effective_message.reply_text('Not found') + def handle_delete_callback(self, update: Update, ctx: CallbackContext): + message_id = update.message.message_id + try: + msg = QueuedItem.objects.get(Q(message_id=message_id) | Q(message_ids_extra__contains=f'{message_id}'), + config=self) + msg.delete() + update.effective_message.edit_text('Deleted', reply_markup=None) + update.callback_query.answer('Deleted') + except QueuedItem.DoesNotExist: + update.callback_query.answer('Not found') + def handle_count(self, update: Update, ctx: CallbackContext): if self.users.count() and not self.users.filter(user_id=update.effective_user.id).count(): update.effective_message.reply_text('GTFO') @@ -202,6 +213,7 @@ class ChannelHelperBotModuleConfig(TelegramBotModuleConfig): def build_dispatcher(self, dispatcher: Dispatcher): dispatcher.add_handler(CommandHandler(['delete', 'del', 'remove', 'rem'], self.handle_delete, Filters.reply)) dispatcher.add_handler(CommandHandler(['count'], self.handle_count)) + dispatcher.add_handler(CallbackQueryHandler(self.handle_delete_callback, pattern=r'^del (\d+)$')) dispatcher.add_handler(MessageHandler(Filters.private, self.handle_message)) return dispatcher diff --git a/bots/tasks.py b/bots/tasks.py index 444bb01..b7e7581 100644 --- a/bots/tasks.py +++ b/bots/tasks.py @@ -3,7 +3,7 @@ import logging import os import traceback -from telegram import Bot +from telegram import Bot, InlineKeyboardMarkup, InlineKeyboardButton from telegram.error import RetryAfter from config.celery import app @@ -22,6 +22,9 @@ def upload_image_rpc(self, queued_image_pk, fpath, caption): config.tmp_uploads_chat_id, open(fpath, 'rb'), caption=caption, + reply_markup=InlineKeyboardMarkup([[ + InlineKeyboardButton('Delete', callback_data=f'del {queued_image_pk}') + ]]), ) qi.message_id = m.message_id qi.args = json.dumps([m.photo[-1].file_id]) @@ -54,6 +57,9 @@ def upload_animation_rpc(self, queued_image_pk, fpath, caption): config.tmp_uploads_chat_id, open(fpath, 'rb'), caption=caption, + reply_markup=InlineKeyboardMarkup([[ + InlineKeyboardButton('Delete', callback_data=f'del {queued_image_pk}') + ]]), ) qi.message_id = m.message_id qi.args = json.dumps([m.animation.file_id])