How to send messages to my Telegram bot from my python script

Published on Aug. 22, 2023, 12:12 p.m.

I have a python script in which logs messages .I want those messages to be sent to my bot.

Make A Telegram Bot Using BotFather for the BotFather by opening it in your Telegram client.A pre-loaded chat bot that helps users make original teh bots to create a new bot, enter /newbot.Name and username your bot.You can use the tokens to create your new chat bot.Please note that anyone who gets your token has complete control over your Telegram bot. Also, do not upload it online.

To send messages using Python, we require the conversation ID that each chat in Telegram holds.

Send a message to your Telegram bot (any random message) Use this program to find your chat ID.

import requests
TOKEN = "YOUR TELEGRAM BOT TOKEN"
url = f"https://api.telegram.org/bot{TOKEN}/getUpdates"
print(requests.get(url).json())

The getUpdates function, which is invoked by this script.Our chat ID can be located in the JSON that was returned.

Copy the chat ID and paste it .

import requests
TOKEN = "YOUR TELEGRAM BOT TOKEN"
chat_id = "YOUR CHAT ID"
message = "hello from your telegram bot"
url = f"https://api.telegram.org/bot{TOKEN}/sendMessage?chat_id={chat_id}&text={message}"
print(requests.get(url).json()) # this sends the message

Run the Python script!