gpt2tg/telegram_poll.sh

41 lines
1.3 KiB
Bash

#!/usr/bin/env bash
usage() {
echo "Usage: $(basename $0) [offset]"
}
OFFSET=$1
if [ -z $TELEGRAM_BOT_TOKEN ]; then
echo "TELEGRAM_BOT_TOKEN must be set with OpenAI authorization bearer token (without Bearer prefix)"
exit 1
fi
TIMEOUT=5
while true
do
echo "Long polling for telegram updates with offset $OFFSET ..."
R=$(curl --silent "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/getUpdates?timeout=$TIMEOUT&offset=$OFFSET")
echo "$R" | jq
update_id="$(echo "$R" | jq -e '.result[0].update_id')"
if [ ! $? -eq 0 ]; then
echo "Timeout"
continue
fi
text=$(echo "$R" | jq -e '.result[0].message.text')
if [ $? -eq 0 ]; then
text=$(echo "$text" | sed -e 's/^"//' -e 's/"$//')
if [[ $text = /prompt* ]]; then
prompt="${text:8}"
chat_id="$(echo "$R" | jq -e '.result[0].message.chat.id')"
echo "Received prompt "$prompt" in chat $chat_id"
# TODO: update parent message id such that GPT knows context of previous requests
gpt2tg_prompt 9e76b8fc-e37e-4dcb-8d8d-d074e5480047 60b11ad6-25c1-4209-b6b4-84482dd774fa "$prompt" | \
gpt2tg_prompt_streamparser | \
gpt2tg_telegram_sender $chat_id
fi
fi
OFFSET=$(( update_id + 1))
done