Create Telegram Bot to Send Message (Python)

Create Bot and Setup Channel

  • Install Telegram on Android
  • Visit FatherBot on Android
  • Create /newbot: A name and username/domain (e.g. YourNewBot) is required.
  • Copy down the Bot Token (e.g. 880123456:AAGe...); Keep your token secure and store it safely, it can be used by anyone to control your bot.
  • View YourNewBot at tg://resolve?domain=YourNewBot (BotFather will send you the message)
  • Use Android Telegram App to create a private/public channel
  • Add YourNewBot as Administrator of the Channel: Select Channel -> Click on the Title Bar -> Subscribers -> Add subscriber -> Key in Bot name and click -> Make Admin.

Get Chat ID of Private Channel

  • Open Telegram Web on Desktop
  • Open the Channel, check the URL: https://web.telegram.org/#/im?p=c1306123456_18441234567890123456. The Chat ID 1306123456, where you must prefex -100 to make it -1001306123456.

Send Message to Private Channel via Bot

https://api.telegram.org/botBOT_TOKEN/sendMessage?chat_id=CHAT_ID&text=MESSAGE
  • BOT_TOKEN: In FatherBot message after /newbot (e.g. 880123456:AAGe...)
  • CHAT_ID: Refer to Get Chat ID of Private Channel (e.g. -1001306123456)
  • MESSAGE

Python

import requestsimport jsonTELEGRAM_BOT_TOKEN = '880123456:AAGe...'TELEGRAM_CHAT_ID = '-1001306...'def send_message(message):    params = {        'chat_id': TELEGRAM_CHAT_ID,        'text': message    }    url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"    r = requests.get(url, params=params)    if r.status_code == 200:        print(json.dumps(r.json(), indent=2))    else:        r.raise_for_status()

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.