Sending a message from Python to a Telegram chat: the simplest way – Telegram Group

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…

core.telegram.org

Ten articles before and after

Creating a Telegram Bot: Full Beginner’s Guide 2021 – Telegram Group

.Net Core Console Uygulması İle Telegram Botu Geliştirmek – Telegram Group

Como criar um Bot no Telegram com NodeJS – Telegram Group

data-rh=”true”>Airdrop 20 WYD ($50) – ALVIN LAGONERO – Medium – Telegram Group

Telegram Bot in JAVA| by Naveen Kumar M N – Telegram Group

Create a telegram bot – Telegram Group

Bot Telegram Untuk Membantu Sekolah/Kuliah Kamu – Telegram Group

Пишем telegram бота для мониторинга сайта на Golang – Telegram Group

Пошаговый мануал: Как создать бота в Телеграм? – Telegram Group

Python ile Telegram Chatbot + Heroku – Telegram Group