From 1977e7cfeec09aa39b4ce767c8b843e698285aa2 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Sat, 9 Sep 2023 15:16:25 +0200 Subject: [PATCH] Add stream parser + telegram script --- prompt.sh | 8 ++++---- prompt_streamparser.sh | 14 ++++++++++++++ telegram_sender.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 prompt_streamparser.sh create mode 100644 telegram_sender.sh diff --git a/prompt.sh b/prompt.sh index 723ac9d..de60d97 100644 --- a/prompt.sh +++ b/prompt.sh @@ -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 diff --git a/prompt_streamparser.sh b/prompt_streamparser.sh new file mode 100644 index 0000000..00877b4 --- /dev/null +++ b/prompt_streamparser.sh @@ -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}" diff --git a/telegram_sender.sh b/telegram_sender.sh new file mode 100644 index 0000000..37b893d --- /dev/null +++ b/telegram_sender.sh @@ -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) " +} + +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}" \ No newline at end of file