skip to Main Content

Telegram introduced Bot Web App View recently, and I need from my bot, which is running in Python, to send a custom payload of data to the site. I know that the site can get stuff like color schemes, user info and so on, but I need my custom packet as well. I really need to use the web app as just an interface to a really, really small database (~30 items) in json. (Other solutions, like storing the database with the website is not useful nor convenient to me)

Is this even possible? I’ve tried looking everywhere, but I’m not sure.

2

Answers


  1. Custom data cannot be currently transmitted from the bot to the webapp as of April 2022.

    Login or Signup to reply.
  2. ofcourse you can, it depends on the webapp you write, following python telegram bot manual i can use my webapp to send payload Json data for my bot.I used javascript in my webapp. for example
    1. code in my webapp:

    <head>
    <meta charset="UTF-8">
        <script src="https://telegram.org/js/telegram-web-app.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/@jaames/iro@5"></script>
    </head>
    <body>
    <script type="text/javascript" defer>
        Telegram.WebApp.ready();
        Telegram.WebApp.MainButton.setText('Xác nhận').show().onClick(function () {
            const data = JSON.stringify({
              username: document.querySelector('input[name=username]').value,
              password: document.querySelector('input[name=password]').value,
              date_start: document.querySelector('input[name=dates]').value,
              date_end: document.querySelector('input[name=datee]').value,
              ei_type: document.getElementsByName('ei_type')[0].value,
              download_ei: [].filter.call(document.getElementsByName('download_ei'), (c) => c.checked).map(c => c.value)
             });
            Telegram.WebApp.sendData(data);
            Telegram.WebApp.close();
        });
    </script>
    
    <script type="text/javascript">
        Telegram.WebApp.expand();
    </script>
    </body>

    2. my Python code:

    #the async function where show up button press and open my webapp
    async def downloadEI(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    user = update.callback_query.from_user
    usertelid = user.id
    msg_id = update.callback_query.message.message_id
    userFname = user.first_name
    userLname = user.last_name
    telusername = user.username
    if userFname is None: userFname = ''
    if userLname is None: userLname = ''
    if telusername is None: telusername = ''
    if userFname == '' or userLname == '':
        userFullname = telusername
    else:
        userFullname = userFname+' '+userLname
    query = update.callback_query
    logger.info("merLoadImgTop-User %s choose: %s",userFullname, str(query.data))
    await query.edit_message_text(
        f"@{telusername} processing your request..."
    )
    #sleep(3)
    await update.callback_query.message.reply_text(
        "press me!",
        reply_markup=ReplyKeyboardMarkup.from_button(
            KeyboardButton(
                text="fill the info",        
                web_app=WebAppInfo(url="my webapp url")
                ),
                
            )
        ),
    )
    return DOWNLOADEIPROCESS
    
    #the function when catching webapp return data:
    

    async def downloadEIProcess(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    data = update.effective_message.web_app_data.data

    please give more imformation about your code in order to let me help you out.

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