Telegrammini应用程序:无法将数据从迷你应用传输到bot

问题描述 投票:0回答:1
我已经探索了各种示例和Mini-Apps的实现,并发现用

InlineKeyboardButton

KeyboardButton取代InlineKeyboardMarkup

ReplyKeyboardMarkup
使数据传输可行。这是否意味着不能用于启动迷你应用?但是,我看到了使用
InlineKeyboardButton
启动迷你应用程序的机器人。这是否意味着需要其他步骤使其与内联按钮一起使用?
机器人代码:
InlineKeyboardButton
迷你应用程序代码:

from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, WebAppInfo from telegram.ext import Application, CommandHandler, MessageHandler, ContextTypes, filters from telegram_token import TELEGRAM_TOKEN async def start(update: Update, context: ContextTypes.DEFAULT_TYPE): keyboard = [ [InlineKeyboardButton( "🕒 Set time", web_app=WebAppInfo(url="https://testsite.com/time-widget/test.html?time=9:33") )] ] await update.message.reply_text( "Press this button to set time:", reply_markup=InlineKeyboardMarkup(keyboard) ) async def handle_web_app_data(update: Update, context: ContextTypes.DEFAULT_TYPE): data = update.message.web_app_data.data await update.message.reply_text(f"Received data: {data}") def main(): application = Application.builder().token(TELEGRAM_TOKEN).build() application.add_handler(CommandHandler("start", start)) application.add_handler(MessageHandler(filters.StatusUpdate.WEB_APP_DATA, handle_web_app_data)) application.run_polling() if __name__ == "__main__": main()

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Time Picker</title> <script src="https://telegram.org/js/telegram-web-app.js?56"></script> </head> <body> <button id="sendTimeBtn">Send current time</button> <script> const tg = window.Telegram?.WebApp; function sendTime() { if (!tg) { alert('Works only in Telegram!'); return; } // Get the current time const now = new Date(); const time = `${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}`; tg.sendData(`time:${time}`); tg.close(); } if (tg) { tg.ready(); document.getElementById('sendTimeBtn').addEventListener('click', sendTime); } else { document.body.innerHTML = '<p>Launch in Telegram!</p>'; } </script> </body> </html>

仅当应用程序通过键盘按钮根据文档打开时才能工作
telegram telegram-bot python-telegram-bot telegram-api
1个回答
0
投票


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.