Skip to content

Instantly share code, notes, and snippets.

@lucarin91
Created April 20, 2020 13:25
Show Gist options
  • Save lucarin91/52b398a723bedaa11440c7fd9763c8df to your computer and use it in GitHub Desktop.
Save lucarin91/52b398a723bedaa11440c7fd9763c8df to your computer and use it in GitHub Desktop.
Simple sh script to send out log to a telegram chat.
#!/bin/sh
#####################################################
# CONFIGURATION #
#####################################################
TOKEN=1233445ASDKLSJDASASDDASBJHBEUBSAÀÒBDFASOFADBSDF
CHAT_ID=12312354
#####################################################
usage="\
Usage: $0 TITLE [MESSAGE]
If MESSAGE is empty stdin is used.\
"
# check input or exit
[ -z "$1" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ] && echo "$usage" && exit
title="$1"
# if message is empty read from stdin
message="$2"
if [ -z "$2" ]; then
while read -r line; do
message="$message$line\n"
done < /dev/stdin
fi
# build telegram message
msg="*LOG: $title*\n\`\`\`sh\n$message\`\`\`"
# send message
curl -s -XPOST "https://api.telegram.org/bot$TOKEN/sendMessage" \
-H "Content-Type: application/json" \
-d "{\"text\":\"$msg\", \"chat_id\":\"$CHAT_ID\", \"parse_mode\":\"MarkdownV2\"}" > /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment