Skip to content

Commit 7c6dcc5

Browse files
committed
feat: add /update_info support for thread
1 parent 618dee2 commit 7c6dcc5

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

efb_telegram_master/bot_manager.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,11 @@ def get_chat_info(self, *args, **kwargs):
556556
def create_forum_topic(self, *args, **kwargs):
557557
return self.updater.bot.create_forum_topic(*args, **kwargs)
558558

559+
@Decorators.retry_on_timeout
560+
@Decorators.retry_on_chat_migration
561+
def edit_forum_topic(self, *args, **kwargs):
562+
return self.updater.bot.edit_forum_topic(*args, **kwargs)
563+
559564
@Decorators.retry_on_timeout
560565
@Decorators.retry_on_chat_migration
561566
def reopen_forum_topic(self, *args, **kwargs) -> bool:

efb_telegram_master/chat_binding.py

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -905,13 +905,18 @@ def update_group_info(self, update: Update, context: CallbackContext):
905905
if update.effective_chat.type == telegram.Chat.PRIVATE:
906906
return self.bot.reply_error(update, self._('Send /update_info to a group where this bot is a group admin '
907907
'to update group title, description and profile picture.'))
908+
909+
if update.effective_chat.is_forum:
910+
return self.update_thread_info(update, context)
911+
908912
forwarded_from_chat = update.effective_message.forward_from_chat
909913
if forwarded_from_chat and forwarded_from_chat.type == telegram.Chat.CHANNEL:
910-
tg_chat = forwarded_from_chat.id
914+
tg_chat = forwarded_from_chat
911915
else:
912-
tg_chat = update.effective_chat.id
916+
tg_chat = update.effective_chat
917+
913918
chats = self.db.get_chat_assoc(master_uid=utils.chat_id_to_str(channel=self.channel,
914-
chat_uid=ChatID(str(tg_chat))))
919+
chat_uid=ChatID(str(tg_chat.id))))
915920
if len(chats) != 1:
916921
return self.bot.reply_error(update, self.ngettext('This only works in a group linked with one chat. '
917922
'Currently {0} chat linked to this group.',
@@ -929,7 +934,7 @@ def update_group_info(self, update: Update, context: CallbackContext):
929934
try:
930935
chat = self.chat_manager.update_chat_obj(channel.get_chat(chat_uid), full_update=True)
931936

932-
self.bot.set_chat_title(tg_chat, self.truncate_ellipsis(chat.chat_title, self.MAX_LEN_CHAT_TITLE))
937+
self.bot.set_chat_title(tg_chat.id, self.truncate_ellipsis(chat.chat_title, self.MAX_LEN_CHAT_TITLE))
933938

934939
# Update remote group members list to Telegram group description if available
935940
desc = chat.description
@@ -944,7 +949,7 @@ def update_group_info(self, update: Update, context: CallbackContext):
944949
if desc:
945950
try:
946951
self.bot.set_chat_description(
947-
tg_chat, self.truncate_ellipsis(desc, self.MAX_LEN_CHAT_DESC))
952+
tg_chat.id, self.truncate_ellipsis(desc, self.MAX_LEN_CHAT_DESC))
948953
except BadRequest as e:
949954
if "Chat description is not modified" in e.message:
950955
pass
@@ -969,7 +974,7 @@ def update_group_info(self, update: Update, context: CallbackContext):
969974

970975
picture.seek(0)
971976

972-
self.bot.set_chat_photo(tg_chat, pic_resized or picture)
977+
self.bot.set_chat_photo(tg_chat.id, pic_resized or picture)
973978
update.effective_message.reply_text(self._('Chat details updated.'))
974979
except EFBChatNotFound:
975980
self.logger.exception("Chat linked (%s) is not found in the slave channel "
@@ -994,6 +999,48 @@ def update_group_info(self, update: Update, context: CallbackContext):
994999
if pic_resized and getattr(pic_resized, 'close', None):
9951000
pic_resized.close()
9961001

1002+
def update_thread_info(self, update: Update, context: CallbackContext):
1003+
assert isinstance(update, Update)
1004+
assert update.effective_message
1005+
assert update.effective_chat
1006+
1007+
try:
1008+
thread_id = update.effective_message.message_thread_id
1009+
if thread_id:
1010+
slave_origin_uid = self.db.get_topic_slave(
1011+
topic_chat_id=TelegramChatID(update.effective_message.chat_id),
1012+
message_thread_id=thread_id
1013+
)
1014+
channel_id, chat_id, _ = utils.chat_id_str_to_id(slave_origin_uid)
1015+
etm_chat: ETMChatType = self.chat_manager.get_chat(channel_id, chat_id, build_dummy=True)
1016+
self.bot.edit_forum_topic(
1017+
chat_id=update.effective_chat.id,
1018+
message_thread_id=thread_id,
1019+
name=self.truncate_ellipsis(etm_chat.chat_title, self.MAX_LEN_CHAT_TITLE),
1020+
icon_custom_emoji_id="" # param required by telegram
1021+
)
1022+
update.effective_message.reply_text(self._('Chat details updated.'))
1023+
except EFBChatNotFound:
1024+
self.logger.exception("Chat linked (%s) is not found in the slave channel "
1025+
"(%s).", channel_id, chat_uid)
1026+
return self.bot.reply_error(update, self._("Chat linked ({chat_uid}) is not found in the slave channel "
1027+
"({channel_name}, {channel_id}).")
1028+
.format(channel_name=channel.channel_name, channel_id=channel_id,
1029+
chat_uid=chat_uid))
1030+
except TelegramError as e:
1031+
if e.message == "Topic_not_modified":
1032+
update.effective_message.reply_text(self._('Chat details updated.'))
1033+
else:
1034+
self.logger.exception("Error occurred while update chat details.")
1035+
return self.bot.reply_error(update, self._('Error occurred while update chat details.\n'
1036+
'{0}'.format(e.message)))
1037+
except EFBOperationNotSupported:
1038+
return self.bot.reply_error(update, self._('No profile picture provided from this chat.'))
1039+
except Exception as e:
1040+
self.logger.exception("Unknown error caught when querying chat.")
1041+
return self.bot.reply_error(update, self._('Error occurred while update chat details. \n'
1042+
'{0}'.format(e)))
1043+
9971044
def chat_migration(self, update: Update, context: CallbackContext):
9981045
"""Triggered by any message update with either
9991046
``migrate_from_chat_id`` or ``migrate_to_chat_id``

0 commit comments

Comments
 (0)