Building Telegram Bot to send messages with Python

Who doesn’t like instant messages! I hope you do as much as I love to receive and send messages quickly. Building Telegram Bot to send messages with Python is very simple. Basically, this tutorial is divided into 2 parts, Building a Telegram bot user and writing a simple python code to send messages. This tutorial can also be extended to telegram groups. One should have a telegram account and python installed on their system.

Building a Telegram bot

To build or create a telegram bot, you should have a telegram account. The below steps should not take more than 10 minutes to create your first telegram bot. Let’s get started:

Step 1: Search for BotFather in the telegram search bar. For quick reference, follow the image below.

Step 2: Once you are on the botfather messages box, you will be provided with multiple options to create telegram bots. Type /newbot in the message area and hit enter.

Step 3: Next you will be prompted to choose your new username. Make sure you give your bot some relevant name, for the demo purpose we have given the name “testbot“.

Step 4: This part is important as you need to select your unique bot username. Going forward this unique username will be used to search your telegram bot in the search box.

Step 5: Note down the API token access code as we will use this code in python to send messages.

Step 6: Now you will be able to search your bot with unique username in telegram search box. Click on /Start button to start the conversation.

Step 7: Time to grab the bot user ID and to do that open the below URL in your browser with your API Token key which we generated in step 5.

https://api.telegram.org/botAPITOKENID/getUpdates

Step 8: Once you enter the URL, the response will be generated in JSON format. You need to copy the user ID from the message_id as shown below. In our case, the user ID is 831751234

{"ok":true,"result":[{"update_id":7605491254,
"message":{"message_id":2,"from":{"id":831751234"is_bot":false,"first_name":"Aj","language_code":"en"},"chat":{"id":831751234"first_name":"Aj","type":"private"},"date":1619898364,"text":"hi"}}

Python Code To Send Telegram Messages

Those who are new to python can install the latest version of python version 3.9. Once you install the python application, the next step is to create a new file “send_msg.py” and copy the below python code. A simple python function can be used to send automatic messages to telegram users.

import requests

def send_telegram_msg(bot_msg):

    token_id = "API Access Token ID"
    chat_id = "831751234"
    send_text = "https://api.telegram.org/bot" + token_id + "/sendMessage? 
                 chat_id="+chat_id + "&parse_mode=MarkdownV2&text=" + bot_msg

    response = requests.get(send_text)

    return response.json()

send_telegram_msg("Hey This is Awesome!")

Yay! Finally I have made it šŸ™‚

To further extend the usage of building Telegram Bot to send messages with Python, you should be able to use it in most of the real-time cases such as:

  1. Automate your daily tasks and use telegram as a scheduler
  2. Add/ delete/ greet users in groups
  3. Track your stocks with the latest updates
  4. News web scrapper to get latest updates on your telegram and many more
Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply