DEV Community

Alex Spinov
Alex Spinov

Posted on

Telegram Bot API — Build a Bot in 15 Minutes With Python (No Framework Needed)

Telegram bots are everywhere — from weather updates to payment processing. And building one takes 15 minutes.

No framework. No library. Just the requests module and the Telegram Bot API.


Step 1: Create Your Bot (2 minutes)

  1. Open Telegram, search for @BotFather
  2. Send /newbot
  3. Choose a name and username
  4. Copy the API token

That is it. Your bot exists.


Step 2: Send a Message

import requests

TOKEN = 'your_bot_token'
BASE = f'https://api.telegram.org/bot{TOKEN}'

def send_message(chat_id, text):
    requests.post(f'{BASE}/sendMessage', json={
        'chat_id': chat_id,
        'text': text,
        'parse_mode': 'Markdown'
    })

# Send to yourself (get your chat_id first)
send_message('YOUR_CHAT_ID', 'Hello from my Python bot!')
Enter fullscreen mode Exit fullscreen mode

How to get your chat_id: Send any message to your bot, then visit:

https://api.telegram.org/bot{TOKEN}/getUpdates
Enter fullscreen mode Exit fullscreen mode

Your chat_id is in the response.


Step 3: Receive Messages (Polling)

import time

def get_updates(offset=0):
    response = requests.get(f'{BASE}/getUpdates', params={
        'offset': offset,
        'timeout': 30
    })
    return response.json().get('result', [])

def handle_message(message):
    chat_id = message['chat']['id']
    text = message.get('text', '')

    if text == '/start':
        send_message(chat_id, 'Welcome! Send me anything and I will echo it back.')
    elif text == '/help':
        send_message(chat_id, 'Commands:\n/start - Start\n/help - Help\n/time - Current time')
    elif text == '/time':
        from datetime import datetime
        send_message(chat_id, f'Server time: {datetime.now().strftime("%H:%M:%S")}')
    else:
        send_message(chat_id, f'You said: {text}')

# Main loop
offset = 0
while True:
    updates = get_updates(offset)
    for update in updates:
        offset = update['update_id'] + 1
        if 'message' in update:
            handle_message(update['message'])
Enter fullscreen mode Exit fullscreen mode

This is long polling — your bot asks Telegram "any new messages?" every 30 seconds.


Useful Bot Features

Send Photos

def send_photo(chat_id, photo_url, caption=''):
    requests.post(f'{BASE}/sendPhoto', json={
        'chat_id': chat_id,
        'photo': photo_url,
        'caption': caption
    })
Enter fullscreen mode Exit fullscreen mode

Send Documents

def send_document(chat_id, file_path):
    with open(file_path, 'rb') as f:
        requests.post(f'{BASE}/sendDocument', 
            data={'chat_id': chat_id},
            files={'document': f}
        )
Enter fullscreen mode Exit fullscreen mode

Inline Keyboard

def send_with_buttons(chat_id, text, buttons):
    keyboard = {
        'inline_keyboard': [
            [{'text': btn['text'], 'callback_data': btn['data']} for btn in row]
            for row in buttons
        ]
    }
    requests.post(f'{BASE}/sendMessage', json={
        'chat_id': chat_id,
        'text': text,
        'reply_markup': keyboard
    })

# Usage
send_with_buttons(chat_id, 'Choose:', [
    [{'text': 'Option A', 'data': 'a'}, {'text': 'Option B', 'data': 'b'}]
])
Enter fullscreen mode Exit fullscreen mode

Real Bots I Have Built

  1. Server monitor — sends alert when CPU > 90% or disk > 80%
  2. Price tracker — scrapes product pages, sends message when price drops
  3. Daily digest — sends summary of metrics every morning at 9am
  4. Reminder bot — parse natural language ("remind me in 2 hours to call John")

All under 100 lines of Python each.


Tips

  1. Use long polling for simple bots — no server needed
  2. Use webhooks for production — more efficient, requires HTTPS
  3. Bot messages have no cost — send as many as you want
  4. Rate limit: 30 messages/second — plenty for most use cases
  5. Inline mode — lets users trigger your bot from any chat

What bot would you build?

Telegram bots can do almost anything — from payments to games to AI assistants. What is the one bot you wish existed?


I write about APIs, Python automation, and developer tools. Follow for weekly tutorials.

More: Notion API | 5 APIs I Use Every Week


Need Custom Data Solutions?

I build web scrapers, API integrations, and data pipelines. 77+ production scrapers serving thousands of requests daily.

📧 spinov001@gmail.com — Describe your data need, get a solution.

Explore my open-source tools and ready-to-use scrapers on Apify.


More from me: 10 Dev Tools I Use Daily | 77 Scrapers on a Schedule | 150+ Free APIs

Top comments (0)