How to send a message to your Telegram channel by Python?
Without any preface, let’s get straight to the point. Consider you’re a Python programmer and you have a channel within Telegram. You want to write a code to send a message to the channel. Is it possible? Sure. In this topic I’ll show you how to do that.
Initially you must do 2 things:
1. Create a bot via official bot (ID: “BotFather”) withinTelegram and then adding it to your channel as an administrator
2. Using Telegram official API to send your message
First one is pretty simple and you can do it as the instruction has been said within the bot I’ve said.
But I want to show you how to use the Telegram official API in Python. You need to be aware of “requests” module in Python for sending requests to the URL I refer to.
Python Codes Structure
First import “requests” module:
import requests
Next declare a variable for your bot API. This API is unique and displayed when you create a new bot within the “BotFather”:
telegramApiKey = ‘YOUR_BOT_API’
Then use “requests” module for requesting to the URL below. This URL needs 2 parameters:
1. “chat_id” — Which is your channel ID
2. “text” — Which is your text you want to send to your channel
telegramResult = requests.get(f’https://api.telegram.org/bot{telegramApiKey}/sendMessage', params={
“chat_id”: “@YOUR_CHANNEL_ID”,
“text”: “YOUR_TEXT”
})
After that you can print the request output and see its status:
print(telegramResult.json())
Finally you can see your post in your channel you’ve sent by Python.
That’s it!
Ten articles before and after
Track SSH Logins via Telegram. If you are a Dev-Ops Engineer, You and… – Telegram 中文版
data-rh=”true”>新手指南(3)—玩 Airdrop (空投)事前準備,15分鐘搞掂開始賺幣 – KK教玩幣 – Medium – Telegram 中文版
Домашняя бухгалтерия в telegram. Отложенные счета. – Telegram 中文版
CryptoBot en Telegram con Python. Creación de un Bot de Telegram con… – Telegram 中文版
Setup Telegram bot app. Register a bot – Telegram 中文版
Tasker and Telegram integration: live location – Telegram 中文版
How to write UK Rent Telegram Bot – Telegram 中文版
Домашняя бухгалтерия в telegram. Электронные чеки. QR. – Telegram 中文版
Example of a telegram bot in python without using specific libraries – Telegram 中文版