by moonboy

Basic Telegram Bot

Test this app for free
103
from flask import Flask, render_template, url_for, redirect
import os
import logging
import threading
from telegram import Update, WebAppInfo
import telegram
from telegram.ext import Updater, CommandHandler, MessageHandler, CallbackContext, Filters
from web_app_routes import app
from utils import get_bot_username

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')

#DO NOT CHANGE THIS
webapp_url = f"{app.config['BASE_URL']}"

def start(update: Update, context: CallbackContext) -> None:
    update.message.reply_text(
        "Hi! I'm a Telegram Web App. Click the link below to open my simple web app!",
        reply_markup=telegram.InlineKeyboardMarkup([[
            telegram.InlineKeyboardButton("Open Web App", web_app=WebAppInfo(url=webapp_url))
        ]])
Get full code

Basic Telegram Bot

Created: | Last Updated:

The Telegram mini app and bot template is a simple starting point for your Telegram mini app. Asking for changes to the HTML of the application will easily allow you to create the telegram mini app of your desires! Use this skeleton to build telegram bots and telegram mini apps. If telegram is mentioned at all use this as a starting point.

Introduction to the Template

This template provides a starting point for creating a Telegram mini app and bot. It includes a Flask web application and a Telegram bot that can interact with users and display a simple web interface. The template is designed to be easily customizable, allowing you to build the Telegram mini app of your desires.

Getting Started

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

Initial Setup

Environment Secrets

This template requires a Telegram API token to function. Follow these steps to obtain and set up the token:

  1. Get a Telegram bot API key:
  2. Open Telegram and search for "BotFather".
  3. Start a chat with BotFather and type /newbot.
  4. Follow the prompts to create a new bot.
  5. BotFather will provide you with an API key for your new bot.

  6. Set up your bot:

  7. Copy the API key provided by BotFather.
  8. In the Lazy Builder, go to the Environment Secrets tab.
  9. Add a new secret with the name TELEGRAM_API_TOKEN and paste the API key as the value.

Test

Press the Test button to deploy the app and launch the Lazy CLI. The CLI will guide you through any required user input.

Using the App

Telegram Bot

  1. Start the Bot:
  2. Open Telegram and search for your bot using the username provided by BotFather.
  3. Start a chat with your bot and type /start.
  4. The bot will respond with a message and a button to open the web app.

  5. Interact with the Bot:

  6. You can send text messages to the bot, and it will echo your messages back with a button to open the web app.

Web App

  1. Open the Web App:
  2. Click the "Open Web App" button in the bot's response.
  3. The web app will open in your browser.

  4. Profile Page:

  5. If you open the web app from Telegram, it will display your profile information.
  6. If you open the web app outside of Telegram, it will prompt you to use it from Telegram.

Integrating the App

Customizing the Web Interface

  1. Change the App Name:
  2. Open header.html and find the line with <span style="color: #ffffff;">App Name</span>.
  3. Replace "App Name" with your desired app name.

  4. Add Content to the Web App:

  5. Open index.html and add your content inside the <div class="container mt-5"> element.

Customizing the Bot

  1. Add New Commands:
  2. Open main.py and add new command handlers using the CommandHandler class.
  3. For example, to add a /help command, add the following code: ```python def help_command(update: Update, context: CallbackContext) -> None: update.message.reply_text("This is a help message.")

    dispatcher.add_handler(CommandHandler("help", help_command)) ```

  4. Modify Existing Commands:

  5. Modify the start, profile, and echo functions in main.py to change the bot's responses.

External Integrations

If you need to integrate the app with external tools or services, follow these steps:

  1. Telegram Web App Data:
  2. The web app can receive data from Telegram using the initData parameter.
  3. Modify the /profile route in web_app_routes.py to handle additional data as needed.

  4. API Endpoints:

  5. Add new routes to web_app_routes.py to create additional API endpoints.
  6. Use the Flask framework to handle requests and responses.

Conclusion

This template provides a flexible starting point for building a Telegram mini app and bot. Customize the web interface and bot commands to suit your needs, and integrate with external tools as required. Happy building!

Technologies

Flask Flask
Python Python
Telegram Telegram