From 6483a070b094b77e3e4ab73e96760559d65fcdd0 Mon Sep 17 00:00:00 2001 From: celso Date: Sat, 25 Mar 2023 21:01:53 -0300 Subject: [PATCH] added mkinline_kbd and reworked sendmsg --- bashbot-lib.sh | 66 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 11 deletions(-) diff --git a/bashbot-lib.sh b/bashbot-lib.sh index 28994c6..098cf38 100755 --- a/bashbot-lib.sh +++ b/bashbot-lib.sh @@ -107,24 +107,68 @@ getcbk_data() { [ ! -z "${callbacks}" ] && utf-16-surrogate-pair-decode "${callbacks:8:-2}" } -sendmsg() { - sentmsg="$(curl -sX GET "${api_url}sendMessage" -d "chat_id=${1}" \ - -d "text=${2}" \ - -d 'reply_markup={"inline_keyboard":[[{"text":"check again","callback_data":"recheck"}]]}')" \ - && [ ! -z "${sentmsg}" ] && printf "%s\n" "${sentmsg}"\ - | sed 's/^{"ok":false.*\|^{"ok":true,"result":{\|}$//g'\ - >> "${bot_tmpdir}sentmsgs.txt" +# $1 is name of a bash array with the number of columns in every row such as ( 1 1 3 ) for a 3 row keyboard in which the first and second row have 1 column, and the third has 3 +# $2 is name of a bash array with as many text entries as there are columns, they are added left to right, up to down, to the rows/columns configuration passed as first arg +# $3 is name of a bash array with as many data entries as there are columns, they are added left to right, up to down, to the rows/columns configuration passed as first arg +mkinline_kbd(){ + local -n rows="${1}" + local -n text="${2}" + local -n data="${3}" + local kbd="reply_markup={\"inline_keyboard\":[]}" + local unit="\"text\":\"\",\"callback_data\":\"\"" + local rowformat="[]" + local colformat="{}" + local rowfirstpass=1 + local colfirstpass=1 + + for (( i=0; i<"${#rows[@]}"; i++ )); do + [ "${rowfirstpass}" -eq 1 ] && { + kbd="$(sed "s/]}$/${rowformat}]}/" <<< "${kbd}")" + rowfirstpass=0 + } || kbd="$(sed "s/]}$/,${rowformat}]}/" <<< "${kbd}")" + for (( j=0; j<"${rows[${i}]}"; j++)); do + [ "${colfirstpass}" -eq 1 ] && { + kbd="$(sed "s/]]}$/${colformat}]]}/" <<< "${kbd}")" + colfirstpass=0 + } || kbd="$(sed "s/]]}$/,${colformat}]]}/" <<< "${kbd}")" + kbd="$(sed "s/}]]}$/${unit}}]]}/" <<< "${kbd}")" + done + colfirstpass=1 + done + + for i in "${text[@]}"; do + kbd="$(sed "s/\"text\":\"\"/\"text\":\"${i}\"/" <<< "${kbd}")" + done + + for i in "${data[@]}"; do + kbd="$(sed "s/\"callback_data\":\"\"/\"callback_data\":\"${i}\"/" <<< "${kbd}")" + done + sed "s/^\|$/\"/g" <<< "${kbd}" } -replymsg() { - sentmsg="$(curl -sX GET "${api_url}sendMessage" -d "chat_id=${curmsg[chat_id]}" -d "reply_to_message_id=${curmsg[message_id]}" \ - -d "text=${curmsg[text]}" \ - && [ ! -z "${sentmsg}" ] && printf "%s\n" "${sentmsg}"\ +# $1 is target chat_id +# $2 is text +# $3 is inline keyboard (optional) +sendmsg() { + [ -z "${3}" ] && sentmsg="$(curl -sX GET "${api_url}sendMessage" -d "chat_id=${1}" \ + -d "text=${2}")" \ + || sentmsg="$(curl -sX GET "${api_url}sendMessage" -d "chat_id=${1}" \ + -d "text=${2}" \ + -d "${3}")" + [ ! -z "${sentmsg}" ] && printf "%s\n" "${sentmsg}"\ | sed 's/^{"ok":false.*\|^{"ok":true,"result":{\|}$//g'\ >> "${bot_tmpdir}sentmsgs.txt" } # TODO +#replymsg() { +# sentmsg="$(curl -sX GET "${api_url}sendMessage" -d "chat_id=${1}" -d "reply_to_message_id=${2}" \ +# -d "text=${3}" \ +# -d 'reply_markup=${4}')" \ +# && [ ! -z "${sentmsg}" ] && printf "%s\n" "${sentmsg}"\ +# | sed 's/^{"ok":false.*\|^{"ok":true,"result":{\|}$//g'\ +# >> "${bot_tmpdir}sentmsgs.txt" +#} #editmsg() { # curl -sX GET "${api_url}editMessageText" -d "chat_id=${1}" -d "message_id=${2}" \ # -d "text=${3}"