Skip to content

Instantly share code, notes, and snippets.

@iAnatoly
Last active May 24, 2022 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iAnatoly/cada7d41c577438d01b8f6afebf83367 to your computer and use it in GitHub Desktop.
Save iAnatoly/cada7d41c577438d01b8f6afebf83367 to your computer and use it in GitHub Desktop.
send alert messages to a telegram channel.
  1. create bot by messaging @botfather (/newbot)
  2. save a token in env var: export token="yourtokenfrombotfather". If you need to re-issue a token, use /revoke command to botfather.
  3. add your bot to a channel you intend for alerts
  4. Send some message into that channel using telegram client. A simple "test" will do.
  5. get chat id: curl https://api.telegram.org/bot${token}/getUpdates | grep test
  6. send a test message: curl https://api.telegram.org/bot${token}/sendMessage --data '{"chat_id":${chat_id},"text": "message"}' -H "Content-type: application/json"
  7. 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment