Telegram Bot Development: Complete Guide for Beginners

Esubalew ChekolEsubalew Chekol
3 min read

Introduction

With its user-friendly interface and extensive features, Telegram is a leading messaging platform. One of its standout capabilities is the support for bots. In this guide, we will explore how to build a Telegram bot step by step, using Python and the python-telegram-bot framework.


What is a Telegram Bot?

A Telegram bot is a program that interacts with Telegram users. Bots can automate tasks, provide information, and enhance user experience. They are useful for personal projects, businesses, or even entertainment purposes.


Getting Started with Telegram Bot API

To create a bot, you need to understand the Telegram Bot API, which facilitates communication between your bot and Telegram servers. Here's how to get started:


Setting Up a Telegram Bot

Step 1: Create a Telegram Account

If you don’t have a Telegram account yet, download the app and sign up. Your account will act as the administrator for your bot.

Step 2: Talk to BotFather

  1. Open Telegram and search for @BotFather.

  2. Start a chat and use the /newbot command to create a bot.

  3. Follow the instructions to name your bot and assign it a username (ending in bot).

  4. Save the API token that BotFather provides; you'll need it to interact with your bot.


Basic Concepts of Telegram Bots

With your bot ready, let’s dive into its functionality using Python. Here’s a simple example:


Receiving Commands

from telegram import Update
from telegram.ext import Updater, CommandHandler, CallbackContext

def start(update: Update, context: CallbackContext):
    update.message.reply_text('Hello! I am your Telegram Bot.')

def main():
    # Replace YOUR_API_TOKEN with the token from BotFather
    updater = Updater('YOUR_API_TOKEN')
    dispatcher = updater.dispatcher

    # Add a handler for the /start command
    dispatcher.add_handler(CommandHandler('start', start))

    # Start the bot
    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
    main()

Run this script to create a bot that responds with a welcome message when the /start command is used.


Sending Messages

Add the ability to send messages to users with the following code:

def send_message(update: Update, context: CallbackContext):
    chat_id = update.message.chat_id
    context.bot.send_message(chat_id, text='This is a custom message from your bot!')

# Add the handler for a custom command
dispatcher.add_handler(CommandHandler("send", send_message))

When users type /send, the bot replies with a custom message.


Expanding Your Bot’s Capabilities

The python-telegram-bot library enables advanced features for bots. Let’s look at a few examples:


Handling Inline Queries

Inline queries let users interact with your bot directly in any chat. Here’s an example:

from telegram import InlineQueryResultArticle, InputTextMessageContent
from telegram.ext import InlineQueryHandler

def inline_query(update: Update, context: CallbackContext):
    query = update.inline_query.query

    results = [
        InlineQueryResultArticle(
            id='1',
            title='Say Hello',
            input_message_content=InputTextMessageContent('Hello, world!'),
        )
    ]

    update.inline_query.answer(results)

# Add the inline query handler
dispatcher.add_handler(InlineQueryHandler(inline_query))

This enables your bot to respond to inline queries with predefined content.


Adding Custom Commands

Bots often have multiple commands for user interactions. Here’s an example of adding a /help command:

def help_command(update: Update, context: CallbackContext):
    update.message.reply_text('Here are the available commands:\n/start - Start the bot\n/help - Get help')

dispatcher.add_handler(CommandHandler("help", help_command))

This allows users to type /help and see a list of commands your bot supports.


Using Webhooks

For better performance, you can use webhooks instead of polling. Update your bot with a webhook URL as follows:

updater.start_webhook(
    listen='0.0.0.0',
    port=8443,
    url_path='YOUR_API_TOKEN',
    webhook_url='https://yourdomain.com/YOUR_API_TOKEN'
)

Make sure your server has HTTPS enabled for webhooks to function properly.


Deploying Your Bot

Hosting Options

  1. Local Hosting: Suitable for development. Use tools like ngrok to expose your local server.

  2. Cloud Platforms: Deploy your bot on platforms like Heroku, AWS, or Google Cloud for production use.

Example Deployment on Heroku

  1. Create a Procfile with the following content:

     worker: python your_bot_script.py
    
  2. Install the Heroku CLI and deploy your bot.


Conclusion

Congratulations! You’ve created a Telegram bot, from setup to advanced features. Bots are a powerful way to interact with users and automate tasks. Explore the official python-telegram-bot documentation for more features, and keep building awesome bots!

0
Subscribe to my newsletter

Read articles from Esubalew Chekol directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Esubalew Chekol
Esubalew Chekol

Experienced Developer in Python, Javascript, Django, Node.js React, React Natvie Jetpack Compose and Telegram bot development.