add delete button to images uploaded via rpc

This commit is contained in:
bakatrouble 2024-06-01 12:00:35 +03:00
parent 2b11e04ad3
commit 393bd636b2
2 changed files with 21 additions and 3 deletions

View File

@ -14,7 +14,7 @@ from PIL import Image
from django.db import models from django.db import models
from django.db.models import Q from django.db.models import Q
from telegram import Update, Bot, InputMediaPhoto 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 jsonrpc import Dispatcher as RPCDispatcher
from djconfig import config from djconfig import config
@ -60,7 +60,7 @@ class ChannelHelperBotModuleConfig(TelegramBotModuleConfig):
width, height = im.size width, height = im.size
if width > 2000 or height > 2000: if width > 2000 or height > 2000:
im.thumbnail((2000, 2000)) im.thumbnail((2000, 2000), Image.LANCZOS)
im = im.convert('RGB') im = im.convert('RGB')
fpath = os.path.join(tempfile.gettempdir(), '{}.jpg'.format(uuid4())) fpath = os.path.join(tempfile.gettempdir(), '{}.jpg'.format(uuid4()))
im.save(fpath) im.save(fpath)
@ -193,6 +193,17 @@ class ChannelHelperBotModuleConfig(TelegramBotModuleConfig):
except QueuedItem.DoesNotExist: except QueuedItem.DoesNotExist:
update.effective_message.reply_text('Not found') 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): def handle_count(self, update: Update, ctx: CallbackContext):
if self.users.count() and not self.users.filter(user_id=update.effective_user.id).count(): if self.users.count() and not self.users.filter(user_id=update.effective_user.id).count():
update.effective_message.reply_text('GTFO') update.effective_message.reply_text('GTFO')
@ -202,6 +213,7 @@ class ChannelHelperBotModuleConfig(TelegramBotModuleConfig):
def build_dispatcher(self, dispatcher: Dispatcher): def build_dispatcher(self, dispatcher: Dispatcher):
dispatcher.add_handler(CommandHandler(['delete', 'del', 'remove', 'rem'], self.handle_delete, Filters.reply)) dispatcher.add_handler(CommandHandler(['delete', 'del', 'remove', 'rem'], self.handle_delete, Filters.reply))
dispatcher.add_handler(CommandHandler(['count'], self.handle_count)) 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)) dispatcher.add_handler(MessageHandler(Filters.private, self.handle_message))
return dispatcher return dispatcher

View File

@ -3,7 +3,7 @@ import logging
import os import os
import traceback import traceback
from telegram import Bot from telegram import Bot, InlineKeyboardMarkup, InlineKeyboardButton
from telegram.error import RetryAfter from telegram.error import RetryAfter
from config.celery import app from config.celery import app
@ -22,6 +22,9 @@ def upload_image_rpc(self, queued_image_pk, fpath, caption):
config.tmp_uploads_chat_id, config.tmp_uploads_chat_id,
open(fpath, 'rb'), open(fpath, 'rb'),
caption=caption, caption=caption,
reply_markup=InlineKeyboardMarkup([[
InlineKeyboardButton('Delete', callback_data=f'del {queued_image_pk}')
]]),
) )
qi.message_id = m.message_id qi.message_id = m.message_id
qi.args = json.dumps([m.photo[-1].file_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, config.tmp_uploads_chat_id,
open(fpath, 'rb'), open(fpath, 'rb'),
caption=caption, caption=caption,
reply_markup=InlineKeyboardMarkup([[
InlineKeyboardButton('Delete', callback_data=f'del {queued_image_pk}')
]]),
) )
qi.message_id = m.message_id qi.message_id = m.message_id
qi.args = json.dumps([m.animation.file_id]) qi.args = json.dumps([m.animation.file_id])