by davi
Basic Telegram Bot
from utils import print_setup_instructions
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
from abilities import apply_sqlite_migrations
from models import db
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:
Frequently Asked Questions
What are some potential business applications for this Basic Telegram Bot template?
The Basic Telegram Bot template offers numerous business applications. It can be used to create customer service bots, appointment scheduling assistants, or product catalog browsers. For example, a retail business could use this template to build a bot that showcases their latest products, handles basic customer inquiries, and even processes simple orders, all within the Telegram platform.
How can this Basic Telegram Bot template improve user engagement for a business?
This template can significantly enhance user engagement by providing an interactive and accessible interface within Telegram. Businesses can use it to send personalized notifications, conduct surveys, or offer quick access to services. The web app component allows for richer interactions than standard Telegram messages, potentially increasing user retention and satisfaction.
What advantages does using a Telegram mini app offer over a traditional website or mobile app?
Telegram mini apps, like the one in this Basic Telegram Bot template, offer several advantages. They're instantly accessible within Telegram without additional downloads, reducing friction for users. They also leverage Telegram's existing user base and authentication system, simplifying user onboarding. Additionally, mini apps can take advantage of Telegram's features like notifications and inline queries, creating a more integrated and seamless user experience.
How can I customize the appearance of the Basic Telegram Bot template?
You can customize the appearance of the Basic Telegram Bot template by modifying the HTML and CSS files. For example, to change the app name and color scheme, you would edit the header.html
file:
html
<a id="appName" class="navbar-brand d-flex align-items-center" href="{{ url_for('home') }}">
<svg class="telegram-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path fill="#your-color-here" d="M12 0C5.373 0 0 5.373 0 12s5.373 12 12 12 12-5.373 12-12S18.627 0 12 0zm5.562 8.248l-2.17 10.234c-.163.77-.766.98-1.552.61l-4.282-3.158-2.066 1.988c-.23.23-.423.423-.863.423l.308-4.374 7.956-7.187c.346-.308-.076-.48-.538-.172L7.04 12.656l-4.22-1.32c-.918-.288-.935-.918.192-1.36l16.48-6.35c.768-.288 1.438.172 1.19 1.36z"/>
</svg>
<span style="color: #your-color-here;">Your App Name</span>
</a>
You would also update the styles.css
file to change the overall color scheme and layout.
How do I add new functionality to the Basic Telegram Bot template?
To add new functionality to the Basic Telegram Bot template, you'll need to modify both the Python backend and the HTML/JavaScript frontend. For example, to add a new command that responds with the current time, you would add this to the main.py
file:
```python from datetime import datetime
def current_time(update: Update, context: CallbackContext) -> None: now = datetime.now().strftime("%H:%M:%S") update.message.reply_text(f"The current time is: {now}")
# In the setup_telegram_bot function: dispatcher.add_handler(CommandHandler("time", current_time)) ```
Then, you might add a button to request the time in your index.html
:
html
<button id="timeButton" class="btn btn-primary">Get Current Time</button>
And handle it with JavaScript:
javascript
document.getElementById('timeButton').addEventListener('click', function() {
Telegram.WebApp.sendData("/time");
});
This demonstrates how the Basic Telegram Bot template can be extended with new features on both the bot and web app sides.
Created: | Last Updated:
Here's a step-by-step guide for using the Basic Telegram Bot template:
Introduction
The Basic Telegram Bot template provides a starting point for creating a Telegram mini app and bot. This template includes a simple web interface and a Telegram bot that can be easily customized to suit your needs.
Getting Started
To begin using this template:
- Click the "Start with this Template" button in the Lazy Builder interface.
Initial Setup
Before running the bot, you need to set up a Telegram Bot API Token:
- Open Telegram and search for the BotFather (@BotFather).
- Start a chat with BotFather and send the command
/newbot
. - Follow the prompts to create a new bot. Choose a name and username for your bot.
- Once created, BotFather will provide you with an API token. Copy this token.
- In the Lazy Builder, go to the "Env Secrets" tab.
- Find the
TELEGRAM_API_TOKEN
secret and paste your API token as its value.
Test the Application
After setting up the API token:
- Click the "Test" button in the Lazy Builder interface to deploy and run the application.
- The Lazy CLI will appear, showing the deployment progress.
Using the Bot
Once the bot is running:
- The CLI will display a message with your bot's username and a link to start chatting with it.
- Click the link or search for your bot's username in Telegram to start a conversation.
- Send the
/start
command to the bot. It will respond with a message and a button to open the web app.
Using the Web App
The web app provides a simple interface for users:
- Click the "Open Web App" button in the Telegram chat to launch the web app.
- The app will open within Telegram, showing a basic interface.
- You can navigate to the Profile page to see your Telegram user information.
Customizing the App
To customize the app for your needs:
- Modify the
index.html
file to add your desired functionality to the main page. - Update the app name in the
header.html
file to reflect your bot's name. - Add new routes or modify existing ones in the
web_app_routes.py
file to create new pages or API endpoints. - Customize the bot's responses by modifying the handler functions in
main.py
.
Integrating the App
This template creates a standalone Telegram bot and mini app. There's no need for additional external integration steps. Users can interact with your bot directly through Telegram and access the web app via the bot's interface.
Remember to thoroughly test your bot and web app functionality after making any changes. You can always redeploy your app using the "Test" button in the Lazy Builder to see your updates in action.
Template Benefits
-
Rapid Telegram Bot Development: This template provides a ready-to-use structure for creating Telegram bots, significantly reducing development time and allowing businesses to quickly deploy custom bot solutions.
-
Seamless Web App Integration: The template includes a Flask-based web application that integrates smoothly with Telegram, enabling businesses to create rich, interactive experiences within the Telegram platform.
-
Enhanced User Engagement: By combining bot functionality with a web interface, businesses can offer a more engaging and versatile user experience, potentially increasing user retention and interaction rates.
-
Scalable Database Integration: The template includes SQLAlchemy setup, allowing for easy database integration and management, which is crucial for businesses that need to store and process user data or transaction information.
-
Customizable User Interface: With a modular HTML/CSS/JS structure, businesses can easily customize the look and feel of their Telegram mini app to match their brand identity and specific requirements, enhancing brand consistency across platforms.