allow finish inline keyboard, allow keyboards without text

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2019-04-25 13:54:31 +02:00
parent a7d14f673c
commit fe4fb34e8a
3 changed files with 59 additions and 26 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.70-dev2-22-g991ecd4
#### $$VERSION$$ v0.70-dev2-23-ga7d14f6
#
# Exit Codes:
# - 0 sucess (hopefully)
@ -159,8 +159,8 @@ UPD_URL=$URL'/getUpdates?offset='
GET_URL=$URL'/getFile'
unset USER
declare -A USER MESSAGE URLS CONTACT LOCATION CHAT FORWARD REPLYTO VENUE
export USER MESSAGE URLS CONTACT LOCATION CHAT FORWARD REPLYTO VENUE
declare -A BOTSENT USER MESSAGE URLS CONTACT LOCATION CHAT FORWARD REPLYTO VENUE
export BOTSENT USER MESSAGE URLS CONTACT LOCATION CHAT FORWARD REPLYTO VENUE
send_normal_message() {
@ -320,35 +320,44 @@ old_send_keyboard() {
res="$(curl -s "$MSG_URL" --header "content-type: multipart/form-data" -F "chat_id=$chat" -F "text=$text" -F "reply_markup={\"keyboard\": [$keyboard],\"one_time_keyboard\": true}")"
}
TEXTISEMPTY="MyTextIsEmpty"
send_keyboard() {
if [[ "$3" != *'['* ]]; then old_send_keyboard "$@"; return; fi
local chat='"chat_id":'"$1"
local text='"text":"'"$2"'"'
local keyboard="$3"
local JSON='{'"${chat}"', '"${text}"', "reply_markup": {"keyboard": [ '"${keyboard}"' ], "one_time_keyboard":true } }'
local chat='"chat_id":'"${1}"
local text='"text":"'"${2}"'"'; [ "${2}" = "" ] && text='"text":"'"${TEXTISEMPTY}"'"'
local one_time=', "one_time_keyboard":true' && [ "$4" != "" ] && one_time=""
local JSON='{'"${chat}"', '"${text}"', "reply_markup": {"keyboard": [ '"${3}"' ] '"${one_time}"' } }'
# '{"chat_id":$1, "text":"$2", "reply_markup": {"keyboard": [ ${3} ], "one_time_keyboard": true} }'
sendJson "$JSON" "$MSG_URL"
[ "${text}" = '"text":"'"${TEXTISEMPTY}"'"' ] && delete_message "${1}" "${2}"
}
remove_keyboard() {
local chat='"chat_id":'"$1"
local text='"text":"'"$2"'"'
local chat='"chat_id":'"${1}"
local text='"text":"'"${2}"'"'; [ "${2}" = "" ] && text='"text":"'"${TEXTISEMPTY}"'"'
local JSON='{'"${chat}"', '"${text}"', "reply_markup": {"remove_keyboard":true} }'
#JSON='{"chat_id":$1, "text":"$2", "reply_markup": {"remove_keyboard":true} }'
sendJson "$JSON" "$MSG_URL"
[ "${text}" = '"text":"'"${TEXTISEMPTY}"'"' ] && delete_message "${1}" "${2}"
}
send_inline_keyboard() {
local chat='"chat_id":'"$1"
local text='"text":"'"$2"'"'
local keyboard="$3"
local JSON='{'"${chat}"', '"${text}"', "reply_markup": {"inline_keyboard": [ '"${keyboard}"' ]} }'
# JSON='{"chat_id":$1, "text":"$2", "reply_markup": {"inline_keyboard": [[ {"text":"$3", "url":"$4"} ... ]]} }'
local chat='"chat_id":'"${1}"
local text='"text":"'"${2}"'"'; [ "${2}" = "" ] && text='"text":"'"${TEXTISEMPTY}"'"'
local JSON='{'"${chat}"', '"${text}"', "reply_markup": {"inline_keyboard": [ '"${3}"' ]} }'
# JSON='{"chat_id":$1, "text":"$2", "reply_markup": {"inline_keyboard": [ $3->[{"text":"text", "url":"url"}]<- ]} }'
sendJson "$JSON" "$MSG_URL"
[ "${text}" = '"text":"'"${TEXTISEMPTY}"'"' ] && delete_message "${1}" "${2}"
}
send_inline_button() {
send_inline_keyboard "${1}" "${2}" '[ {"text":"'"${3}"'", "url":"'"${4}"'"}]'
# JSON='{"chat_id":$1, "text":"$2", "reply_markup": {"inline_keyboard": [[ {"text":"$3", "url":"$4"} ... ]]} }'
}
# this will be the only send interface to telegram!
sendJson(){
res="$(curl -d "$1" -H "Content-Type: application/json" -X POST "$2")"
res="$(curl -d "${1}" -H "Content-Type: application/json" -X POST "${2}" | "${JSONSHFILE}" -s -b -n )"
BOTSENT[OK]="$(echo "$res" | JsonGetLine '"ok"')"
BOTSENT[ID]="$(echo "$res" | JsonGetValue '"result","message_id"')"
}
@ -460,7 +469,7 @@ JsonGetString() {
sed -n -e '0,/\['"$1"'\]/ s/\['"$1"'\][ \t]"\(.*\)"$/\1/p'
}
JsonGetLine() {
sed -n -e '0,/\['"$1"'\]/ s/\['"$1"'\]\][ \t]//p'
sed -n -e '0,/\['"$1"'\]/ s/\['"$1"'\][ \t]//p'
}
JsonGetValue() {
sed -n -e '0,/\['"$1"'\]/ s/\['"$1"'\][ \t]\([0-9.,]*\).*/\1/p'

View File

@ -1,5 +1,5 @@
#!/usr/bin/env bash
#### $$VERSION$$ v0.70-dev2-21-g0cfb9f0
#### $$VERSION$$ v0.70-dev2-23-ga7d14f6
# magic to ensure that we're always inside the root of our application,
# no matter from which directory we'll run script
@ -8,6 +8,7 @@ cd "$GIT_DIR/.." || exit 1
export HOOKDIR="dev/hooks"
LASTPUSH='.git/.lastpush'
REMOTEVER="$(git ls-remote -t --refs 2>/dev/null | tail -1 | sed 's/.*\/v//')"
VERSION="$(git describe --tags | sed -e 's/-.*//' -e 's/v//')"
@ -23,8 +24,12 @@ unset IFS; set -f
# LOCAL version must greater than latest REMOTE release version
if (( $(echo "${VERSION} > ${REMOTEVER}" | bc -l) )); then
# update version in bashbot files on push
set +f
[ -f "${LASTPUSH}" ] && LASTFILES="$(find ./* -newer "${LASTPUSH}")"
[ "${LASTFILES}" = "" ] && exit
echo -n " "
dev/version.sh 2>/dev/null || exit 1
# shellcheck disable=SC2086
dev/version.sh ${LASTFILES} 2>/dev/null || exit 1
echo " OK"
else
echo "Error: local version ${VERSION} must be greater than latest release version."
@ -32,3 +37,5 @@ else
exit 1
fi
# note date of last push for version
touch "${LASTPUSH}"

View File

@ -5,10 +5,11 @@
# This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
#
#### $$VERSION$$ v0.70-dev2-21-g0cfb9f0
#### $$VERSION$$ v0.70-dev2-23-ga7d14f6
#
# source from commands.sh to use the aliases
# some handy aliases for use when processing messages:
# easy handling of users:
_is_botadmin() {
user_is_botadmin "${USER[ID]}"
}
@ -19,7 +20,7 @@ _is_creator() {
user_is_creator "${CHAT[ID]}" "${USER[ID]}"
}
_is_allowed() {
user_is_allowed "${USER[ID]}" "$1" "${CHAT[ID]}"
user_is_allowed "${USER[ID]}" "${1}" "${CHAT[ID]}"
}
_leave() {
leave_chat "${CHAT[ID]}"
@ -32,14 +33,30 @@ _unban_user() {
}
# easy sending of messages of messages
_message() {
send_normal_message "${CHAT[ID]}" "$1"
send_normal_message "${CHAT[ID]}" "${1}"
}
_normal_message() {
send_normal_message "${CHAT[ID]}" "$1"
send_normal_message "${CHAT[ID]}" "${1}"
}
_html_message() {
send_html_message "${CHAT[ID]}" "$1"
send_html_message "${CHAT[ID]}" "${1}"
}
_markdown_message() {
send_markdown_message "${CHAT[ID]}" "$1"
send_markdown_message "${CHAT[ID]}" "${1}"
}
# easy handling of keyboards
_inline_button() {
send_inline_button "${CHAT[ID]}" "${1}" "${2}" "${3}"
}
_inline_keyboard() {
send_inline_keyboard "${CHAT[ID]}" "${1}" "${2}"
}
_keyboard_numpad() {
send_keyboard "${CHAT[ID]}" "${1}" '["1","2","3"],["4","5","6"],["7","8","9"],["-","0","."]' "yes"
}
_keyboard_yesno() {
send_keyboard "${CHAT[ID]}" "${1}" '["yes","no"]'
}
_del_keyboard() {
remove_keyboard "${CHAT[ID]}" "${1}"
}