by Lazy Sloth
Basic Telegram Bot
import os
import logging
from telegram import Update
from telegram.ext import (
Updater,
CommandHandler,
MessageHandler,
CallbackContext,
Filters,
)
TELEGRAM_API_TOKEN = os.environ['TELEGRAM_API_TOKEN']
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.WARNING)
def start(update: Update, context: CallbackContext) -> None:
update.message.reply_text("Hi! I am your BasicTelegramBot.")
def echo(update: Update, context: CallbackContext) -> None:
update.message.reply_text(update.message.text)
def main() -> None:
updater = Updater(TELEGRAM_API_TOKEN)
Basic Telegram Bot
Created: | Last Updated:
Introduction to the Basic Telegram Bot Template
Welcome to the Basic Telegram Bot template! This template is designed to help you create a simple Telegram bot that can respond to messages and commands. It's a great starting point for those who are new to building bots on the Telegram platform. The bot created with this template will be able to greet users and echo their messages. Let's get started with setting up your new Telegram bot using the Lazy platform.
Clicking Start with this Template
To begin using this template, simply click on the "Start with this Template" button. This will pre-populate the code in the Lazy Builder interface, so you won't need to copy, paste, or delete any code manually.
Initial Setup
Before you can test your bot, you'll need to set up an environment secret within the Lazy Builder. This secret will store your Telegram bot's API token securely.
- Go to the Telegram BotFather to create a new bot and receive your TELEGRAM_API_TOKEN.
- Copy the token provided by BotFather.
- In the Lazy Builder, navigate to the Environment Secrets tab.
- Create a new secret with the key `TELEGRAM_API_TOKEN` and paste the token you received from BotFather as the value.
With the environment secret set, your bot will be able to authenticate with the Telegram API.
Test: Pressing the Test Button
Once you have set up your environment secret, you can press the "Test" button. This will deploy your application and launch the Lazy CLI. There is no need for user input at this stage, as the bot does not require it to start.
Using the App
After pressing the "Test" button, Lazy will handle the deployment of your bot. Once the deployment is complete, Lazy will provide you with a dedicated server link. You can use this link to interact with your bot on Telegram.
To interact with your bot:
- Open Telegram and search for your bot by the username you set up with BotFather.
- Start a conversation with your bot by sending the `/start` command.
- Your bot will greet you with a message: "Hi! I am your BasicTelegramBot."
- Try sending any text message to your bot, and it will echo the same message back to you.
Integrating the App
If you wish to integrate this Telegram bot into an external service or frontend, you will need to consider the specific requirements of that service. For example, if you want to use the bot to send notifications from a website, you would need to set up a webhook or use the Telegram API to send messages from your site to the bot.
For detailed steps on how to integrate your bot with external services, you should refer to the Telegram Bot API documentation:
https://core.telegram.org/bots/api
Remember, the Lazy platform has taken care of the deployment process, so you can focus on building and integrating your bot without worrying about server setup or environment configuration.
Enjoy building your Basic Telegram Bot with Lazy!
Technologies
![Telegram](https://storage.googleapis.com/lazy-apps-images-bucket/technologies/52dffc834291988d8797782043116064_telegram.1024x1024.png)