From one older post “Create a job using crontab” I used into my script the service exim4/ssmtp as mail server using gmail, but as we’re trying to walk away of Google’s jaw, I switched my notification engine to Telegram by an API service(bot)… almost it’s a step forward ! :)
Requirement:
- Bot (Telegram)
- Web Browser
- Terminal(script)
I will assume you have already set up your Bot on Telegram with BotFather
When you have already created your Bot you can check by the API through a web browser
https://api.telegram.org/botxxxxxxxxx:YYYYYYYYYYYYYYYYYY/getMe
The output it will be like:
ok true
result
id xxxxxxxxx
is_bot true
first_name "bot_name"
username "bot_username"
can_join_groups true
can_read_all_group_messages false
supports_inline_queries false
Telegram API fields you should know:
- /botxxxxxxxxx:YYYYYYYYYYYYYYYYYY/ the identity of your bot
- chat_id=xxxxxxxxxx this is the ID of your chat
The method to use is sendMessage from the Telegram official page we can see that the structure to send it is the following(by curl):
eg: From bot to me(private)
curl 'https://api.telegram.org/botxxxxxxxxx:YYYYYYYYYYYYYYYYYY/sendMessage?chat_id=357214926&text=hello_this_is_a_test'
eg: From bot to a group
curl 'https://api.telegram.org/botxxxxxxxxx:YYYYYYYYYYYYYYYYYY/sendMessage?chat_id=-380927924&text=hello_this_is_a_test'
Below you will see a little script I made to wrap the message and call the job to sender.
Messenger script:
#!/bin/bash
GROUP_ID=-xxxxxxxxxxxx
BOT_TOKEN=zzzzzzzzzzzzz:YYYYYYYYYYYYYYYY
curl -s --data "text=$1" --data "chat_id=$GROUP_ID" 'https://api.telegram.org/bot'$BOT_TOKEN'/sendMessage' > /dev/null
sh ./home/scripts/my_messenger ""
Testing command:
sh ./home/scripts/my_messenger "Hello, I'm a bot"
Here we got our own bot as sender, also you can use other method (getUpdates) described into the official Telegram documentation where you can send tasks to your bot.