Telegram is a messaging platform similar to Whatsapp, but you can create bots and use an API to send messages from devices to yourself.
I have been experimenting with using Telegram bots to send notifications and alerts instead of emails.
I thought I'd try it out on the Onion and see what happens! You can also send commands through the bot too although I havnt ventured down that path ie. send a "/lighton" command and the omega switches a specific GPIO pin to 1 to turn a light on.
You should be able to follow the sequence below to get started:
-
Browse to https://telegram.org and create an account OR download the app on your iDevice or similar and create an account that way. If you have created an account on your device you can login to a web session on the telegram website to send messages via your browser also.
-
You must get your unique CHATID for your user. Send /getid
to @IDBot, it needs to be this specific command to get the answer. This will give you your CHATID which we will use later.
-
Next create a bot! Send /help
to @BotFather, next to create a bot send /newbot
. The BotFather will then ask a couple of questions, first a Name that is just a label and then the username for the bot which must end in Bot. He will send you the API token which again you must keep, although you can ask him to generate another by using the /token
message. You should notice there are a load of commands you can ask of the BotFather, I mainly use the /setuserpic
which you can use to change the bots avatar!
-
Next is to install curl which is used to create the HTTP requests, in this case a POST.
opkg update
opkg upgrade
opkg install curl
- Once curl is installed we can start sending messages! Copy pasta the following to setup the script file:
cd /root
touch telegram.sh
chmod 775 telegram.sh
- Open the file using your favourite text editor (I enjoy VI as it is on version of linux!) Copy the below text to create a script file that is used to send a message:
#!/bin/sh
#
# This script sends whatever is piped to it as a message to the specified Telegram bot
#
message=$( cat )
apiToken=<PLACE API TOKEN HERE>
# example:
# apiToken=123456789:AbCdEfgijk1LmPQRSTu234v5Wx-yZA67BCD
userChatId=<PLACE CHATID HERE>
# example:
# userChatId=123456789
sendTelegram() {
curl -s \
-X POST \
https://api.telegram.org/bot$apiToken/sendMessage \
-d text="$message" \
-d chat_id=$userChatId
}
if [[ -z "$message" ]]; then
echo "Please pipe a message to me!"
else
sendTelegram
fi
- Once the script is setup and you have modified it to have your CHATID and bot API token then use it!
echo "Test: Onion Omega FTW!!" | ./telegram.sh
You should get back a return of a valid message from the console and on whatever platform you have Telgram installed on!
One problem I have found is the pipe validation. Within bash it echos a response if nothing is piped. The same bit of code doesnt seem to work for busybox. If anyone has got a better way of doing it let me know!
Hopefully this will get some people thinking of some better ways of notifying people of events!
Hopefully @Boken-Lin will get it added to the Wiki if it is worthy!