skip to Main Content

Problem: I used FSM Telegram Bot and in the end bot changes chat user to chat admin and changes admin’s custom title . But I have a problem with changing custom title, this is console output:

ERROR:aiogram.event:Cause exception while process update id=XXXXXXXX by bot id=XXXXXXXX
TelegramBadRequest: Telegram server says - Bad Request: RIGHT_FORBIDDEN

Code:

@dp.message(CommandStart())
async def start_questionnaire_process(message: Message, state: FSMContext):
    async with ChatActionSender.typing(bot=bot, chat_id=message.chat.id):
        await asyncio.sleep(2)
        await message.answer('Hi. What is your name: ')
    await state.set_state(Form.name)


@dp.message(F.text, Form.name)
async def capture_name(message: Message, state: FSMContext):
    await state.update_data(name=message.text)
    async with ChatActionSender.typing(bot=bot, chat_id=message.chat.id):
        await asyncio.sleep(2)
        await message.answer('How old a you: ')
    await state.set_state(Form.age)

    
@dp.message(F.text, Form.age)
async def capture_age(message: Message, state: FSMContext):
    check_age = extract_number(message.text)
    await state.update_data(age=check_age)

    data = await state.get_data()
    msg_text = (f'Name:{data.get("name")}/Age:{data.get("age")}')
    await message.answer(msg_text)
    await state.clear()
    
    #####
    await message.bot.promote_chat_member(chat_id=CHANNEL_ID, 
                                user_id=message.from_user.id,
                                request_timeout=2)
    await message.bot.set_chat_administrator_custom_title(chat_id=CHANNEL_ID,
                                                        user_id=message.from_user.id,
                                                        request_timeout=2,
                                                        custom_title=msg_text)

Could your please help me?

Edit: I do not have a problem with promote_chat_member(), I have a problem with set_chat_administrator_custom_title(). With first run /start in console output Bad Request: RIGHT_FORBIDDEN, error in function set_chat_administrator_custom_title() and with second run /start, function `set_chat_administrator_custom_title() finishes correctly.

Bot has only one permission "Add new Admins".

2

Answers


  1. Check Bot Permissions:

    1. Go to your Telegram channel.
    2. Open the channel settings.
    3. Tap "Administrators".
    4. Find your bot in the list of administrators.
    5. Ensure that the "Can promote members" permission is enabled for your
      bot.

    Promote User with Bot:

    1. If your bot doesn’t have the permission to promote members, grant it temporarily.
    2. Modify your code to first promote the user to admin using bot.
    3. promote_chat_member() if they are not already an admin. Then, set the custom title using bot.set_chat_administrator_custom_title().
    @dp.message(F.text, Form.age)
    async def capture_age(message: Message, state: FSMContext):
        # ... (rest of your code)
    
        member = await bot.get_chat_member(chat_id=CHANNEL_ID, user_id=message.from_user.id)
        if not member.is_chat_admin():  # Check if user is already an admin
            await message.bot.promote_chat_member(
                chat_id=CHANNEL_ID,
                user_id=message.from_user.id,
                can_change_info=True,  # Essential for setting custom title
                can_delete_messages=True,  # Other permissions as needed
                # ... other relevant permissions
            )
    
        await message.bot.set_chat_administrator_custom_title(
            chat_id=CHANNEL_ID,
            user_id=message.from_user.id,
            custom_title=msg_text,
        )
    
    Login or Signup to reply.
  2. I ran into the same problem and seemed to have solved it.
    If bot trying to change custom title for admin, that not promoted by bot itself, it cannot do it. In other words – bot can change custom title only for admins promoted by it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search