fix send_file, add test for send_file

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2019-05-20 17:26:21 +02:00
parent d1a3372d39
commit e5f7b2da50
4 changed files with 32 additions and 9 deletions

View File

@ -12,7 +12,7 @@
# This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
#
#### $$VERSION$$ v0.80-pre-3-g3c5ffdb
#### $$VERSION$$ v0.80-pre-4-gd1a3372
#
# Exit Codes:
# - 0 sucess (hopefully)
@ -183,6 +183,16 @@ if [ "${BASHBOT_WGET}" = "" ] && _exists curl ; then
BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$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
# simple curl or wget call outputs result to stdout
getJson(){
@ -197,6 +207,10 @@ else
BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$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
# convert common telegram entities to JSON

View File

@ -5,7 +5,7 @@
# This file is public domain in the USA and all free countries.
# 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
@ -87,10 +87,16 @@ send_button() {
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() {
[ "$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 CAPTION=',"caption":"'$3'"'; [ "$3" = "" ] && CAPTION=""
# file access checks ...
[[ "$file" = *'..'* ]] && return # no directory traversal
[[ "$file" = '.'* ]] && return # no hidden or relative files
@ -136,8 +142,7 @@ send_file() {
;;
esac
send_action "${1}" "$STATUS"
# shellcheck disable=SC2034
sendJson "${1}" '"'"$WHAT"'":"'"$2"'"'"$CAPTION"'"' "$CUR_URL"
sendUpload "$1" "${WHAT}" "${file}" "${CUR_URL}" "$3"
}
# 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

View File

@ -1,5 +1,5 @@
#!/usr/bin/env bash
#### $$VERSION$$ v0.80-pre-3-g3c5ffdb
#### $$VERSION$$ v0.80-pre-4-gd1a3372
# include common functions and definitions
# shellcheck source=test/ALL-tests.inc.sh
@ -28,6 +28,10 @@ sendEmpty() {
sendJson() {
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

View File

@ -53,12 +53,12 @@ URL:https://api.telegram.org/botbashbottestscript/sendMessage
chat:123456 JSON:"action": "upload_photo"
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
chat:123456 JSON:"action": "upload_document"
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