This repository has been archived by the owner on Oct 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
sendtelegram.sh
executable file
·63 lines (47 loc) · 1.91 KB
/
sendtelegram.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash
function usage
{
if [ -n "$1" ]; then echo $1; fi
echo "Usage: sendtelegram [-v] [-c configfile] [-t token] [-i chatid] [-p parse mode] [-m message]"
exit 1
}
VERBOSE=0
while getopts ":c:t:i:p:m:v" opt; do
case "$opt" in
c) CONFIGFILE=$OPTARG ;;
t) TOKEN_ARG=$OPTARG ;;
i) CHATID_ARG=$OPTARG ;;
p) PARSEMODE_ARG=$OPTARG ;;
m) TEXT=$OPTARG ;;
v) VERBOSE=1 ;;
*) echo "Unknown param: $opt"; usage ;;
esac
done
# Test config file
if [ -n "$CONFIGFILE" -a ! -f "$CONFIGFILE" ]; then echo "Configfile not found: $CONFIGFILE"; usage; fi
# Check config file if given
if [ -n "$CONFIGFILE" ]; then . "$CONFIGFILE";
# Default config file ~/.telegramrc if it exists
elif [ -f /etc/telegramrc ]; then . /etc/telegramrc;
fi
# If TOKEN or CHATID were given in the commandline, then override that in the configfile
if [ -n "$TOKEN_ARG" ]; then TOKEN=$TOKEN_ARG; fi
if [ -n "$CHATID_ARG" ]; then CHATID=$CHATID_ARG; fi
# Verify parameters
if [ -z "$TOKEN" ]; then usage "Bot token not set, it must be provided in the config file, or on the command line."; fi;
if [ -z "$CHATID" ]; then usage "Chat ID not set, it must be provided in the config file, or on the command line."; fi;
if [ -z "$TEXT" ]; then usage "Message not set, it must be provided on the command line."; fi;
if [ ! -z $PARSEMODE_ARG ] && [[ "$PARSEMODE_ARG" != +(markdown|html) ]]; then usage "Parse mode must be either 'markdown' or 'html'."; fi;
# Sending to Telegram
URL="https://api.telegram.org/bot$TOKEN/sendMessage"
TIMEOUT=10
echo "Sending message '$TEXT' to $CHATID"
CMDARGS="chat_id=$CHATID&disable_web_page_preview=1&text=$TEXT"
if [ ! -z $PARSEMODE_ARG ]; then
CMDARGS=${CMDARGS}"&parse_mode=$PARSEMODE_ARG"
fi
CMD=`curl -s --max-time $TIMEOUT -d "$CMDARGS" $URL 2>&1`
if [ $? -gt 0 ]; then echo "Failed sending Telegram"
else echo "Done!"
fi
if [ "$VERBOSE" -eq 1 ]; then echo $CMD; fi