mirror of
https://github.com/octoleo/telegram-bot-bash.git
synced 2024-12-31 22:01:46 +00:00
replace all curl calls, add wget as curl replacement
This commit is contained in:
parent
df03727e14
commit
bce7f1a6dc
156
bashbot.sh
156
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-dev2-11-gb55c171
|
#### $$VERSION$$ v0.80-dev2-12-gdf03727
|
||||||
#
|
#
|
||||||
# Exit Codes:
|
# Exit Codes:
|
||||||
# - 0 sucess (hopefully)
|
# - 0 sucess (hopefully)
|
||||||
@ -66,16 +66,6 @@ if [ ! -f "${TOKENFILE}" ]; then
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
JSONSHFILE="${BASHBOT_JSONSH:-${RUNDIR}/JSON.sh/JSON.sh}"
|
|
||||||
[[ "${JSONSHFILE}" != *"/JSON.sh" ]] && echo -e "${RED}ERROR: \"${JSONSHFILE}\" ends not with \"JSONS.sh\".${NC}" && exit 3
|
|
||||||
|
|
||||||
if [ ! -f "${JSONSHFILE}" ]; then
|
|
||||||
echo "Seems to be first run, Downloading ${JSONSHFILE}..."
|
|
||||||
[[ "${JSONSHFILE}" = "${RUNDIR}/JSON.sh/JSON.sh" ]] && mkdir "JSON.sh" 2>/dev/null;
|
|
||||||
curl -sL -o "${JSONSHFILE}" "https://cdn.jsdelivr.net/gh/dominictarr/JSON.sh/JSON.sh"
|
|
||||||
chmod +x "${JSONSHFILE}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
BOTADMIN="${BASHBOT_ETC:-.}/botadmin"
|
BOTADMIN="${BASHBOT_ETC:-.}/botadmin"
|
||||||
if [ ! -f "${BOTADMIN}" ]; then
|
if [ ! -f "${BOTADMIN}" ]; then
|
||||||
if [ "${CLEAR}" = "" ]; then
|
if [ "${CLEAR}" = "" ]; then
|
||||||
@ -144,20 +134,58 @@ if [ "$1" != "source" ]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
# returns true if command exist
|
||||||
|
_exists()
|
||||||
|
{
|
||||||
|
[ "$(LC_ALL=C type -t "$1")" = "file" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
# returns true if function exist
|
||||||
|
_is_function()
|
||||||
|
{
|
||||||
|
[ "$(LC_ALL=C type -t "$1")" = "function" ]
|
||||||
|
}
|
||||||
|
|
||||||
DELETE_URL=$URL'/deleteMessage'
|
DELETE_URL=$URL'/deleteMessage'
|
||||||
delete_message() {
|
delete_message() {
|
||||||
sendJson "${1}" 'message_id: '"${2}"'' "${DELETE_URL}"
|
sendJson "${1}" 'message_id: '"${2}"'' "${DELETE_URL}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get_file() {
|
||||||
|
[ "$1" = "" ] && return
|
||||||
|
local JSON='"file_id": '"${1}"
|
||||||
|
sendJson "" "${JSON}" "${GETFILE_URL}"
|
||||||
|
echo "${URL}/$(echo "${res}" | jsonGetString '"result","file_path"')"
|
||||||
|
}
|
||||||
|
|
||||||
# usage: sendJson "chat" "JSON" "URL"
|
# usage: sendJson "chat" "JSON" "URL"
|
||||||
sendJson(){
|
if [ "${BASHBOT_WGET}" = "" ] && _exists curl ; then
|
||||||
|
# simple curl or wget call, output to stdout
|
||||||
|
getJson(){
|
||||||
|
curl -sL "$1"
|
||||||
|
}
|
||||||
|
sendJson(){
|
||||||
local chat="";
|
local chat="";
|
||||||
[ "${1}" != "" ] && chat='"chat_id":'"${1}"','
|
[ "${1}" != "" ] && chat='"chat_id":'"${1}"','
|
||||||
res="$(curl -s -d '{'"${chat} $2"'}' -X POST "${3}" \
|
res="$(curl -s -d '{'"${chat} $2"'}' -X POST "${3}" \
|
||||||
-H "Content-Type: application/json" | "${JSONSHFILE}" -s -b -n )"
|
-H "Content-Type: application/json" | "${JSONSHFILE}" -s -b -n )"
|
||||||
BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$res")"
|
BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$res")"
|
||||||
BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "$res")"
|
BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "$res")"
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
# simple curl or wget call outputs result to stdout
|
||||||
|
getJson(){
|
||||||
|
wegt -q -O- "$1"
|
||||||
|
}
|
||||||
|
sendJson(){
|
||||||
|
local chat="";
|
||||||
|
[ "${1}" != "" ] && chat='"chat_id":'"${1}"','
|
||||||
|
res="$(wget -q -O- --post-data='{'"${chat} $2"'}' \
|
||||||
|
--header='Content-Type:application/json' "${3}" | "${JSONSHFILE}" -s -b -n )"
|
||||||
|
BOTSENT[OK]="$(JsonGetLine '"ok"' <<< "$res")"
|
||||||
|
BOTSENT[ID]="$(JsonGetValue '"result","message_id"' <<< "$res")"
|
||||||
|
}
|
||||||
|
fi
|
||||||
|
|
||||||
# convert common telegram entities to JSON
|
# convert common telegram entities to JSON
|
||||||
# title caption description markup inlinekeyboard
|
# title caption description markup inlinekeyboard
|
||||||
@ -171,18 +199,49 @@ title2Json(){
|
|||||||
echo "${title}${caption}${desc}${markup}${keyboard}"
|
echo "${title}${caption}${desc}${markup}${keyboard}"
|
||||||
}
|
}
|
||||||
|
|
||||||
get_file() {
|
# get bot name
|
||||||
[ "$1" = "" ] && return
|
getBotName() {
|
||||||
local JSON='"file_id": '"${1}"
|
sendJson "" "" "$ME_URL"
|
||||||
sendJson "" "${JSON}" "${GETFILE_URL}"
|
JsonGetString '"result","username"' <<< "$res"
|
||||||
echo "${URL}/$(echo "${res}" | jsonGetString '"result","file_path"')"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# returns true if function exist
|
# use phyton JSON to decode JSON UFT-8, provide bash implementaion as fallback
|
||||||
_is_function()
|
if [ "${BASHBOT_DECODE}" != "" ] && _exists python ; then
|
||||||
{
|
JsonDecode() {
|
||||||
[ "$(LC_ALL=C type -t "$1")" = "function" ]
|
printf '"%s\\n"' "${1//\"/\\\"}" | python -c 'import json, sys; sys.stdout.write(json.load(sys.stdin).encode("utf-8"))'
|
||||||
|
}
|
||||||
|
else
|
||||||
|
# pure bash implementaion, done by KayM (@gnadelwartz)
|
||||||
|
# see https://stackoverflow.com/a/55666449/9381171
|
||||||
|
JsonDecode() {
|
||||||
|
local out="$1"
|
||||||
|
local remain=""
|
||||||
|
local regexp='(.*)\\u[dD]([0-9a-fA-F]{3})\\u[dD]([0-9a-fA-F]{3})(.*)'
|
||||||
|
local W1 W2 U
|
||||||
|
while [[ "${out}" =~ $regexp ]] ; do
|
||||||
|
# match 2 \udxxx hex values, calculate new U, then split and replace
|
||||||
|
W1=$(( ( 0xd${BASH_REMATCH[2]} & 0x3ff) <<10 ))
|
||||||
|
W2=$(( 0xd${BASH_REMATCH[3]} & 0x3ff ))
|
||||||
|
U=$(( ( W1 | W2 ) + 0x10000 ))
|
||||||
|
remain="$(printf '\\U%8.8x' "${U}")${BASH_REMATCH[4]}${remain}"
|
||||||
|
out="${BASH_REMATCH[1]}"
|
||||||
|
done
|
||||||
|
echo -e "${out}${remain}"
|
||||||
|
}
|
||||||
|
fi
|
||||||
|
|
||||||
|
JsonGetString() {
|
||||||
|
sed -n -e '0,/\['"$1"'\]/ s/\['"$1"'\][ \t]"\(.*\)"$/\1/p'
|
||||||
}
|
}
|
||||||
|
JsonGetLine() {
|
||||||
|
sed -n -e '0,/\['"$1"'\]/ s/\['"$1"'\][ \t]//p'
|
||||||
|
}
|
||||||
|
JsonGetValue() {
|
||||||
|
sed -n -e '0,/\['"$1"'\]/ s/\['"$1"'\][ \t]\([0-9.,]*\).*/\1/p'
|
||||||
|
}
|
||||||
|
|
||||||
|
################
|
||||||
|
# processing of updates starts here
|
||||||
process_updates() {
|
process_updates() {
|
||||||
MAX_PROCESS_NUMBER="$(sed <<< "${UPDATE}" '/\["result",[0-9]*\]/!d' | tail -1 | sed 's/\["result",//g;s/\].*//g')"
|
MAX_PROCESS_NUMBER="$(sed <<< "${UPDATE}" '/\["result",[0-9]*\]/!d' | tail -1 | sed 's/\["result",//g;s/\].*//g')"
|
||||||
for ((PROCESS_NUMBER=0; PROCESS_NUMBER<=MAX_PROCESS_NUMBER; PROCESS_NUMBER++)); do
|
for ((PROCESS_NUMBER=0; PROCESS_NUMBER<=MAX_PROCESS_NUMBER; PROCESS_NUMBER++)); do
|
||||||
@ -207,16 +266,6 @@ process_client() {
|
|||||||
grep -q "$tmpcount" <"${COUNTFILE}" >/dev/null 2>&1 || echo "$tmpcount">>"${COUNTFILE}"
|
grep -q "$tmpcount" <"${COUNTFILE}" >/dev/null 2>&1 || echo "$tmpcount">>"${COUNTFILE}"
|
||||||
# To get user count execute bash bashbot.sh count
|
# To get user count execute bash bashbot.sh count
|
||||||
}
|
}
|
||||||
JsonGetString() {
|
|
||||||
sed -n -e '0,/\['"$1"'\]/ s/\['"$1"'\][ \t]"\(.*\)"$/\1/p'
|
|
||||||
}
|
|
||||||
JsonGetLine() {
|
|
||||||
sed -n -e '0,/\['"$1"'\]/ s/\['"$1"'\][ \t]//p'
|
|
||||||
}
|
|
||||||
JsonGetValue() {
|
|
||||||
sed -n -e '0,/\['"$1"'\]/ s/\['"$1"'\][ \t]\([0-9.,]*\).*/\1/p'
|
|
||||||
}
|
|
||||||
|
|
||||||
process_inline() {
|
process_inline() {
|
||||||
local num="${1}"
|
local num="${1}"
|
||||||
iQUERY[0]="$(JsonDecode "$(JsonGetString <<<"${UPDATE}" '"result",0,"inline_query","query"')")"
|
iQUERY[0]="$(JsonDecode "$(JsonGetString <<<"${UPDATE}" '"result",0,"inline_query","query"')")"
|
||||||
@ -304,6 +353,8 @@ process_message() {
|
|||||||
rm "$TMP"
|
rm "$TMP"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#########################
|
||||||
# main get updates loop, should never terminate
|
# main get updates loop, should never terminate
|
||||||
start_bot() {
|
start_bot() {
|
||||||
local DEBUG="$1"
|
local DEBUG="$1"
|
||||||
@ -315,7 +366,7 @@ start_bot() {
|
|||||||
[ "${DEBUG}" != "" ] && date && echo "Start BASHBOT in Mode \"${DEBUG}\""
|
[ "${DEBUG}" != "" ] && date && echo "Start BASHBOT in Mode \"${DEBUG}\""
|
||||||
[[ "${DEBUG}" = "xdebug"* ]] && set -x
|
[[ "${DEBUG}" = "xdebug"* ]] && set -x
|
||||||
while true; do
|
while true; do
|
||||||
UPDATE="$(curl -s "$UPD_URL$OFFSET" | "${JSONSHFILE}" -s -b -n)"
|
UPDATE="$(getJson "$UPD_URL$OFFSET" | "${JSONSHFILE}" -s -b -n)"
|
||||||
|
|
||||||
# Offset
|
# Offset
|
||||||
OFFSET="$(grep <<< "${UPDATE}" '\["result",[0-9]*,"update_id"\]' | tail -1 | cut -f 2)"
|
OFFSET="$(grep <<< "${UPDATE}" '\["result",[0-9]*,"update_id"\]' | tail -1 | cut -f 2)"
|
||||||
@ -358,11 +409,15 @@ bot_init() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# get bot name
|
JSONSHFILE="${BASHBOT_JSONSH:-${RUNDIR}/JSON.sh/JSON.sh}"
|
||||||
getBotName() {
|
[[ "${JSONSHFILE}" != *"/JSON.sh" ]] && echo -e "${RED}ERROR: \"${JSONSHFILE}\" ends not with \"JSONS.sh\".${NC}" && exit 3
|
||||||
sendJson "" "" "$ME_URL"
|
|
||||||
JsonGetString '"result","username"' <<< "$res"
|
if [ ! -f "${JSONSHFILE}" ]; then
|
||||||
}
|
echo "Seems to be first run, Downloading ${JSONSHFILE}..."
|
||||||
|
[[ "${JSONSHFILE}" = "${RUNDIR}/JSON.sh/JSON.sh" ]] && mkdir "JSON.sh" 2>/dev/null;
|
||||||
|
getJson "https://cdn.jsdelivr.net/gh/dominictarr/JSON.sh/JSON.sh" >"${JSONSHFILE}"
|
||||||
|
chmod +x "${JSONSHFILE}"
|
||||||
|
fi
|
||||||
|
|
||||||
ME="$(getBotName)"
|
ME="$(getBotName)"
|
||||||
if [ "$ME" = "" ]; then
|
if [ "$ME" = "" ]; then
|
||||||
@ -374,31 +429,6 @@ if [ "$ME" = "" ]; then
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# use phyton JSON to decode JSON UFT-8, provide bash implementaion as fallback
|
|
||||||
if [ "${BASHBOT_DECODE}" != "" ] && which python >/dev/null 2>&1 ; then
|
|
||||||
JsonDecode() {
|
|
||||||
printf '"%s\\n"' "${1//\"/\\\"}" | python -c 'import json, sys; sys.stdout.write(json.load(sys.stdin).encode("utf-8"))'
|
|
||||||
}
|
|
||||||
else
|
|
||||||
# pure bash implementaion, done by KayM (@gnadelwartz)
|
|
||||||
# see https://stackoverflow.com/a/55666449/9381171
|
|
||||||
JsonDecode() {
|
|
||||||
local out="$1"
|
|
||||||
local remain=""
|
|
||||||
local regexp='(.*)\\u[dD]([0-9a-fA-F]{3})\\u[dD]([0-9a-fA-F]{3})(.*)'
|
|
||||||
local W1 W2 U
|
|
||||||
while [[ "${out}" =~ $regexp ]] ; do
|
|
||||||
# match 2 \udxxx hex values, calculate new U, then split and replace
|
|
||||||
W1=$(( ( 0xd${BASH_REMATCH[2]} & 0x3ff) <<10 ))
|
|
||||||
W2=$(( 0xd${BASH_REMATCH[3]} & 0x3ff ))
|
|
||||||
U=$(( ( W1 | W2 ) + 0x10000 ))
|
|
||||||
remain="$(printf '\\U%8.8x' "${U}")${BASH_REMATCH[4]}${remain}"
|
|
||||||
out="${BASH_REMATCH[1]}"
|
|
||||||
done
|
|
||||||
echo -e "${out}${remain}"
|
|
||||||
}
|
|
||||||
fi
|
|
||||||
|
|
||||||
# source the script with source as param to use functions in other scripts
|
# source the script with source as param to use functions in other scripts
|
||||||
# do not execute if read from other scripts
|
# do not execute if read from other scripts
|
||||||
|
|
||||||
|
@ -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-dev2-11-gb55c171
|
#### $$VERSION$$ v0.80-dev2-12-gdf03727
|
||||||
|
|
||||||
# source from commands.sh to use the sendMessage functions
|
# source from commands.sh to use the sendMessage functions
|
||||||
|
|
||||||
@ -89,9 +89,8 @@ UPLOADDIR="${BASHBOT_UPLOAD:-${TMPDIR}/upload}"
|
|||||||
|
|
||||||
send_file() {
|
send_file() {
|
||||||
[ "$2" = "" ] && return
|
[ "$2" = "" ] && return
|
||||||
local CAPTION
|
local file="$2"
|
||||||
local chat_id=$1
|
local CAPTION=',"caption":"'$3'"'; [ "$3" = "" ] && CAPTION=""
|
||||||
local file=$2
|
|
||||||
# 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
|
||||||
@ -105,45 +104,40 @@ send_file() {
|
|||||||
local ext="${file##*.}"
|
local ext="${file##*.}"
|
||||||
case $ext in
|
case $ext in
|
||||||
mp3|flac)
|
mp3|flac)
|
||||||
CUR_URL=$AUDIO_URL
|
CUR_URL="$AUDIO_URL"
|
||||||
WHAT=audio
|
WHAT="audio"
|
||||||
STATUS=upload_audio
|
STATUS="upload_audio"
|
||||||
CAPTION="$3"
|
|
||||||
;;
|
;;
|
||||||
png|jpg|jpeg|gif)
|
png|jpg|jpeg|gif)
|
||||||
CUR_URL=$PHO_URL
|
CUR_URL="$PHO_URL"
|
||||||
WHAT=photo
|
WHAT="photo"
|
||||||
STATUS=upload_photo
|
STATUS="upload_photo"
|
||||||
CAPTION="$3"
|
|
||||||
;;
|
;;
|
||||||
webp)
|
webp)
|
||||||
CUR_URL=$STICKER_URL
|
CUR_URL="$STICKER_URL"
|
||||||
WHAT=sticker
|
WHAT="sticker"
|
||||||
STATUS=
|
STATUS="upload_photo"
|
||||||
;;
|
;;
|
||||||
mp4)
|
mp4)
|
||||||
CUR_URL=$VIDEO_URL
|
CUR_URL="$VIDEO_URL"
|
||||||
WHAT=video
|
WHAT="video"
|
||||||
STATUS=upload_video
|
STATUS="upload_video"
|
||||||
CAPTION="$3"
|
|
||||||
;;
|
;;
|
||||||
|
|
||||||
ogg)
|
ogg)
|
||||||
CUR_URL=$VOICE_URL
|
CUR_URL="$VOICE_URL"
|
||||||
WHAT=voice
|
WHAT="voice"
|
||||||
STATUS=
|
STATUS="upload_audio"
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
CUR_URL=$DOCUMENT_URL
|
CUR_URL="$DOCUMENT_URL"
|
||||||
WHAT=document
|
WHAT="document"
|
||||||
STATUS=upload_document
|
STATUS="upload_document"
|
||||||
CAPTION="$3"
|
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
send_action "$chat_id" "$STATUS"
|
send_action "${1}" "$STATUS"
|
||||||
# convert over to sendJson!! much better: use sendjson in case above ...
|
|
||||||
# shellcheck disable=SC2034
|
# shellcheck disable=SC2034
|
||||||
res="$(curl -s "$CUR_URL" -F "chat_id=$chat_id" -F "$WHAT=@$file" -F "caption=$CAPTION")"
|
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
|
||||||
|
Loading…
Reference in New Issue
Block a user