diff --git a/bashbot-lib.sh b/bashbot-lib.sh index bdf8dac..f736213 100755 --- a/bashbot-lib.sh +++ b/bashbot-lib.sh @@ -9,9 +9,8 @@ # bot_id="[insert your bot_id]" # 3. the following files to read and write from # updates.txt -# sentmessages.txt -# callbacks.txt -# pay attention to the trailing forward slash +# sentmsgs.txt +# delmsgs.txt # 4. (optional) a directory for the bot's files as # bot_tmpdir="/some/path/" # pay attention to the trailing forward slash @@ -20,6 +19,7 @@ # IMPORTANT: this library uses the getUpdates method with # tcp_keepalive,NOT webhooks +# attempts to grab the last update's update_id and prints it to stdout calc_offset() { local current="$(bc -l <<< "$(tail -n 1 "${bot_tmpdir}updates.txt" | \ grep -om1 "\"update_id\":[0-9]\+" | \ @@ -27,6 +27,9 @@ calc_offset() { [ ! -z "${current}" ] && printf "%d" "${current}" } +# captures output of calc_offset to use as offset and fetches a single update with that offset +# uses tcp_keepalive method for 300 seconds, but telegram only keeps the connection open for 60 seconds +# prints result of fetching update if not empty and formats it for better use with the other functions getupd() { local offset="$(calc_offset)" local update="$(curl -sX GET --keepalive-time 300 "${api_url}getUpdates" -d "offset=${offset}" \ @@ -50,6 +53,7 @@ getchat_id() { "\"chat\":{\"id\":-\?[0-9]\+" | grep -om1 "\-\?[0-9]\+" } +# makes decoding emojis possible, important for sending them too utf-16-surrogate-pair-decode() { # shamelessly stolen from stackoverflow local out="$1" @@ -173,17 +177,30 @@ replymsg() { [ ! -z "${sentmsg}" ] && sed 's/^{"ok":false.*\|^{"ok":true,"result":{\|}$//g'\ <<< "${sentmsg}" >> "${bot_tmpdir}sentmsgs.txt" } -# TODO -#editmsg() { -# curl -sX GET "${api_url}editMessageText" -d "chat_id=${1}" -d "message_id=${2}" \ -# -d "text=${3}" -#} -# -#delmsg() { -# curl -sX GET "${api_url}deleteMessage" -d "chat_id=${1}"\ -# -d "message_id=${2}" -#} +# $1 is target chat_id +# $2 is target msg_id +# $3 is text +# $4 is inline keyboard (optional) +editmsg() { + local editmsg + [ -z "${3}" ] && editmsg="$(curl -sX GET "${api_url}editMessageText" -d "chat_id=${1}" -d "message_id=${2}" \ + -d "text=${3}")" || editmsg="$(curl -sX GET "${api_url}editMessageText" -d "chat_id=${1}" -d "message_id=${2}" \ + -d "text=${3}" -d "reply_markup=${4}")" + [ ! -z "${editmsg}" ] && sed 's/^{"ok":false.*\|^{"ok":true,"result":{\|}$//g'\ + <<< "${editmsg}" >> "${bot_tmpdir}sentmsgs.txt" +} + +# $1 is target chat_id +# $2 is target msg_id +delmsg() { + local delmsg + delmsg="$(curl -sX GET "${api_url}deleteMessage" -d "chat_id=${1}"\ + -d "message_id=${2}")" + [ ! -z "${delmsg}" ] && printf "deleted message %s from chat %s\n" "${2}" "${1}" >> "${bot_tmpdir}delmsgs.txt" +} + +# $1 is the name of a bash associative array to be filled with the message contents getmsg_content(){ local -n _REF="${1}" _REF[user_id]="$(getusr_id "${2}")"