topkecleon / telegram-bot-bash

Telegram bot written in bash
Other
426 stars 123 forks source link

editMessage example #162

Closed jonashaag closed 3 years ago

jonashaag commented 3 years ago

I use editMessage to maintain a "self-updating" message with server stats. I added this:

diff --git a/modules/sendMessage.sh b/modules/sendMessage.sh
index fc9d195..299ecf9 100644
--- a/modules/sendMessage.sh
+++ b/modules/sendMessage.sh
@@ -15,6 +15,7 @@ eval "$(basename "${BASH_SOURCE[0]}")(){ :; }"
 # source from commands.sh to use the sendMessage functions

 MSG_URL=$URL'/sendMessage'
+EDIT_MSG_URL=$URL'/editMessageText'
 PHO_URL=$URL'/sendPhoto'
 AUDIO_URL=$URL'/sendAudio'
 DOCUMENT_URL=$URL'/sendDocument'
@@ -69,6 +70,19 @@ send_markdownv2_message() {
    done
 }

+# $1 CHAT $2 msg id $3 message
+edit_markdownv2_message() {
+   local text; text="$(JsonEscape "${3}")"
+   text="${text//$'\n'/\\n}"
+   [ "${#text}" -ge 4096 ] && log_error "Warning: markdown message longer than 4096 characters, message is rejected if formatting crosses 4096 border."
+   # markdown v2 needs additional double escaping!
+   text="$(sed -E -e 's|([#{}()!.-])|\\\1|g' <<< "$text")"
+   until [ -z "${text}" ]; do
+       sendJson "${1}" '"message_id":'${2}',"text":"'"${text:0:4096}"'","parse_mode":"markdownv2"' "${EDIT_MSG_URL}"
+       text="${text:4096}"
+   done
+}
+
 # $1 CHAT $2 message
 send_html_message() {
    local text; text="$(JsonEscape "${2}")"

The script I use (see below for the definition of $msg):

MSGID=24
source bashbot.sh
edit_markdownv2_message "$(getConfigKey "botadmin")" $MSGID "$msg"

You have to get the message id (24 in my example) from somewhere. Not sure where you're supposed to get it from; I got it from MESSAGE.log with debug mode.

Just posting this here in case someone finds it useful.

Code for $msg:

gpu=`nvidia-smi --query-gpu=utilization.gpu --format=csv,noheader,nounits`
cpu=`uptime | egrep -o 'load.+' | cut -d ' ' -f 3- | sed s/,[0-9]/.\0/g`
mem=`free -h | grep Mem | sd '\s+' ' ' | cut -d ' ' -f3`
df=`df -h /mnt /mnt2 / | tail -n+2`

msg=$(cat <<EOF
\`\`\`
GPU: $gpu%
CPU: $cpu
Mem: $mem

Disks
-----
$df
\`\`\`
EOF
)
gnadelwartz commented 3 years ago

if you send a message you get the result in BOTSENT ... hm looks like I did not docment this.

MessageID can be found in ${BOTSENT[ID]}, e.g. see https://github.com/topkecleon/telegram-bot-bash/blob/6dc8f1b267e8604651e34f3621f3f7605cf8d3f1/mycommands.sh#L154

jonashaag commented 3 years ago

Good to know, thanks!

Feel free to close this issue if you like.

gnadelwartz commented 3 years ago

thanks for the edit Message example implementation. I'll move it into the new file, modules/editMessage.sh, so all edit Message variants will be togther in one file later on.

gnadelwartz commented 3 years ago

BOTSENT https://github.com/topkecleon/telegram-bot-bash/commit/5db2ef6f3071861466a0de9511a4c7965373ba52

gnadelwartz commented 3 years ago

ich dachte schon Jonas Haag heist doch ein Sohn vom Weingut Haag, Karlsruhe ist aber ein Stück weg. Gruß aus Worms

gnadelwartz commented 3 years ago

First try of an edit_xxx_message implementation https://github.com/topkecleon/telegram-bot-bash/blob/a25f876a06701dca837b2268e4b06f740511c2ff/modules/sendMessage.sh#L54-L114

thanks for your example implemenatation ...