-
Notifications
You must be signed in to change notification settings - Fork 4
/
telegramSend.sh
executable file
·126 lines (119 loc) · 3.11 KB
/
telegramSend.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/bash
TG_SEND_CFG_FILE="telegram.conf"
LAST_MSG_ID_FILE="lastMsgId.txt"
# Colors
RED="\033[1;31m" # For errors / warnings
NC="\033[0m" # reset color
isFile=0
isPreview=1
isCite=0
isEdit=0
isPin=0
tmpDir="./"
while [[ $# -gt 1 ]]; do
case "$1" in
"--config")
if [[ "$2" != '' ]] && [[ -f "$2" ]]; then
TG_SEND_CFG_FILE="$2"
fi
shift 2
;;
"--file")
isFile=1
shift 1
;;
"--disable-preview")
isPreview=0
shift 1
;;
"--cite")
isCite=1
shift 1
;;
"--edit")
isEdit=1
shift 1
;;
"--pin")
isPin=1
shift 1
;;
"--unpin")
isPin=-1
shift 1
;;
"--tmp")
if [[ "$2" != '' ]] && [[ -d "$2" ]]; then
tmpDir="$2"
fi
shift 2
;;
--*|-*)
echo "Unknown flag ${1}"
shift 1
;;
esac
done
MSG="$1"
TOKEN="$(grep token "${TG_SEND_CFG_FILE}" | sed 's/token = //')"
CHAT_ID="$(grep chat_id "${TG_SEND_CFG_FILE}" | sed 's/chat_id = //')"
idFile="${tmpDir}${LAST_MSG_ID_FILE}"
[[ ! -f "$idFile" ]] && touch "$idFile"
mId=$(cat "${idFile}")
if [[ $isPin == -1 ]]; then
out=$(curl -s -X POST \
-H 'Content-Type: application/json' \
-d "{\"chat_id\": \"${CHAT_ID}\", \"message_id\": \"${mId}\"}" \
https://api.telegram.org/bot"${TOKEN}"/unpinChatMessage)
isOK=$(echo "$out" | jq -r .ok)
if [[ $isOK != "true" ]]; then
echo -e "${RED}Failed unpinning msg${NC}"
echo "$out" | jq
fi
exit 0
fi
if [[ $isFile == 1 ]]; then # no edit here!
params=""
[[ $isPreview == 0 ]] && params="&disable_web_page_preview=True"
[[ $isCite == 1 ]] && params="${params}&reply_to_message_id=${mId}"
out=$(curl -s -F document=@"${MSG}" \
https://api.telegram.org/bot$TOKEN/sendDocument?chat_id="${CHAT_ID}""${params}")
isOK=$(echo "$out" | jq -r .ok)
if [[ $isOK != "true" ]]; then
echo -e "${RED}Failed sending file${NC}"
echo "$out" | jq
else
echo "$out" | jq -r .result | jq -r .message_id > "${idFile}"
fi
exit 0
fi
MSG="$(echo "$MSG" | sed 's/"/\\"/g')"
op="sendMessage"
[[ $isEdit == 1 ]] && op="editMessageText"
params="{\"chat_id\": \"${CHAT_ID}\", \"text\": \"${MSG}\", \"parse_mode\": \"HTML\""
[[ $isPreview == 0 ]] && params="${params}, \"disable_web_page_preview\": \"true\""
[[ $isCite == 1 ]] && params="${params}, \"reply_to_message_id\": \"${mId}\""
[[ $isEdit == 1 ]] && params="${params}, \"message_id\": \"${mId}\""
params="${params}}"
out=$(curl -s -X POST \
-H 'Content-Type: application/json' \
-d "${params}" \
https://api.telegram.org/bot"${TOKEN}/${op}")
isOK=$(echo "$out" | jq -r .ok)
if [[ $isOK != "true" ]]; then
echo -e "${RED}Failed sending msg${NC}"
echo "$out" | jq
else
echo "$out" | jq -r .result | jq -r .message_id > "${idFile}"
fi
if [[ $isPin == 1 ]]; then
out=$(curl -s -X POST \
-H 'Content-Type: application/json' \
-d "{\"chat_id\": \"${CHAT_ID}\", \"message_id\": \"$(cat "${idFile}")\", \"disable_notification\": \"true\"}" \
https://api.telegram.org/bot"${TOKEN}"/pinChatMessage)
isOK=$(echo "$out" | jq -r .ok)
if [[ $isOK != "true" ]]; then
echo -e "${RED}Failed pinning msg${NC}"
echo "$out" | jq
fi
fi