Search code examples
pythonweb-applicationstelegramaiogram

unable to create web_app button aiogram


Good afternoon, an update has recently been released in telegram and added web_app buttons. leading to the "site" in the telegram itself, the documentation says that you need to import the script to the page and create a button in the bot

html:

<html>
<head>
    <script src="https://telegram.org/js/telegram-web-app.js"></script>
</head>
</html>

code for sending a message with a button:

from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from aiogram.types.web_app_info import WebAppInfo
await msg.reply("test", reply_markup=InlineKeyboardMarkup().add(InlineKeyboardButton(text="test", web_app=WebAppInfo(url="https://test_link"))))

Error:

Button_type_invalid

thanks in advance for your help)

Update: you cannot send a message with a web_app type button in non-personal messages to the user. attempting to do so produces an error "Button_type_invalid"


Solution

  • Try it like this, everything worked for me, although yesterday I also encountered this error, it's strange. Run this code with your API token:

    from aiogram import Bot, Dispatcher, executor, types
    from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton
    from aiogram.types.web_app_info import WebAppInfo
    
    API_TOKEN = 'your bot api token'
    
    bot = Bot(token=API_TOKEN)
    dp = Dispatcher(bot)
    
    @dp.message_handler(commands="start")
    async def cmd_start(message: types.Message):
        await message.answer("test", 
    reply_markup=InlineKeyboardMarkup().add(InlineKeyboardButton(text="test", 
    web_app=WebAppInfo(url="https://github.com/aiogram/aiogram/issues/891"))))
    
    if __name__ == '__main__':
        executor.start_polling(dp, skip_updates=True)