Cara Membuat Bot Telegram dengan Python: Panduan Lengkap 2026 - Elpsy AI
Lompat ke konten Lompat ke sidebar Lompat ke footer

Cara Membuat Bot Telegram dengan Python: Panduan Lengkap 2026

Telegram Bot adalah salah satu cara paling efektif untuk mengotomasi pesan, membangun layanan, atau bahkan menghasilkan uang. Dengan Python, lo bisa buat bot dari nol dalam hitungan menit.

Artikel ini akan bawa lo step-by-step: dari buat bot di Telegram sampai deploy ke server.

Telegram Bot Development

Apa Itu Telegram Bot?

Bot Telegram adalah akun Telegram yang dijalankan oleh program, bukan manusia. Bot bisa:

  • Membalas pesan otomatis
  • Mengirim notifikasi
  • Mengelola grup/channel
  • Menerima pembayaran
  • Menjalankan perintah khusus

Step 1: Buat Bot via BotFather

  1. Buka Telegram → cari @BotFather
  2. Kirim /newbot
  3. Masukkan nama bot (misal: MyTutorialBot)
  4. Masukkan username bot (harus unik, misal: mytutorial2026_bot)
  5. BotFather akan kasih API Token — simpan baik-baik
Contoh token: 7123456789:AAH...ABC123

Step 2: Install Library Python

pip install python-telegram-bot==21.0

Step 3: Tulis Bot Pertama

from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
import os

TOKEN = os.getenv('BOT_TOKEN')

async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
    await update.message.reply_text(
        f'Halo {update.effective_user.first_name}! Saya bot sederhana.'
    )

async def help_cmd(update: Update, context: ContextTypes.DEFAULT_TYPE):
    await update.message.reply_text(
        '/start - Mulai bot
/help - Bantuan
/info - Info bot'
    )

async def info(update: Update, context: ContextTypes.DEFAULT_TYPE):
    await update.message.reply_text(
        'Bot ini dibuat dengan python-telegram-bot v21'
    )

app = ApplicationBuilder().token(TOKEN).build()
app.add_handler(CommandHandler("start", start))
app.add_handler(CommandHandler("help", help_cmd))
app.add_handler(CommandHandler("info", info))

print("Bot berjalan...")
app.run_polling()

Step 4: Jalankan Bot

# Simpan token sebagai environment variable
export BOT_TOKEN="7123456789:AAH...ABC123"
python bot.py

Buka Telegram → cari bot lo → ketik /start → bot akan membalas!

Step 5: Tambah Fitur

Menangani Pesan Non-Command

from telegram.ext import MessageHandler, filters

async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE):
    text = update.message.text
    await update.message.reply_text(f'Anda mengirim: {text}')

app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))

Mengirim Gambar

async def send_photo(update: Update, context: ContextTypes.DEFAULT_TYPE):
    await update.message.reply_photo(
        photo='https://example.com/image.jpg',
        caption='Ini gambar dari bot!'
    )

Step 6: Deploy ke Server

Untuk bot yang jalan 24/7, deploy ke VPS:

# Install di server
sudo apt update && sudo apt install python3-pip
pip3 install python-telegram-bot==21.0

# Jalankan dengan systemd (supaya auto-start)
sudo nano /etc/systemd/system/telegram-bot.service
[Unit]
Description=Telegram Bot
After=network.target

[Service]
User=ubuntu
ExecStart=/usr/bin/python3 /home/ubuntu/bot.py
Restart=always
RestartSec=5
Environment=BOT_TOKEN=your_token_here

[Install]
WantedBy=multi-user.target
sudo systemctl enable telegram-bot
sudo systemctl start telegram-bot
sudo systemctl status telegram-bot

Kesimpulan

Membuat bot Telegram sangat mudah dengan Python. Dari bot sederhana, lo bisa kembangkan jadi:

  • Bot e-commerce (order, bayar, tracking)
  • Bot notifikasi (monitoring server, harga crypto)
  • Bot customer service
  • Bot AI (integrasi ChatGPT API)

Kuncinya: mulai dari sederhana, iterasi terus.

Naka Rhythm Apakah suka anime disebut wibu

Posting Komentar untuk "Cara Membuat Bot Telegram dengan Python: Panduan Lengkap 2026"