This a proof of concept created based on a need to get notified of completed runs or the like. This is a multi-user approach, meaning, that just like email lists, users can choose which notifications they would like to receive, but receive them through an IM application, in this case, Telegram. You will find references to all sources at the end of this post.
What is Telegram
Much like WhatsApp and Facebook Messenger, Telegram is "a cloud-based mobile and desktop messaging app with a focus on security and speed." Their focus on, specifically, security and privacy are very high.
Getting Started
First, you will need to set up Telegram on your device. Telegram supports pretty much everything but initially, you need to use your phone. Go to your App Store and get Telegram and setup your account. Secondly, install Telegram on your Desktop and make sure it is up and running before continuing. Once you are sure the Desktop version is up and running we need to create our Telegram bot using BotFather.
Creating our bot
The bot is where our notifications will be sent and anyone can join the bot channel until you restrict it. We will, for the purpose of this guide, not restrict it but feel free to add this step later. To create the bot, click on this link and open it in Telegram, https://telegram.me/botfather. Once you are talking to the BotFather perform the following steps:
- Send a “/newbot” message, then follow the instructions to set up a name and a username
- Your bot is now ready, be sure to save a backup of your API token, and correct, this API token is your
bot_token
Getting your Chat id
- On Telegram, search your bot (by the username you just created) or by going to , press the “Start” button or send a “/start” message
- Open a new tab with your browser, enter , replace <yourtoken> with your API token, press enter and you should see something like this:
{"ok":true,"result":[{"update_id":955696534, "message":{"message_id":4,"from":{"id":74xxxxxx,"is_bot":false,"first_name":"Petter","language_code":"en"},"chat":{"id":745094505,"first_name":"Petter","type":"private"},"date":1565178445,"text":"/help","entities [{"offset":0,"length":5,"type":"bot_command"}]}},{"update_id":955696535, "message":{"message_id":5,"from":{"id":74xxxxxx,"is_bot":false,"first_name":"Petter","language_code":"en"},"chat":{"id":745094505,"first_name":"Petter","type":"private"},"date":1565178479,"text":"/token","entities":[{"offset":0,"length":6,"type":"bot_command"}]}}]}
- Look for “id”, for instance, 745xxxxxx above is my chat id. Look for yours and put it as your bot_chatID in the code below.
import requests def telegram_bot_sendtext(bot_message): bot_token = '' bot_chatID = '' send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message response = requests.get(send_text) return response.json() test = telegram_bot_sendtext("My Test Text") print(test)
References
Telegram API: https://core.telegram.org/
Medium Post: https://medium.com/@ManHay_Hong/how-to-create-a-telegram-bot-and-send-messages-with-python-4cf314d9fa3e
Comments
0 comments
Please sign in to leave a comment.