def reply_message(self, message: Message, text: str): reply = Message() reply.text = text # reply.chat = coordinator.slaves[message.chat.channel_id].get_chat(message.chat.chat_uid) reply.chat = coordinator.slaves[message.chat.module_id].get_chat( message.chat.uid) reply.author = message.chat.make_system_member( uid=self.middleware_id, name=self.middleware_name, middleware=self) reply.type = MsgType.Text # reply.deliver_to = coordinator.master reply.deliver_to = coordinator.slaves[message.chat.module_id] # reply.target = message reply.uid = str(uuid.uuid4()) r2 = reply coordinator.send_message(reply) r2.deliver_to = coordinator.master coordinator.send_message(r2)
def reply_message_img(self, message: Message, im3: Image.Image): reply = Message() # reply.text = text # reply.chat = coordinator.slaves[message.chat.channel_id].get_chat(message.chat.chat_uid) reply.chat = coordinator.slaves[message.chat.module_id].get_chat( message.chat.uid) reply.author = message.chat.make_system_member( uid=self.middleware_id, name=self.middleware_name, middleware=self) reply.type = MsgType.Image reply.mime = 'image/png' f = tempfile.NamedTemporaryFile(suffix='.png') img_data = io.BytesIO() im3.save(img_data, format='png') f.write(img_data.getvalue()) f.file.seek(0) reply.file = f reply.path = f.name reply.filename = os.path.basename(reply.file.name) # reply.deliver_to = coordinator.master reply.deliver_to = coordinator.slaves[message.chat.module_id] # reply.target = message reply.uid = str(uuid.uuid4()) r2 = reply coordinator.send_message(reply) r2.type = MsgType.Image r2.mime = 'image/png' f = tempfile.NamedTemporaryFile(suffix='.png') img_data = io.BytesIO() im3.save(img_data, format='png') f.write(img_data.getvalue()) f.file.seek(0) r2.file = f r2.path = f.name r2.filename = os.path.basename(r2.file.name) r2.deliver_to = coordinator.master coordinator.send_message(r2)
def build_dummy_message(chat: Chat, author: Chat) -> Message: message = Message() message.chat = chat message.author = author return message
def handle_tg_img_preview(self, message: Message): if not message or not message.file or not message.filename: return if message.author.uid == self.middleware_id: # trysh-middleware # self.lg('self') return if message.type != MsgType.Image: return try: message.file.seek(0) fbs = message.file.read() message.file.seek(0) im: Image.Image = Image.open(io.BytesIO(fbs)) max_size = max(im.size) min_size = min(im.size) img_ratio = max_size / min_size if img_ratio < 10.0: return im2 = im.copy() for _ in range(100): max_size = max(im.size) min_size = min(im.size) img_ratio = max_size / min_size if img_ratio >= 10.0: if im.width == min_size: im = im.resize((im.width * 2, im.height), box=(0, 0, 1, 1)) else: im = im.resize((im.width, im.height * 2), box=(0, 0, 1, 1)) continue else: break im.paste(im2, (0, 0, im2.width, im2.height)) im3 = im.convert('RGB') # im.copy() # reply = Message() # reply.text = text # reply.chat = coordinator.slaves[message.chat.channel_id].get_chat(message.chat.chat_uid) reply.chat = coordinator.slaves[message.chat.module_id].get_chat( message.chat.uid) reply.author = message.chat.make_system_member( uid=self.middleware_id, name=self.middleware_name, middleware=self) reply.type = MsgType.Image reply.mime = 'image/png' f = tempfile.NamedTemporaryFile(suffix='.png') img_data = io.BytesIO() im3.save(img_data, format='png') f.write(img_data.getvalue()) f.file.seek(0) reply.file = f reply.path = f.name reply.filename = os.path.basename(reply.file.name) # reply.deliver_to = coordinator.master reply.deliver_to = coordinator.master # reply.target = message reply.uid = str(uuid.uuid4()) coordinator.send_message(reply) except BaseException as e: self.lg(f'handle_tg_img_preview e:{e}') pass