Skip to content

Instantly share code, notes, and snippets.

@dlaptev
Last active April 17, 2024 12:23
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save dlaptev/7f1512ee80b7e511b0435d3ba95d88cc to your computer and use it in GitHub Desktop.
Save dlaptev/7f1512ee80b7e511b0435d3ba95d88cc to your computer and use it in GitHub Desktop.
Programmatically send push notifications to telegram from python
  1. Create a new Telegram bot:
    1. start a chat with BotFather;
    2. type /newbot, select a name (to be shown in chats) and handle for your bot;
    3. note the bot token to access HTTP API - a long string with a colon in the middle (later referred to as <token>);
    4. optionally /setdescription and /setuserpic.
  2. Add the bot to the chat/channel and note its id:
    1. add the bot to a new or existing chat or group - this is where your bot will send notifications to;
    2. go to https://api.telegram.org/bot<token>/getUpdates (replace <token> with your token);
    3. within the chat part, find and note the id field - a positive or negative number (later referred to as <chat_id>).
  3. Programmatically send notifications:
    1. Essentially, you just need to construct a special URL and open it;
    2. For example, this can be done like this in Python (replace <token>, <chat_id> and <message>):
      import urllib, requests
      url = 'https://api.telegram.org/bot%s/sendMessage?chat_id=%s&text=%s' % (
          '<token>', '<chat_id>', urllib.parse.quote_plus('<message>'))
      _ = requests.get(url, timeout=10)
      Depending on your version of python, urllib.parse.quote_plus could be urllib.quote_plus.
    3. For options, refer to the official documentation.

To use telegram notifications with supervisor, modify /etc/supervisor/supervisord.conf to include the following lines (possible events):

[eventlistener:telegramnotitfactor]
command=curl "https://api.telegram.org/bot<token>/sendMessage?chat_id=<chat_id>&text=<message>"
events=PROCESS_STATE_STARTING
@tiagofrancafernandes
Copy link

thank you. I Wil test it

@vardanyan1
Copy link

great work thanks a lot.

@khaerulumam42
Copy link

just little update

change urllib.quote_plus to urllib.parse.quote_plus

the rest work for me, thank you

@dlaptev
Copy link
Author

dlaptev commented Feb 19, 2023

change urllib.quote_plus to urllib.parse.quote_plus

Thanks for the suggestion! Updated!

@baotang2118
Copy link

Available methods
All methods in the Bot API are case-insensitive. We support GET and POST HTTP methods. Use either URL query string or application/json or application/x-www-form-urlencoded or multipart/form-data for passing parameters in Bot API requests.
On successful call, a JSON-object containing the result will be returned.

I think it makes sense If we use POST request to send a message, in this case:

import requests
url = 'https://api.telegram.org/bot%s/sendMessage?chat_id=%s' % (
    '<token>', '<chat_id>')
_ = requests.post(url, json={'text': '<message>'}, timeout=10)

😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment