From 094815fd2f7739a0e00b37aabbac97ad8c0605c2 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Sat, 9 Sep 2023 18:19:48 +0200 Subject: [PATCH] Send latest response from ChatGPT after every second This is faster than waiting one second and then sending the _next_ response from ChatGPT. --- telegram_sender.sh | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/telegram_sender.sh b/telegram_sender.sh index 3268831..701663c 100644 --- a/telegram_sender.sh +++ b/telegram_sender.sh @@ -23,12 +23,19 @@ TELEGRAM_CHAT_ID="$1" mid= retry_after= +last_update=$(date +%s) +last_line= while read -r line do + last_line="$line" if [ -z $mid ]; then R=$(curl --silent "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage?disable_web_page_preview=true&chat_id=$TELEGRAM_CHAT_ID" --data-urlencode "text=$line") mid=$(echo "$R" | jq -e '.result.message_id') else + elapsed=$(( $(date +%s) - $last_update )) + if [ $(( $elapsed < 1 )) ]; then + continue + fi R=$(curl --silent "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/editMessageText?disable_web_page_preview=true&chat_id=$TELEGRAM_CHAT_ID&message_id=$mid" --data-urlencode "text=$line") # check for 429 Too Many Requests retry_after=$(echo "$R" | jq -e '.parameters.retry_after') @@ -37,6 +44,10 @@ do sleep $retry_after fi fi + last_update=$(date +%s) # https://core.telegram.org/bots/faq#my-bot-is-hitting-limits-how-do-i-avoid-this sleep 1 -done < "${2:-/dev/stdin}" \ No newline at end of file +done < "${2:-/dev/stdin}" + +# final update +curl --silent "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/editMessageText?disable_web_page_preview=true&chat_id=$TELEGRAM_CHAT_ID&message_id=$mid" --data-urlencode "text=$last_line"