Telegram Bot调用set_chat_administrator_custom_title遇RIGHT_FORBIDDEN错误求助
Telegram Bot 自定义头衔权限错误问题解决
问题根源
首次执行流程时,promote_chat_member 成功后,Telegram服务器的权限状态同步存在延迟——虽然Bot已经完成了提升操作,但服务器还没及时更新Bot对该新管理员的操作权限,导致紧接着调用 set_chat_administrator_custom_title 时触发权限禁止错误。第二次执行时,权限状态已经同步完成,所以操作正常。
解决方案
1. 增加操作间隔延迟
在 promote_chat_member 调用完成后,添加短暂的延迟,给服务器足够时间同步权限状态。
- 同步代码示例:
import time # 提升用户为管理员 bot.promote_chat_member(chat_id, user_id, can_add_web_page_previews=False, ...) # 延迟1秒,可根据实际情况调整时长 time.sleep(1) # 设置自定义头衔 bot.set_chat_administrator_custom_title(chat_id, user_id, "自定义头衔")
- 异步代码示例(如python-telegram-bot v13+):
import asyncio # 提升用户为管理员 await bot.promote_chat_member(chat_id, user_id, can_add_web_page_previews=False, ...) # 延迟1秒,可根据实际情况调整时长 await asyncio.sleep(1) # 设置自定义头衔 await bot.set_chat_administrator_custom_title(chat_id, user_id, "自定义头衔")
2. 实现错误重试机制
捕获 RIGHT_FORBIDDEN 错误,自动重试操作,避免单次延迟不够的情况:
from telegram.error import TelegramBadRequest max_retries = 3 retry_delay = 1 for attempt in range(max_retries): try: bot.set_chat_administrator_custom_title(chat_id, user_id, "自定义头衔") break except TelegramBadRequest as e: if "RIGHT_FORBIDDEN" in str(e) and attempt < max_retries - 1: time.sleep(retry_delay) retry_delay *= 2 # 每次重试延迟翻倍,降低服务器压力 else: raise
异步版本同理,改用 asyncio.sleep 和异步异常捕获逻辑即可。
3. 额外权限检查
虽然你提到Bot已有「添加新管理员」权限,仍需确认:
- Bot自身在频道中的角色为管理员(而非仅拥有添加权限的普通成员)
- 频道权限设置中,允许管理员修改其他管理员的头衔
内容的提问来源于stack exchange,提问作者Sv.
相关产品推荐
相关产品推荐

