Search code examples
pythonaiogram

How to implement a share profile button in aiogram like PremiumBot?


How to implement a share profile button in aiogram like PremiumBot?

Im not sure if it is even possible because i didnt found any info about it in official documentation

Create share profile button like in PremiumBot (I attached photo)

enter image description here


Solution

  • This feature was added in Bot API 6.5. You can create a button like this using KeyboardButtonRequestUser. This allows you to select user data according to the desired criteria and send it to the bot.

    keyboard.py

    def button():
        keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True)
        request_button = types.KeyboardButton(text="Select user",
                                      request_user=types.KeyboardButtonRequestUser(
                                            request_id=1
                                        )
                                        )
        keyboard.add(request_button)
        return keyboard
    

    app.py

    import logging
    
    from aiogram import Bot, Dispatcher, executor, types
    
    from keyboard import button
    
    API_TOKEN = 'YOUR BOT TOKEN'
    
    logging.basicConfig(level=logging.INFO)
    
    bot = Bot(token=API_TOKEN)
    dp = Dispatcher(bot)
    
    
    @dp.message_handler(commands=['start'])
    async def send_welcome(message: types.Message):
        await message.reply("Hi!\nI'm Bot!", reply_markup=button())
    
    
    @dp.message_handler(content_types=types.ContentType.USER_SHARED)
    async def get_shared_user_data(message: types.Message):
        logging.info(message.user_shared.user_id)
        await message.answer(message.user_shared.user_id)
    
    
    if __name__ == '__main__':
        executor.start_polling(dp, skip_updates=True)
    

    You can get more information from this link