Change name template and add !sub names to actualize all names

New format: [contain/regexp] 'rule' = [id_from] name_from -> [id_dest] name_dest
!sub names resolves chat titles for all existing rules and updates their names

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
bvn13 2026-06-28 18:57:43 +03:00
parent bb203a7356
commit 501614f9c9
2 changed files with 36 additions and 2 deletions

View File

@ -137,7 +137,8 @@ class Bot():
value = tokens[5]
src_title = await self.__get_chat_title(src)
dst_title = await self.__get_chat_title(dst)
name = "{0} -> {1} ({2})".format(src_title, dst_title, value)
name = "[{0}] '{1}' = [{2}] {3} -> [{4}] {5}".format(
ftype, value, src, src_title, dst, dst_title)
rule = self.__config.add_subscription(src, ftype, value, dst, name=name)
if rule is None:
return "Already exists: {0} [{1}] '{2}' -> {3}".format(src, ftype, value, dst)
@ -166,10 +167,30 @@ class Bot():
return "No such line: {0} (total: {1})".format(line_no, len(subs))
self.__config.set_enabled(subs[line_no - 1]['id'], False)
return "Disabled line {0}".format(line_no)
if cmd == 'names':
return await self.__sub_actualize_names()
except (ValueError, IndexError):
return self.__sub_help()
return self.__sub_help()
async def __sub_actualize_names(self) -> str:
subs = self.__config.list_subscriptions()
if not subs:
return "No subscriptions"
updated = 0
for s in subs:
src = s.get('srcChatId')
dst = s.get('action', {}).get('chatId')
ftype = s.get('filter', {}).get('type', '')
value = s.get('filter', {}).get('value', '')
src_title = await self.__get_chat_title(src)
dst_title = await self.__get_chat_title(dst)
name = "[{0}] '{1}' = [{2}] {3} -> [{4}] {5}".format(
ftype, value, src, src_title, dst, dst_title)
self.__config.set_name(s['id'], name)
updated += 1
return "Updated {0} names".format(updated)
async def __get_chat_title(self, chat_id: int) -> str:
try:
entity = await self.__bot.get_entity(chat_id)
@ -199,7 +220,8 @@ class Bot():
"!sub list\n"
"!sub add <src_chat_id> <dst_chat_id> <contain|regexp> <value>\n"
"!sub del <line>\n"
"!sub on <line> | !sub off <line>")
"!sub on <line> | !sub off <line>\n"
"!sub names")
async def __send_all_chats_to_owner(self) -> None:
text = ""

View File

@ -78,6 +78,18 @@ class Config():
return True
return False
def set_name(self, sub_id: int, name: str) -> bool:
with self.__lock:
for rules in self.__config.get('onMessage', {}).values():
if not isinstance(rules, list):
continue
for rule in rules:
if rule.get('id') == sub_id:
rule['name'] = name
self.__save()
return True
return False
def set_enabled(self, sub_id: int, enabled: bool) -> bool:
with self.__lock:
for rules in self.__config.get('onMessage', {}).values():