Build Your First Telegram Bot with Python — For Absolute Beginners

Welcome! If you know the basics of Python and you're looking to build something fun — you’re in the right place. By the end of this article, you'll have an echo bot — which I believe is the "Hello World" of Telegram bots. This article is written for absolute beginners. If you're a pro and find this too sweet — feel free to get outta here.

What is an Echo Bot ?

Guess it?... Yep. All this bot does is resend whatever the user sends to it. Pretty simple, right? — how do you eat an elephant? One bite at a time.

What will you need? (Prerequisites)

As mentioned earlier, you'll need a basic understanding of Python. If you can create and run a Python file, define a function and call a function — you will do just fine. You'll also need:

  • Python installed on your pc

  • A Telegram account

Create a Telegram Bot

To create a bot, you'll need @Botfather . If you already know how to use it, skip ahead. If you're still reading this section, don’t worry — I’m not going to walk you through it like you’re three years old. It's really easy. Just follow the steps:

  1. Open Telegram and search for Botfather. (Make sure the username is @BotFather and it has a verified badge.)

  2. Start the bot — you'll see a list of commands.

  3. Send /newbot and follow the instructions.

Getting Your Credentials

You already have the bot token from @Botfather. But you'll also need two more credentials:

  • API ID

  • API Hash

What are these? Don't worry about it. just follow the steps.

  1. Go to https://my.telegram.org/auth

  2. Enter the phone number linked to your Telegram account.

  3. Click Next and enter the code sent via Telegram (not SMS) and click Sign In.

  4. Click API development tools.

  5. Fill in the form — App Title and Short Name are enough.

  6. On the next screen, copy your API ID and API Hash.

Now store your Bot Token, API ID, and API Hash somewhere safe. Hell Yeah! We did the most stressful part.

Installing Pyrogram

If you don't know what Pyrogram is — you'll know. It's one of the best third-party libraries for building Telegram bots in python. Let's install it.

  • Open your terminal ( Command Prompt, Powershell — whatever you have ) and enter the following command.
pip install -U pyrogram tgcrypto
  • Then, create a folder wherever you need and create a python file ( main.py). I assume that you can do that. I believe in you — Believe me :)

Time to code

Alright! I don’t care about what you use to code — as long as you know what you’re doing, you're fine.

  1. First let's import pyrogram to our main.py. Don't cry in case you don't know to import libraries. Now you know.
from pyrogram import Client

Client is the main class which has the all available methods & types, filters, decorators, handlers, etc. If you are in my target audience, it's totally possible that you didn't get the previous sentence — nothing to worry.

  1. Now create a Client object. This sets up the connection to Telegram.
bot = Client(
    name='examplebot',           # pass whatever name (bot name, your name, etc)
    api_id=12345,                # pass your API_ID (as a int or str)
    api_hash='your api hash',    # pass your API_HASH (as a str)
    bot_token='your bot token'   # pass your BOT_TOKEN (as a str)
)
  1. Let's verify our work — We imported the Client class and created a object using it. To run our bot we have to call one of its main methods which is run()— Watch and learn.
bot.run()

What bot.run() do?... It's like saying, "Alright buddy, wake up and start listening to everything happening in the chat" .

Your full code so far should look like this:

from pyrogram import Client


bot = Client(
    name='examplebot',           # pass whatever name (bot name, your name, etc)
    api_id=12345,                # pass your API_ID (as a int or str)
    api_hash='your api hash',    # pass your API_HASH (as a str)
    bot_token='your bot token'   # pass your BOT_TOKEN (as a str)
)

bot.run()

What are you waiting for? — Run it.

python main.py

If the terminal stays blank — good news, it's running! You should also see a file like examplebot.session in your folder (name may vary). In case you didn't get the result above, you will have to find what you messed up by your own. Otherwise, press Ctrl + c to stop the bot and get back to code.

Add Echo Function

This is the main function of our bot — echoing. Here’s what the bot needs to do:

  • Listen for text messages sent to it. ( For the sake of simplicity, let's only focus only on text messages. )

  • Reply to those messages with the same text.

Simple, right?

We are going to use the Python decorator concept. Do you need to know how decorators work? No. Absolutely not. If you know — good for you.

@bot.on_message()
def echo(client,message):...

echo function is called every time someone sends a message to the bot. That's what's happening here.

What are client and message ?

  • client is your bot (the pyrogram.Client object)

  • message is the incoming message (a pyrogram.types.Message object)

Now let's echo the message. To that you need the text of the message, right? Here's how to do it.

    text = message.text

Want to see what else is in the message object? Just:

@bot.on_message()
def echo(client,message):
    print(message)
    text = message.text
    print(f'This is the text:{text}')

Let's send the text to the same chat or user as a reply.

    message.reply(text)

Final Code:

from pyrogram import Client


bot = Client(
    name='examplebot',           # pass whatever name (bot name, your name, etc)
    api_id=12345,                # pass your API_ID (as a int or str)
    api_hash='your api hash',    # pass your API_HASH (as a str)
    bot_token='your bot token'   # pass your BOT_TOKEN (as a str)
)


@bot.on_message()
def echo(client,message):
    text = message.text
    message.reply(text)

bot.run()

Now run it, and send a message to your bot. It should reply with the same message. Have fun!

Final Thoughts

If you learned something and had fun, looks like I’ve done something right. But I should say this — I didn’t write this article for any of you. I don’t really want to be just another random programming tutorial guy. I wrote this article because I wanted to build a writing habit. I want to write about anything I’m interested in, and I think it’s easier to start with something I completely understand. That’s the reason you have this article.
Again, if you got something from it — that’s a bonus for my effort. I’d love to hear your feedback.

8
Subscribe to my newsletter

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

Written by

Beenuka Hettiarachchi
Beenuka Hettiarachchi