Telegram Web App Bot Template

Start with this template
50
import os
import logging
from telegram import Update
from telegram.ext import (
    Updater,
    CommandHandler,
    MessageHandler,
    CallbackContext,
    Filters,
)

TELEGRAM_API_TOKEN = os.getenv('TELEGRAM_API_TOKEN')

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

def start(update: Update, context: CallbackContext) -> None:
    message = (
        "• ***HOW TO MAKE A TELEGRAM WEB APP WITH LAZY AI*** 😜\n\n"
        "**Building your web app**\n"
        "-> Create your game as an ordinary website in Lazy AI. Treat it as if you were just creating a game for the browser and not for Telegram at all.\n\n"
        "**Building a launcher bot**\n"
        "-> Go into the Lazy Builder and say \"Build me a Telegram bot that says hello when people ping it\" and *send*.\n\n"
        "-> Get a Telegram bot API key by messaging @BotFather on Telegram and saying /newbot. You will be asked about the name and username of the new bot you want to create, and this process will ultimately leave you with an API key for your new bot.\n\n"
Get full code

Telegram Web App Bot Template

Created: | Last Updated:

Introduction to the Telegram Web App Bot Template

This template helps you create a Telegram bot that provides users with steps to build a Telegram web app using Lazy AI. The bot responds with a detailed guide when users interact with it.

Clicking Start with this Template

To get started with the template, click "Start with this Template".

Test

Press the "Test" button to begin the deployment of the app. The Lazy CLI will handle the deployment process.

Using the App

Once the app is deployed, your bot will be online and ready to interact with users. The bot will respond with a detailed guide on how to build a Telegram web app using Lazy AI when users send the `/start` command.

Integrating the App

To link your web app to your bot, follow these steps:

  • Open the Telegram app and search for @BotFather or go to https://t.me/botfather.
  • Start a chat with BotFather and send the command `/mybots`.
  • Select the bot you want to link to your website.
  • Click on the "Bot Settings" option.
  • Click on "Menu Button".
  • Click on "Configure menu button".
  • BotFather will ask for a URL - send the URL of your Lazy website.

Code Example

`main.py:
import os
import logging
from telegram import Update
from telegram.ext import (
    Updater,
    CommandHandler,
    MessageHandler,
    CallbackContext,
    Filters,
)

TELEGRAM_API_TOKEN = os.getenv('TELEGRAM_API_TOKEN')

logger = logging.getLogger(name)
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

def start(update: Update, context: CallbackContext) -> None:
    message = (
        "• HOW TO MAKE A TELEGRAM WEB APP WITH LAZY AI 😜\n\n"
        "Building your web app\n"
        "-> Create your game as an ordinary website in Lazy AI. Treat it as if you were just creating a game for the browser and not for Telegram at all.\n\n"
        "Building a launcher bot\n"
        "-> Go into the Lazy Builder and say \"Build me a Telegram bot that says hello when people ping it\" and send.\n\n"
        "-> Get a Telegram bot API key by messaging @BotFather on Telegram and saying /newbot. You will be asked about the name and username of the new bot you want to create, and this process will ultimately leave you with an API key for your new bot.\n\n"
        "-> Copy the API key @BotFather gave you into the Env Secrets tab in the Lazy Builder (there will be an Env Secret ready for you to fill)\n\n"
        "-> Run your Lazy app, and your bot will be online\n\n"
        "Linking your web app to your bot\n"
        "-> Go back into Telegram and say /mybots (to BotFather)\n\n"
        "-> Select the bot you want to link to your website\n\n"
        "-> Click on the \"Bot Settings\" option\n\n"
        "-> Click on \"Menu Button\"\n\n"
        "-> Click on \"Configure menu button\"\n\n"
        "-> Botfather will ask for you to send it a URL - send the URL of your Lazy website (domain doesn't matter as the bot will not display the domain anyway)\n\n"
        "Video tutorial coming soon!!!"
    )
    update.message.reply_text(message, parse_mode='Markdown')

def error_handler(update: Update, context: CallbackContext) -> None:
    logger.error(f"Update {update} caused error {context.error}")
    update.message.reply_text("An error occurred. Please try again later.")

def main():
    if not TELEGRAM_API_TOKEN:
        print("🚨 Oops! The TELEGRAM_API_TOKEN 🔑 environment variable is missing. 🚨")
        print("Let's fix that together! 😊 Here's how:\n" +
              "1. 🌐 Open the Telegram app and search for the BotFather or go to https://t.me/botfather 🌐\n" +
              "2. ✏️ Start a chat with BotFather and send the command /newbot. ✏️\n" +
              "3. 🤖 Follow the instructions to set up your new bot, providing a name and username. 🤖\n" +
              "4. 🔑 Once your bot is created, BotFather will provide you with a TELEGRAM_API_TOKEN. 🔑\n" +
              "5. 📋 Copy this token. 📋\n" +
              "6. 🔐 Set this value in the Env Secrets tab 🔐\n" +
              "For more information, refer to the Telegram Bot API documentation: https://core.telegram.org/bots#3-how-do-i-create-a-bot. 🚀")
        return

updater = Updater(TELEGRAM_API_TOKEN)

dispatcher = updater.dispatcher
    dispatcher.add_handler(CommandHandler("start", start))
    dispatcher.add_error_handler(error_handler)

logger.info("Bot has successfully connected and started polling. 🚀")
    updater.start_polling()
    updater.idle()

if name == "main":
    main()

requirements.txt:
python-telegram-bot==13.13`

For more information, refer to the Telegram Bot API documentation: https://core.telegram.org/bots#3-how-do-i-create-a-bot.

Technologies

Telegram Telegram