- Create a new Telegram bot:
- start a chat with BotFather;
- type
/newbot, select a name (to be shown in chats) and handle for your bot; - note the bot token to access HTTP API - a long string with a colon in the middle (later referred to as
<token>); - optionally
/setdescriptionand/setuserpic.
- Add the bot to the chat/channel and note its id:
- add the bot to a new or existing chat or group - this is where your bot will send notifications to;
- go to
https://api.telegram.org/bot<token>/getUpdates(replace<token>with your token); - within the
chatpart, find and note theidfield - a positive or negative number (later referred to as<chat_id>).
- Programmatically send notifications:
- Essentially, you just need to construct a special URL and open it;
- For example, this can be done like this in Python (replace
<token>,<chat_id>and<message>):Depending on your version of python,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)
urllib.parse.quote_pluscould beurllib.quote_plus. - 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
thank you. I Wil test it