fix send_file, add test for send_file
This commit is contained in:
parent
d1a3372d39
commit
e5f7b2da50
16
bashbot.sh
16
bashbot.sh
|
@ -12,7 +12,7 @@
|
||||||
# This file is public domain in the USA and all free countries.
|
# This file is public domain in the USA and all free countries.
|
||||||
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
|
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
|
||||||
#
|
#
|
||||||
#### $$VERSION$$ v0.80-pre-3-g3c5ffdb
|
#### $$VERSION$$ v0.80-pre-4-gd1a3372
|
||||||
#
|
#
|
||||||
# Exit Codes:
|
# Exit Codes:
|
||||||
# - 0 sucess (hopefully)
|
# - 0 sucess (hopefully)
|
||||||
|
@ -183,6 +183,16 @@ if [ "${BASHBOT_WGET}" = "" ] && _exists curl ; then
|
||||||
BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$res")"
|
BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$res")"
|
||||||
BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "$res")"
|
BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "$res")"
|
||||||
}
|
}
|
||||||
|
#$1 Chat, $2 what , $3 file, $4 URL, $5 caption
|
||||||
|
sendUpload() {
|
||||||
|
[ "$#" -lt 4 ] && return
|
||||||
|
if [ "$5" != "" ]; then
|
||||||
|
res="$(curl -s "$4" -F "chat_id=$1" -F "$2=@$3;${3##*/}" -F "caption=$5" | "${JSONSHFILE}" -s -b -n )"
|
||||||
|
else
|
||||||
|
res="$(curl -s "$4" -F "chat_id=$1" -F "$2=@$3;${3##*/}" | "${JSONSHFILE}" -s -b -n )"
|
||||||
|
fi
|
||||||
|
BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$res")"
|
||||||
|
}
|
||||||
else
|
else
|
||||||
# simple curl or wget call outputs result to stdout
|
# simple curl or wget call outputs result to stdout
|
||||||
getJson(){
|
getJson(){
|
||||||
|
@ -197,6 +207,10 @@ else
|
||||||
BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$res")"
|
BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$res")"
|
||||||
BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "$res")"
|
BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "$res")"
|
||||||
}
|
}
|
||||||
|
sendUpload() {
|
||||||
|
sendJson "$1" '"text":"Sorry, wget does not support file upload"' "${MSG_URL}"
|
||||||
|
BOTSENT[OK]="false"
|
||||||
|
}
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# convert common telegram entities to JSON
|
# convert common telegram entities to JSON
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
# This file is public domain in the USA and all free countries.
|
# This file is public domain in the USA and all free countries.
|
||||||
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
|
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
|
||||||
#
|
#
|
||||||
#### $$VERSION$$ v0.80-pre-3-g3c5ffdb
|
#### $$VERSION$$ v0.80-pre-4-gd1a3372
|
||||||
|
|
||||||
# source from commands.sh to use the sendMessage functions
|
# source from commands.sh to use the sendMessage functions
|
||||||
|
|
||||||
|
@ -87,10 +87,16 @@ send_button() {
|
||||||
|
|
||||||
UPLOADDIR="${BASHBOT_UPLOAD:-${TMPDIR}/upload}"
|
UPLOADDIR="${BASHBOT_UPLOAD:-${TMPDIR}/upload}"
|
||||||
|
|
||||||
|
# for now this can only send local files with curl!
|
||||||
|
# extend to allow send files by URL or telegram ID
|
||||||
send_file() {
|
send_file() {
|
||||||
[ "$2" = "" ] && return 0
|
[ "$2" = "" ] && return
|
||||||
|
[[ "$2" = "http"* ]] && return # currently we do not support URL
|
||||||
|
upload_file "${@}"
|
||||||
|
}
|
||||||
|
|
||||||
|
upload_file(){
|
||||||
local CUR_URL WHAT STATUS file="$2"
|
local CUR_URL WHAT STATUS file="$2"
|
||||||
local CAPTION=',"caption":"'$3'"'; [ "$3" = "" ] && CAPTION=""
|
|
||||||
# file access checks ...
|
# file access checks ...
|
||||||
[[ "$file" = *'..'* ]] && return # no directory traversal
|
[[ "$file" = *'..'* ]] && return # no directory traversal
|
||||||
[[ "$file" = '.'* ]] && return # no hidden or relative files
|
[[ "$file" = '.'* ]] && return # no hidden or relative files
|
||||||
|
@ -136,8 +142,7 @@ send_file() {
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
send_action "${1}" "$STATUS"
|
send_action "${1}" "$STATUS"
|
||||||
# shellcheck disable=SC2034
|
sendUpload "$1" "${WHAT}" "${file}" "${CUR_URL}" "$3"
|
||||||
sendJson "${1}" '"'"$WHAT"'":"'"$2"'"'"$CAPTION"'"' "$CUR_URL"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# 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
|
# 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
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
#### $$VERSION$$ v0.80-pre-3-g3c5ffdb
|
#### $$VERSION$$ v0.80-pre-4-gd1a3372
|
||||||
|
|
||||||
# include common functions and definitions
|
# include common functions and definitions
|
||||||
# shellcheck source=test/ALL-tests.inc.sh
|
# shellcheck source=test/ALL-tests.inc.sh
|
||||||
|
@ -28,6 +28,10 @@ sendEmpty() {
|
||||||
sendJson() {
|
sendJson() {
|
||||||
printf 'chat:%s\tJSON:%s\nURL:%s\n\n' "${1}" "${2}" "${3}"
|
printf 'chat:%s\tJSON:%s\nURL:%s\n\n' "${1}" "${2}" "${3}"
|
||||||
}
|
}
|
||||||
|
sendUpload() {
|
||||||
|
#JSON:"document":"/tmp/allowed/this_is_my.doc","caption":"Text plus absolute file will appear in chat""
|
||||||
|
printf 'chat:%s\tJSON:"%s":"%s","caption":"%s"\nURL:%s\n\n' "${1}" "${2}" "${3}" "${5}" "${4}"
|
||||||
|
}
|
||||||
|
|
||||||
# send text input to send_message
|
# send text input to send_message
|
||||||
|
|
||||||
|
|
|
@ -53,12 +53,12 @@ URL:https://api.telegram.org/botbashbottestscript/sendMessage
|
||||||
chat:123456 JSON:"action": "upload_photo"
|
chat:123456 JSON:"action": "upload_photo"
|
||||||
URL:https://api.telegram.org/botbashbottestscript/sendChatAction
|
URL:https://api.telegram.org/botbashbottestscript/sendChatAction
|
||||||
|
|
||||||
chat:123456 JSON:"photo":"/tmp/allowed/this_is_my.gif","caption":"Text plus absolute file will appear in chat""
|
chat:123456 JSON:"photo":"/tmp/allowed/this_is_my.gif","caption":"Text plus absolute file will appear in chat"
|
||||||
URL:https://api.telegram.org/botbashbottestscript/sendPhoto
|
URL:https://api.telegram.org/botbashbottestscript/sendPhoto
|
||||||
|
|
||||||
chat:123456 JSON:"action": "upload_document"
|
chat:123456 JSON:"action": "upload_document"
|
||||||
URL:https://api.telegram.org/botbashbottestscript/sendChatAction
|
URL:https://api.telegram.org/botbashbottestscript/sendChatAction
|
||||||
|
|
||||||
chat:123456 JSON:"document":"/tmp/allowed/this_is_my.doc","caption":"Text plus absolute file will appear in chat""
|
chat:123456 JSON:"document":"/tmp/allowed/this_is_my.doc","caption":"Text plus absolute file will appear in chat"
|
||||||
URL:https://api.telegram.org/botbashbottestscript/sendDocument
|
URL:https://api.telegram.org/botbashbottestscript/sendDocument
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue