- create bot by messaging @botfather (/newbot)
- save a token in env var:
export token="yourtokenfrombotfather"
. If you need to re-issue a token, use /revoke command to botfather. - add your bot to a channel you intend for alerts
- Send some message into that channel using telegram client. A simple "test" will do.
- get chat id: curl https://api.telegram.org/bot${token}/getUpdates | grep test
- send a test message: curl https://api.telegram.org/bot${token}/sendMessage --data '{"chat_id":${chat_id},"text": "message"}' -H "Content-type: application/json"
- codify the above experience.
#!/bin/bash
set -eu -o pipefail
TOKEN="xxxxx:xxxxx"
URL="https://api.telegram.org/bot${TOKEN}/sendMessage"
HDR="Content-type: application/json"
RETVAL=-1
while [ $RETVAL -ne 0 ];
do
TEXT="`uname -a`\n\n`ip addr`"
PAYLOAD='{"chat_id": xxxxxx, "text": "'${TEXT}'"}'
# echo $PAYLOAD
curl -XPOST -H "$HDR" --data "$PAYLOAD" $URL
RETVAL=$?
[[ $RETVAL -ne 0 ]] && sleep 10
done