Skip to content

Commit 94efd3c

Browse files
committed
send Telegram files from sendTG
1 parent 18a2cc9 commit 94efd3c

1 file changed

Lines changed: 36 additions & 4 deletions

File tree

‎functions‎

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,17 @@ function onLogin() {
128128

129129
# Sends messages to Telegram to a specified chat via the desired bot
130130
function sendTG() {
131-
local bot chat parse_mode msg
131+
local bot chat parse_mode msg file file_size response
132132
bot="${BOT:-kronic}"
133-
chat="${1:-me}"
134133
parse_mode="${PARSE_MODE:-md}"
135-
shift
134+
135+
if [[ $# -eq 1 ]]; then
136+
chat="me"
137+
else
138+
chat="${1:-me}"
139+
shift
140+
fi
141+
136142
msg=${*:?}
137143
case "${bot}" in
138144
"ts") BOT_API_KEY="$(cat ~/.tskey)" ;;
@@ -159,7 +165,33 @@ function sendTG() {
159165
return
160166
;;
161167
esac
162-
curl -s "https://api.telegram.org/bot${BOT_API_KEY}/sendMessage" --data "text=${msg}&chat_id=${CHAT_ID}&parse_mode=${parse_mode}" 1> /dev/null
168+
169+
# Telegram Bot API allows multipart document uploads up to 50 MiB.
170+
if [[ $# -eq 1 && -f "${1}" && -r "${1}" ]]; then
171+
file="${1}"
172+
file_size="$(wc -c < "${file}")"
173+
if [[ "${file_size}" -gt 52428800 ]]; then
174+
echo "File is too large for Telegram Bot API upload: ${file} (${file_size} bytes > 52428800 bytes)"
175+
return 1
176+
fi
177+
if [[ "${file_size}" -le 52428800 ]]; then
178+
response="$(curl -s "https://api.telegram.org/bot${BOT_API_KEY}/sendDocument" \
179+
--form "chat_id=${CHAT_ID}" \
180+
--form "document=@${file}")"
181+
if [[ "${response}" != *'"ok":true'* ]]; then
182+
echo "${response}"
183+
return 1
184+
fi
185+
echo
186+
return
187+
fi
188+
fi
189+
190+
response="$(curl -s "https://api.telegram.org/bot${BOT_API_KEY}/sendMessage" --data "text=${msg}&chat_id=${CHAT_ID}&parse_mode=${parse_mode}")"
191+
if [[ "${response}" != *'"ok":true'* ]]; then
192+
echo "${response}"
193+
return 1
194+
fi
163195
echo
164196
}
165197

0 commit comments

Comments
 (0)