Send latest response from ChatGPT after every second

This is faster than waiting one second and then sending the _next_ response from ChatGPT.
This commit is contained in:
ekzyis 2023-09-09 18:19:48 +02:00
parent d8e8d5ab8c
commit 094815fd2f
1 changed files with 12 additions and 1 deletions

View File

@ -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}"
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"