Add stream parser + telegram script

This commit is contained in:
ekzyis 2023-09-09 15:16:25 +02:00
parent 6c195215db
commit 1977e7cfee
3 changed files with 60 additions and 4 deletions

View File

@ -12,13 +12,13 @@ if [ ! $# -eq 3 ]; then
exit 1
fi
if [ -z $TOKEN ]; then
echo "TOKEN must be set with OpenAI authorization bearer token (without Bearer prefix)"
if [ -z $OPENAI_TOKEN ]; then
echo "OPENAI_TOKEN must be set with OpenAI authorization bearer token (without Bearer prefix)"
exit 1
fi
if [ -z "$COOKIE" ]; then
echo "COOKIE must be set with OpenAI cookies"
if [ -z "$OPENAI_COOKIE" ]; then
echo "OPENAI_COOKIE must be set with OpenAI cookies"
exit 1
fi

14
prompt_streamparser.sh Normal file
View File

@ -0,0 +1,14 @@
#!/usr/bin/env bash
# This script parses the event stream returned by prompt queries.
# It prints individual lines per ChatGPT response update.
while read -r line
do
[ -z "$line" ] && continue
[ "$line" == "data: [DONE]" ] && continue
event=$(echo "$line" | sed -e 's/^data: //')
content=$(echo "$event" | jq -e '.message.content.parts[0]')
[ ! $? -eq 0 ] && continue
echo "$content"
done < "${1:-/dev/stdin}"

42
telegram_sender.sh Normal file
View File

@ -0,0 +1,42 @@
#!/usr/bin/env bash
# This script sends a message to Telegram.
# Every single line will be interpreted as a message update thus the message will be edited with the new content.
# exit if any command fails
usage() {
echo "Usage: $(basename $0) <chat_id> <file>"
}
if [ ! $# -eq 2 ]; then
usage
exit 1
fi
if [ -z $TELEGRAM_BOT_TOKEN ]; then
echo "TELEGRAM_BOT_TOKEN must be set"
exit 1
fi
TELEGRAM_CHAT_ID="$1"
mid=
retry_after=
while read -r line
do
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
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')
if [ $? -eq 0 ]; then
echo "Received 429 Too Many Requests. Waiting for $retry_after seconds ..."
sleep $retry_after
fi
fi
# https://core.telegram.org/bots/faq#my-bot-is-hitting-limits-how-do-i-avoid-this
sleep 1
done < "${2:-/dev/stdin}"