Basic Telegram Bot

Start with this template
112
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:
    update.message.reply_text("Hi! I am your BasicTelegramBot.")

def whats_up(update: Update, context: CallbackContext) -> None:
    update.message.reply_text("whats up?")

def main():
    if not TELEGRAM_API_TOKEN:
Get full code

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 greet users and respond to any text message with "whats up?".

Clicking Start with this Template

To begin using this template, simply click on the "Start with this Template" button. This will set up the template in your Lazy Builder interface, pre-populating the code so you can start customizing your bot right away.

Initial Setup

Before you can test and use your bot, you'll need to set up a TELEGRAM_API_TOKEN. This token is essential for your bot to communicate with the Telegram API. Here's how to obtain and set up your TELEGRAM_API_TOKEN:

  • Open the Telegram app and search for the BotFather or go to https://t.me/botfather.
  • Start a chat with BotFather and send the command /newbot.
  • Follow the instructions to set up your new bot, providing a name and username.
  • Once your bot is created, BotFather will provide you with a TELEGRAM_API_TOKEN.
  • Copy this token.
  • Set this value in the Environment Secrets tab within the Lazy Builder.

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

Test: Pressing the Test Button

After setting up your TELEGRAM_API_TOKEN in the Environment Secrets tab, you can press the "Test" button. This will deploy your application and start the bot. The Lazy CLI will handle the deployment, and no further action is required from you at this point.

Using the App

Once your bot is up and running, you can interact with it on Telegram. Send a "/start" command to your bot, and it will greet you with a message. Any text message you send to the bot will receive a "whats up?" response. This interaction showcases the bot's ability to handle commands and messages.

Integrating the App

If you wish to integrate this bot into an existing service or frontend, you may need to add the bot's server link provided by Lazy to your external tool. For example, if you have a website that needs to send notifications through your Telegram bot, you would use the server link as the endpoint for sending messages.

Remember, this bot is a starting point, and you can expand its capabilities by adding more commands and message handlers. Explore the Telegram Bot API documentation to learn more about what you can do with your bot.

Happy building with Lazy!

Technologies

Telegram Telegram