implemented delmsg and fixed up comments

This commit is contained in:
celso 2023-03-26 14:15:20 -03:00
parent 9db5810f3d
commit 4574806ca6
1 changed files with 30 additions and 13 deletions

View File

@ -9,9 +9,8 @@
# bot_id="[insert your bot_id]" # bot_id="[insert your bot_id]"
# 3. the following files to read and write from # 3. the following files to read and write from
# updates.txt # updates.txt
# sentmessages.txt # sentmsgs.txt
# callbacks.txt # delmsgs.txt
# pay attention to the trailing forward slash
# 4. (optional) a directory for the bot's files as # 4. (optional) a directory for the bot's files as
# bot_tmpdir="/some/path/" # bot_tmpdir="/some/path/"
# pay attention to the trailing forward slash # pay attention to the trailing forward slash
@ -20,6 +19,7 @@
# IMPORTANT: this library uses the getUpdates method with # IMPORTANT: this library uses the getUpdates method with
# tcp_keepalive,NOT webhooks # tcp_keepalive,NOT webhooks
# attempts to grab the last update's update_id and prints it to stdout
calc_offset() { calc_offset() {
local current="$(bc -l <<< "$(tail -n 1 "${bot_tmpdir}updates.txt" | \ local current="$(bc -l <<< "$(tail -n 1 "${bot_tmpdir}updates.txt" | \
grep -om1 "\"update_id\":[0-9]\+" | \ grep -om1 "\"update_id\":[0-9]\+" | \
@ -27,6 +27,9 @@ calc_offset() {
[ ! -z "${current}" ] && printf "%d" "${current}" [ ! -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() { getupd() {
local offset="$(calc_offset)" local offset="$(calc_offset)"
local update="$(curl -sX GET --keepalive-time 300 "${api_url}getUpdates" -d "offset=${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]\+" "\"chat\":{\"id\":-\?[0-9]\+" | grep -om1 "\-\?[0-9]\+"
} }
# makes decoding emojis possible, important for sending them too
utf-16-surrogate-pair-decode() { utf-16-surrogate-pair-decode() {
# shamelessly stolen from stackoverflow # shamelessly stolen from stackoverflow
local out="$1" local out="$1"
@ -173,17 +177,30 @@ replymsg() {
[ ! -z "${sentmsg}" ] && sed 's/^{"ok":false.*\|^{"ok":true,"result":{\|}$//g'\ [ ! -z "${sentmsg}" ] && sed 's/^{"ok":false.*\|^{"ok":true,"result":{\|}$//g'\
<<< "${sentmsg}" >> "${bot_tmpdir}sentmsgs.txt" <<< "${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(){ getmsg_content(){
local -n _REF="${1}" local -n _REF="${1}"
_REF[user_id]="$(getusr_id "${2}")" _REF[user_id]="$(getusr_id "${2}")"