GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — python-telegram-bot/examples/webappbot.py at master · MSutt/python-telegram-bot Expand file tree/ Copy pathwebappbot.pyMore file actions 77 lines (62 loc) · 2.82 KB / Copy pathwebappbot.pyFile metadata and controls77 lines (62 loc) · 2.82 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 #!/usr/bin/env python # pylint: disable=unused-argument,wrong-import-position # This program is dedicated to the public domain under the CC0 license. """ Simple example of a Telegram WebApp which displays a color picker. The static website for this website is hosted by the PTB team for your convenience. Currently only showcases starting the WebApp via a KeyboardButton, as all other methods would require a bot token. """ import json import logging from telegram import __version__ as TG_VER try: from telegram import __version_info__ except ImportError: __version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment] if __version_info__ < (20, 0, 0, "alpha", 1): raise RuntimeError( f"This example is not compatible with your current PTB version {TG_VER}. To view the " f"{TG_VER} version of this example, " f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html" ) from telegram import KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove, Update, WebAppInfo from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters # Enable logging logging.basicConfig( format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO ) logger = logging.getLogger(__name__) # Define a `/start` command handler. async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: """Send a message with a button that opens a the web app.""" await update.message.reply_text( "Please press the button below to choose a color via the WebApp.", reply_markup=ReplyKeyboardMarkup.from_button( KeyboardButton( text="Open the color picker!", web_app=WebAppInfo(url="https://python-telegram-bot.org/static/webappbot"), ) ), ) # Handle incoming WebAppData async def web_app_data(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: """Print the received data and remove the button.""" # Here we use `json.loads`, since the WebApp sends the data JSON serialized string # (see webappbot.html) data = json.loads(update.effective_message.web_app_data.data) await update.message.reply_html( text=f"You selected the color with the HEX value <code>{data['hex']}</code>. The " f"corresponding RGB value is <code>{tuple(data['rgb'].values())}</code>.", reply_markup=ReplyKeyboardRemove(), ) def main() -> None: """Start the bot.""" # Create the Application and pass it your bot's token. application = Application.builder().token("TOKEN").build() application.add_handler(CommandHandler("start", start)) application.add_handler(MessageHandler(filters.StatusUpdate.WEB_APP_DATA, web_app_data)) # Run the bot until the user presses Ctrl-C application.run_polling() if __name__ == "__main__": main() |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |