exclam: Add edit (!ed) command to modify already sent messages
patch 74e097435eeb72086c6ed3f43a98ce3f5463d1c6
Author: E. Bosch <presidev@AT@gmail.com>
Date: Tue Jul 11 01:48:40 CEST 2023
* exclam: Add edit (!ed) command to modify already sent messages
diff -rN -u old-irgramd/exclam.py new-irgramd/exclam.py
--- old-irgramd/exclam.py 2024-11-22 16:15:31.517362224 +0100
+++ new-irgramd/exclam.py 2024-11-22 16:15:31.517362224 +0100
@@ -6,6 +6,8 @@
# Use of this source code is governed by a MIT style license that
# can be found in the LICENSE file included in this project.
+from telethon.errors.rpcerrorlist import MessageNotModifiedError, MessageAuthorRequiredError
+
from utils import command, HELP
class exclam(command):
@@ -13,6 +15,7 @@
self.commands = \
{ # Command Handler Arguments Min Max Maxsplit
'!re': (self.handle_command_re, 2, 2, 2),
+ '!ed': (self.handle_command_ed, 2, 2, 2),
}
self.tg = telegram
self.irc = telegram.irc
@@ -28,10 +31,14 @@
res = False
return res, self.tmp_tg_msg
+ async def check_msg(self, cid):
+ id = self.tg.mid.id_to_num_offset(self.tmp_telegram_id, cid)
+ chk_msg = await self.tg.telegram_client.get_messages(entity=self.tmp_telegram_id, ids=id)
+ return id, chk_msg
+
async def handle_command_re(self, cid=None, msg=None, help=None):
if not help:
- id = self.tg.mid.id_to_num_offset(self.tmp_telegram_id, cid)
- chk_msg = await self.tg.telegram_client.get_messages(entity=self.tmp_telegram_id, ids=id)
+ id, chk_msg = await self.check_msg(cid)
if chk_msg is not None:
self.tmp_tg_msg = await self.tg.telegram_client.send_message(self.tmp_telegram_id, msg, reply_to=id)
reply = True
@@ -47,3 +54,29 @@
'channel/chat.'
)
return reply
+
+ async def handle_command_ed(self, cid=None, new_msg=None, help=None):
+ if not help:
+ id, ed_msg = await self.check_msg(cid)
+ if ed_msg is not None:
+ try:
+ self.tmp_tg_msg = await self.tg.telegram_client.edit_message(ed_msg, new_msg)
+ except MessageNotModifiedError:
+ self.tmp_tg_msg = ed_msg
+ reply = True
+ except MessageAuthorRequiredError:
+ reply = ('Not the author of the message to edit',)
+ else:
+ reply = True
+ else:
+ reply = ('Unknown message to edit',)
+ else: # HELP.brief or HELP.desc (first line)
+ reply = (' !ed Edit a message',)
+ if help == HELP.desc: # rest of HELP.desc
+ reply += \
+ (
+ ' !ed <compact_id> <new_message>',
+ 'Edit a message with <compact_id> on current channel/chat,',
+ '<new_message> replaces the current message.'
+ )
+ return reply