深度阅读

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

作者
作者
2023年08月22日
更新时间
12.76 分钟
阅读时间
0
阅读量

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!

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。