How To Create A Simple Telegram Bot With Python

How To Create A Simple Telegram Bot With Python

In this article, we will be building a very simple telegram bot with python. Our telegram bot will be able to send a simple message. Now, let's get started.

Pre-requisites

  • Basic knowledge of python
  • A telegram account
  • A suitable code editor like Vs Code

Step 1: Getting Started

Let's create our telegram bot. To do this, we need to search for BotFather on our telegram account.

Screenshot (279).png

Once we have seen BotFather, we need to create our new telegram bot by sending the command "/newbot" to BotFather.

Screenshot (281).png

we need to choose a name for our bot, we can call it anything. I have decided to call mine "message_bot".

After this, we will be required to choose a username. I gave mine "steel_message_bot". The username for our bot must end with "bot".

After setting our username, we will be given token access by BotFather. This token access is a secret key and must not be revealed to anyone.

Token access allows us to communicate with our bots.

Step 2: Setup telegram with python

We need to install telegram on our local machine. Before we install all we need, we need to create a folder and name it something suitable like "Bot".

Inside the Bot folder, we need to install python-telegram-bot. We can do this by running the code below:


pip install python-telegram-bot

After this has been installed, we need to create a python script bot.py.

In our bot.py, we need to import telegram and every other thing required like Command Handler and Updater.

import telegram
from telegram.ext import Updater, CommandHandler

After importing this, we need to set up our bot token.


bot_token = "your_bot_token"

bot = telegram.Bot(token=bot_token)

Step 3: Create a function to send messages

After setting up our bot token, we need to write the python function that will send a message to the user.

def Start(update,context):
    chat_id = update.effective_chat.id
    first_name = update["message"]["from_user"]["first_name"]
    context.bot.send_message(chat_id=chat_id,text = f"Hello, {first_name}. I am your best  buddy.")

As seen above, we can send a message by using context.bot.send_message. This method takes in some parameters in which chat_id is required and not optional.

The next thing is to add the Start function as a command. We will add this command inside a function called main


def main():

    updater=Updater("your_bot_token",use_context=True)

    dp=updater.dispatcher

    dp.add_handler(CommandHandler("send",Send))

Step 4: Run your bot

The final step is to see if our bot is working whenever we type "/start". To do this we need to start our bot using the polling method.

We need to adjust our main function as seen below:

def main():
    updater=Updater("bot_token",use_context=True)

    dp=updater.dispatcher

    dp.add_handler(CommandHandler("start",Start))

    updater.start_polling()

    updater.idle()


if __name__ == '__main__':
    main()


`

After this, we need to run our bot from our terminal using python bot.py.

To check if our bot is working, let's send our command to it.

From BotFather's chat click on the bot's link, it looks like "t.me/steel_message_bot". Then we input our command "/start"

Screenshot (286).png

Conclusion

As seen above, we have just created a simple telegram bot that sends a simple message.

In the upcoming articles, we shall be creating a more advanced bot and we are going to host it on a webhook. Stay tuned ๐Ÿ˜Š๐Ÿ˜Š

ย