Skip to content

Instantly share code, notes, and snippets.

@gourytch
Created July 1, 2015 07:09
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 gourytch/c58640999cb974001309 to your computer and use it in GitHub Desktop.
Save gourytch/c58640999cb974001309 to your computer and use it in GitHub Desktop.
simple telegram sender bot
#! /bin/bash
die() {
echo "EDIT YOUR ~/telegram_bot.rc AND RE-RUN!" 1>&2
exit 1
}
if [ ! -f $HOME/telegram_bot.rc ]; then
cat >$HOME/telegram_bot.rc <<_EOF_
#
# BOT_KEY='123456:enterYourTelegramBotAPIKeyHere'
#
BOT_KEY=''
#
# BOT_MASTER=your telegram id (ID! not your telephone!)
#
BOT_MASTER=
# REMOVE THIS LINE AND BELOW AFTER EDITING AND CHECKING
echo "YOU ARE LAZY! CHECK YOUR CONFIG!"
exit 1
_EOF_
die
fi
source $HOME/telegram_bot.rc
[ "x$BOT_KEY" = "x" -o "x$BOT_MASTER" = "x" ] && die
rawurlencode() {
# see
# http://stackoverflow.com/questions/296536/how-to-urlencode-data-for-curl-command
#
local string="${*}"
local strlen=${#string}
local encoded=""
for (( pos=0 ; pos<strlen ; pos++ )); do
c=${string:$pos:1}
case "$c" in
[-_.~a-zA-Z0-9] ) o="${c}" ;;
* ) printf -v o '%%%02x' "'$c"
esac
encoded+="${o}"
done
REPLY="${encoded}" #+or echo the result (EASIER)... or both... :p
}
message() {
rawurlencode "$@"
wget "https://api.telegram.org/bot${BOT_KEY}/sendMessage?chat_id=${BOT_MASTER}&text=${REPLY}" -O /dev/null &>/dev/null
}
msg="
TIMESTAMP: $(date +'%Y-%m-%d %H:%M:%S')
HOSTNAME : $(hostname)
UNAME : $(uname -a)
UPTIME : $(uptime)
--- LAST LOG ---
$(last)
--- DF --
$(df)
-- DONE --
"
message "$msg"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment