A Step-by-Step Guide on Creating a Python Telegram Bot

Photo by Adem AY on Unsplash

A Step-by-Step Guide on Creating a Python Telegram Bot

Table of contents

The popular messaging app Telegram is well-known for its feature-rich bot API, which enables developers to make potent and engaging bots. In this article, we'll show you how to use Python to build your own Telegram bot. This article will help you get started, whether you want to create a bot for entertainment purposes, automate activities, or communicate with users

Prerequisites

  1. Python Installed: You'll need Python 3. x installed on your computer

  2. Telegram Account: Ensure you have a Telegram account, as you'll need it to create and manage your bot

Step 1: Create a New Bot on Telegram

  • Open the Telegram app and search for "@BotFather."

  • Start a chat with BotFather and send the command "/newbot" to create a new bot

  • Follow the prompts to choose a name and username for your bot.

  • Once your bot is created, BotFather will provide you with a unique API token. Keep this token safe; you'll need it in the next steps.

Step 2: Install the Python Telegram API Library

  • The "python-telegram-bot" package will be used for interaction with the Telegram API.
pip install python-telegram-bot==13.3

Step 3: Write Python Code to Create the Bot

Here's a simple Python script to create your Telegram bot:

import telegram
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, ConversationHandler

# Replace 'YOUR_API_TOKEN' with the token you received from BotFather
TOKEN = 'YOUR_API_TOKEN'

# Initialize the bot
bot = telegram.Bot(token=TOKEN)
updater = Updater(token=TOKEN, use_context=True)
dispatcher = updater.dispatcher

# Define a function to handle the /start command
def start(update, context):
    context.bot.send_message(chat_id=update.effective_chat.id, text="Hello! I'm your Telegram bot.")

# Register the /start command handler
start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)

# Start the bot
updater.start_polling()

Replace 'YOUR_API_TOKEN' with the API token you received from BotFather.

Step 4: Run Your Bot

python bot.py

Testing Your Bot:

Open Telegram, search for your bot's username (the one you set during the bot creation process), and start a chat with it. Your bot should now be up and running, and it will respond to the "/start" command with a greeting message.

Customize and Expand Your Bot

you can use the bot for displaying information as well as Automation using Rest APIs

In the following example, we are displaying information on cryptocurrency by using the API and user input

# Define a function to handle user messages
def echo(update, context):
    user_input = update.message.text  # Get the user's message
    url = "https://api.coincap.io/v2/assets/"
    user_input = update.message.text
    response = requests.get(url + user_input)
    data = json.loads(response.text)
    context.bot.send_message(chat_id=update.effective_chat.id, text=data)

# Register the message handler
message_handler = MessageHandler(Filters.text & ~Filters.command, echo)
dispatcher.add_handler(message_handler)

To make your bot more interactive and useful to users, you can start with basic capabilities and continually add more functions. The possibilities are endless, whether you want to automate processes, provide information, or have natural language conversations.

Did you find this article valuable?

Support Niraj Patharkar by becoming a sponsor. Any amount is appreciated!