Menu

1 / 2

Build a Telegram Bot with Python: Complete 2024 Guide

DEV Community·Brad·6 months ago
#ldKaB04N
#python#telegram#bot#tutorial#token
Reading 0:00
15s threshold

Build a Telegram Bot with Python: Complete 2024 Guide

Telegram bots are useful for notifications, automation triggers, and team tools.

Setup

pip install python-telegram-bot

Enter fullscreen mode

Exit fullscreen mode

Create your bot by messaging @botfather on Telegram, run /newbot, and get your token.

Basic Bot

from telegram import Update from telegram.ext import Application , CommandHandler , MessageHandler , filters , ContextTypes

TOKEN = " your-bot-token "

async def start ( update : Update , context : ContextTypes . DEFAULT_TYPE ): await update . message . reply_text ( " Hello! I am your automation bot. " )

async def handle_message ( update : Update , context : ContextTypes . DEFAULT_TYPE ): text = update . message . text await update . message . reply_text ( " You said: " + text )

app = Application . builder (). token ( TOKEN ). build () app . add_handler ( CommandHandler ( " start " , start )) app . add_handler ( MessageHandler ( filters . TEXT & ~ filters . COMMAND , handle_message )) app .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.