mirror of
https://github.com/octoleo/telegram-bot-bash.git
synced 2024-11-21 23:25:08 +00:00
modules: send_file support for URL
This commit is contained in:
parent
4582efd556
commit
dd19f0f34e
10
bashbot.sh
10
bashbot.sh
@ -30,7 +30,7 @@
|
||||
# 8 - curl/wget missing
|
||||
# 10 - not bash!
|
||||
#
|
||||
#### $$VERSION$$ v1.25-dev-14-g2fe6d4b
|
||||
#### $$VERSION$$ v1.25-dev-15-g4582efd
|
||||
##################################################################
|
||||
|
||||
# emmbeded system may claim bash but it is not
|
||||
@ -487,13 +487,13 @@ if detect_curl ; then
|
||||
sendUpload() {
|
||||
[ "$#" -lt 4 ] && return
|
||||
if [ -n "$5" ]; then
|
||||
[ -n "${BASHBOTDEBUG}" ] &&\
|
||||
log_update "sendUpload CHAT=$1 WHAT=$2 FILE=$3 CAPT=$5"
|
||||
# shellcheck disable=SC2086
|
||||
[ -n "${BASHBOTDEBUG}" ] &&\
|
||||
log_update "sendUpload CHAT=$1 WHAT=$2 FILE=$3 CAPT=$5"
|
||||
# shellcheck disable=SC2086
|
||||
res="$("${BASHBOT_CURL}" -s -k ${BASHBOT_CURL_ARGS} "$4" -F "chat_id=$1"\
|
||||
-F "$2=@$3;${3##*/}" -F "caption=$5" | "${JSONSHFILE}" -b -n 2>/dev/null )"
|
||||
else
|
||||
# shellcheck disable=SC2086
|
||||
# shellcheck disable=SC2086
|
||||
res="$("${BASHBOT_CURL}" -s -k ${BASHBOT_CURL_ARGS} "$4" -F "chat_id=$1"\
|
||||
-F "$2=@$3;${3##*/}" | "${JSONSHFILE}" -b -n 2>/dev/null )"
|
||||
fi
|
||||
|
@ -21,7 +21,7 @@
|
||||
# AUTHOR: KayM (gnadelwartz), kay@rrr.de
|
||||
# CREATED: 25.12.2020 20:24
|
||||
#
|
||||
#### $$VERSION$$ v1.25-dev-14-g2fe6d4b
|
||||
#### $$VERSION$$ v1.25-dev-15-g4582efd
|
||||
#===============================================================================
|
||||
|
||||
####
|
||||
@ -54,7 +54,8 @@ else
|
||||
fi
|
||||
|
||||
FILE="$2"
|
||||
[[ "$2" != "/"* ]] && FILE="${PWD}/$2"
|
||||
# convert to absolute path if not start with / or http://
|
||||
[[ ! ( "$2" == "/"* || "$2" =~ ^https*:// ) ]] && FILE="${PWD}/$2"
|
||||
|
||||
# send message in selected format
|
||||
"${SEND}" "${CHAT}" "${FILE}" "$3"
|
||||
|
@ -6,7 +6,7 @@
|
||||
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
|
||||
#
|
||||
# shellcheck disable=SC1117
|
||||
#### $$VERSION$$ v1.25-dev-14-g2fe6d4b
|
||||
#### $$VERSION$$ v1.25-dev-15-g4582efd
|
||||
|
||||
# will be automatically sourced from bashbot
|
||||
|
||||
@ -205,19 +205,28 @@ send_file() {
|
||||
fi
|
||||
}
|
||||
|
||||
# supports http and local file
|
||||
# $1 chat, $2 file, $3 caption, $4 extension (optional)
|
||||
upload_file(){
|
||||
local CUR_URL WHAT STATUS text=$3 file="$2"
|
||||
# file access checks ...
|
||||
[[ "${file}" = *'..'* ]] && return 1 # no directory traversal
|
||||
[[ "${file}" = '.'* ]] && return 1 # no hidden or relative files
|
||||
if [[ "${file}" = '/'* ]] ; then
|
||||
[[ ! "${file}" =~ ${FILE_REGEX} ]] && return 2 # absolute must match REGEX
|
||||
local CUR_URL WHAT STATUS media text file="$2" ext="$4"
|
||||
text="$(JsonEscape "$3")"
|
||||
if [[ "${file}" =~ ^https*:// ]]; then
|
||||
media="URL"
|
||||
else
|
||||
file="${UPLOADDIR:-NOUPLOADDIR}/${file}" # othiers must be in UPLOADDIR
|
||||
# we have a file, check file location ...
|
||||
media="FILE"
|
||||
[[ "${file}" = *'..'* ]] && return 1 # no directory traversal
|
||||
[[ "${file}" = '.'* ]] && return 1 # no hidden or relative files
|
||||
if [[ "${file}" = '/'* ]] ; then
|
||||
[[ ! "${file}" =~ ${FILE_REGEX} ]] && return 2 # absolute must match REGEX
|
||||
else
|
||||
file="${UPLOADDIR:-NOUPLOADDIR}/${file}" # othiers must be in UPLOADDIR
|
||||
fi
|
||||
[ ! -r "${file}" ] && return 3 # and file must exits of course
|
||||
fi
|
||||
[ ! -r "${file}" ] && return 3 # and file must exits of course
|
||||
|
||||
case "${file##*.}" in
|
||||
[ -z "${ext}" ] && ext="${file##*.}"
|
||||
case "${ext}" in
|
||||
mp3|flac)
|
||||
CUR_URL="${AUDIO_URL}"
|
||||
WHAT="audio"
|
||||
@ -250,8 +259,19 @@ upload_file(){
|
||||
STATUS="upload_document"
|
||||
;;
|
||||
esac
|
||||
|
||||
# prepare to send FILE / URL
|
||||
send_action "$1" "${STATUS}"
|
||||
sendUpload "$1" "${WHAT}" "${file}" "${CUR_URL}" "${text//\\n/$'\n'}"
|
||||
# select method to use
|
||||
case "${media}" in
|
||||
FILE) # send local file ...
|
||||
sendUpload "$1" "${WHAT}" "${file}" "${CUR_URL}" "${text//\\n/$'\n'}";;
|
||||
|
||||
URL) # send URL, should also work for file_id ...
|
||||
# e.g. '"photo":"https://dealz.rrr.de/assets/images/rbofd-1.gif","caption":"some text"'
|
||||
sendJson "$1" '"'"${WHAT}"'":"'"${file}"'","caption":"'"${text//\\n/$'\n'}"'"' "${CUR_URL}"
|
||||
esac
|
||||
return 0
|
||||
}
|
||||
|
||||
# typing for text messages, upload_photo for photos, record_video or upload_video for videos, record_audio or upload_audio for audio files, upload_document for general files, find_location for location
|
||||
|
Loading…
Reference in New Issue
Block a user