How To Send Telegram Notifications with Python

Published on July 15, 2025

Tutorial: Setting Up Telegram Notifications with Python (One-on-One Chat)

This tutorial guides you through sending automated notifications to a one-on-one chat with your Telegram bot using Python. You’ll create a bot, obtain the chat ID for a direct conversation (which will be a positive integer), and write a Python script to send messages.

Prerequisites

  • A Telegram account and the Telegram app installed
  • Python installed on your machine
  • A code editor (e.g., Visual Studio Code)
  • Basic knowledge of Python and APIs

Step 1: Create a New Telegram Bot

  1. Open Telegram and search for "BotFather".
  2. Click "Start" to interact with BotFather.
  3. Send the command /newbot to create a new bot.
  4. Follow the prompts to name your bot and set a username ending with "Bot".
  5. BotFather will provide a bot token (e.g., 123456789:ABCDEF...). Copy this token and keep it secure.

Step 2: Start a Direct Conversation with Your Bot

  1. Search for your bot in Telegram using its username.
  2. Click "Start" to begin a one-on-one conversation. This activates the bot and allows you to retrieve the chat ID for your direct chat.

Step 3: Retrieve the Chat ID for One-on-One Chat

To send messages, you need the chat ID for your one-on-one conversation. In Telegram, one-on-one chat IDs are positive integers matching the user’s Telegram ID.

Here’s how to get it:

  1. After starting a conversation (Step 2), send a message to your bot (e.g., /start or "Hi").
  2. Retrieve updates via the Telegram API:
  3. In a browser, enter this URL, replacing <YourBOTToken> with your bot token:
https://api.telegram.org/bot<YourBOTToken>/getUpdates

Example:

https://api.telegram.org/bot123456789:ABCDEF.../getUpdates

Examine the JSON response to find the chat ID:

Look for a "chat" object with type set to "private". The id will be a positive integer. Example:

{
    "update_id": 8393,
    "message": {
        "message_id": 3,
        "from": {
            "id": 987654321,
            "first_name": "John"
        },
        "chat": {
            "id": 987654321,
            "first_name": "John",
            "type": "private"
        },
        "date": 25497,
        "text": "/start"
    }
}

Key Fields:

  • chat.id: The chat ID (e.g., 987654321), a positive number matching the user’s ID.
  • chat.type: Confirms it’s "private" for a one-on-one chat.

Copy the chat.id value (e.g., 987654321) for later use.

Note: Since this is a one-on-one chat, the bot isn’t added to a group, and the chat ID will always be positive.

Step 4: Set Up Your Python Environment

  1. Open your code editor and create a file named telegram_notifier.py.
  2. Install the requests library for API calls:
  3. Run in your terminal:
pip install requests

Step 5: Create the Send Message Function

Store your bot token and chat ID in a configuration file:

  1. Create config.py and add:
# config.py
TOKEN = 'your_bot_token_here'  # Replace with your token from Step 1
CHAT_ID = 'your_chat_id_here'  # Replace with your chat ID from Step 3 (e.g., 987654321)

Write the message-sending function in telegram_notifier.py:

Add:

import requests
from config import TOKEN, CHAT_ID

def SEND_MESSAGE(message):
    url = f"https://api.telegram.org/bot{TOKEN}/sendMessage?chat_id={CHAT_ID}&text={message}"
    response = requests.get(url).json()
    print(response)
    print("Telegram Message Sent!")

Step 6: Test Your Setup

Add a test message to telegram_notifier.py:

Append:

if __name__ == "__main__":
    SEND_MESSAGE("Hello, this is a test message from Python!")

Run the script:

In your terminal:

python telegram_notifier.py

Check your one-on-one chat with the bot in Telegram for the message.

Additional Notes

  • Security: Keep config.py private to protect your bot token.
  • Customization: Modify SEND_MESSAGE for advanced features (see Telegram Bot API).
  • Purpose: Use this setup for personal notifications or alerts in a direct chat.

This tutorial is designed for one-on-one chats only, ensuring a straightforward process with a positive chat ID.