added mkinline_kbd and reworked sendmsg

This commit is contained in:
celso 2023-03-25 21:01:53 -03:00
parent d0f9ebf71c
commit 6483a070b0
1 changed files with 55 additions and 11 deletions

View File

@ -107,24 +107,68 @@ getcbk_data() {
[ ! -z "${callbacks}" ] && utf-16-surrogate-pair-decode "${callbacks:8:-2}" [ ! -z "${callbacks}" ] && utf-16-surrogate-pair-decode "${callbacks:8:-2}"
} }
sendmsg() { # $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
sentmsg="$(curl -sX GET "${api_url}sendMessage" -d "chat_id=${1}" \ # $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
-d "text=${2}" \ # $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
-d 'reply_markup={"inline_keyboard":[[{"text":"check again","callback_data":"recheck"}]]}')" \ mkinline_kbd(){
&& [ ! -z "${sentmsg}" ] && printf "%s\n" "${sentmsg}"\ local -n rows="${1}"
| sed 's/^{"ok":false.*\|^{"ok":true,"result":{\|}$//g'\ local -n text="${2}"
>> "${bot_tmpdir}sentmsgs.txt" 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() { # $1 is target chat_id
sentmsg="$(curl -sX GET "${api_url}sendMessage" -d "chat_id=${curmsg[chat_id]}" -d "reply_to_message_id=${curmsg[message_id]}" \ # $2 is text
-d "text=${curmsg[text]}" \ # $3 is inline keyboard (optional)
&& [ ! -z "${sentmsg}" ] && printf "%s\n" "${sentmsg}"\ 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'\ | sed 's/^{"ok":false.*\|^{"ok":true,"result":{\|}$//g'\
>> "${bot_tmpdir}sentmsgs.txt" >> "${bot_tmpdir}sentmsgs.txt"
} }
# TODO # 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() { #editmsg() {
# curl -sX GET "${api_url}editMessageText" -d "chat_id=${1}" -d "message_id=${2}" \ # curl -sX GET "${api_url}editMessageText" -d "chat_id=${1}" -d "message_id=${2}" \
# -d "text=${3}" # -d "text=${3}"