Sending a message from Python to a Telegram chat: the simplest way
In this article, I will present the simplest way to send a message to a Telegram using Python.
To send the message you first have to:
- Create a Telegram BOT via BotFather
- Get your BOT_API_TOKEN key and CHAT_ID
$ pip install requests
Provided that you did the above, now you can send a message to your bot by issuing an HTTP GET or POST request to the Telegram BOT API at the following URL:
https://api.telegram.org/bot<BOT_API_TOKEN>/sendMessage?chat_id=<CHAT_ID>&text=<YOUR_MESSAGE_TEXT>
where:
- BOT_API_TOKEN is the API Key generated by BotFather when you created your bot
- CHAT_ID unique identifier for the target chat or username of the target channel (in the format
@channelusername
) - YOUR_MESSAGE_TEXT text of the message to be sent (URL-encoded)
All code in Python looks like this:
import requestsCHAT_ID = insert_your_chat_id
BOT_API_TOKEN = insert_your_bot_api_token
url = 'https://api.telegram.org/bot'message = 'Hello world!'
params = {'chat_id': CHAT_ID, 'text': message}
response = requests.get(url + BOT_API_TOKEN + '/sendMessage', params=params)
References:
Requests: HTTP for Humans™ – Requests 2.25.1 documentation
Release v2.25.1. ( Installation ) Requests is an elegant and simple HTTP library for Python, built for human beings…
requests.readthedocs.io
Telegram Bot API
The Bot API is an HTTP-based interface created for developers keen on building bots for Telegram.To learn how to create…
Ten articles before and after
Viber, WhatsApp and Telegram – new tendencies in the world of communication – Telgram.cn
How to restrict saving content on Telegram – Telegram hacks
25+ Best Telegram Dating Groups For Boys & Girls in 2022
Best Telegram Channels For Telugu Movies – Telgram.cn
How to Increase Telegram Channel Members Free (2022)
Bengali Telegram channels For Bengali Users – Telegram Channels -Telgram.cn
Best 15+ Telegram Animated Stickers In 2022 – Telegram Guide
Tamil Telegram Channels and Group Links – Telegram Channels