From e5f7b2da50083618de4ceef3ebd716cdd026d7a2 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Mon, 20 May 2019 17:26:21 +0200 Subject: [PATCH] fix send_file, add test for send_file --- bashbot.sh | 16 +++++++++++++++- modules/sendMessage.sh | 15 ++++++++++----- test/d-send_message-test.sh | 6 +++++- .../d-send_message-test.result | 4 ++-- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/bashbot.sh b/bashbot.sh index 4342a0b..ef59191 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -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 diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh index b45547e..7ad8024 100644 --- a/modules/sendMessage.sh +++ b/modules/sendMessage.sh @@ -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 diff --git a/test/d-send_message-test.sh b/test/d-send_message-test.sh index d5705d2..989e5d3 100755 --- a/test/d-send_message-test.sh +++ b/test/d-send_message-test.sh @@ -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 diff --git a/test/d-send_message-test/d-send_message-test.result b/test/d-send_message-test/d-send_message-test.result index 96b38cb..3abd7e0 100644 --- a/test/d-send_message-test/d-send_message-test.result +++ b/test/d-send_message-test/d-send_message-test.result @@ -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