Last active
February 28, 2023 16:13
-
-
Save vitouXY/06499aa32746b3f8b1f768a42d590d34 to your computer and use it in GitHub Desktop.
Telegram Bot & OpenAI Chat - Bash Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
#set -x | |
# Run As: | |
# while :;do echo -en "$(bash telegramBot_chatGPT.bash)\t\t\t\t\r";done | |
# # https://telegram.me/BotFather | |
DATA_Telegram_Token='' | |
# Login: https://beta.openai.com | https://chat.openai.com/auth/login | |
# And access to: https://chat.openai.com/api/auth/session | |
# Then copy the FULL value of "accessToken" and paste it into the file. (Expires) | |
DATA_File_accesToken='accessToken.chatBLog' | |
# [!] Cloudflare | |
# Use a cookie manager in your browser to get the value of "cf_clearance". | |
# The cookie is linked to your IP and UserAgent. | |
# Make sure you use the same IP and UserAgent as when you got it. | |
DATA_File_Cookie_cf_clearance='cf_clearance.chatBlog' | |
# | |
# https://wtfismyip.com/headers | |
DATA_File_user_agent='user_agent.chatBLog' | |
################# | |
unset CURL JQ SED ECHO DATE CAT WC SEQ TAIL STAT RM SLEEP | |
LAST_Telegram_message_message_id=0 | |
CURL_EXTRAo='-sSL' | |
CURL_EXTRAt='-fsSL' | |
DATA_Telegram_URL="https://api.telegram.org/bot${DATA_Telegram_Token:?}" | |
if [ -f "${DATA_File_accesToken}" ];then | |
DATA_OpenAI_accessToken="$(cat ${DATA_File_accesToken} |${SED:-sed} -e 's#^"##g' -e 's#"$##g')" | |
if [[ ! -n "${DATA_OpenAI_accessToken}" ]];then exit 2;fi | |
else exit 2;fi | |
if [[ -f "${DATA_File_user_agent}" && -f "${DATA_File_Cookie_cf_clearance}" ]];then | |
DATA_OpenAI_user_agent="$(cat ${DATA_File_user_agent})" | |
DATA_OpenAI_Cookie_cf_clearance="$(cat ${DATA_File_Cookie_cf_clearance})" | |
DATA_OpenAI_Cookies="cf_clearance=${DATA_OpenAI_Cookie_cf_clearance}" | |
if [[ -z "${DATA_OpenAI_user_agent}" || -z "${DATA_OpenAI_Cookie_cf_clearance}" ]];then exit 2;fi | |
else exit 2;fi | |
function aiUse_OpenAI(){ | |
unset OUTPUT_OpenAI_response DATA_OpenAI_message_conversation_message_content_parts DATA_OpenAI_conversation_id DATA_OpenAI_messages_id DATA_OpenAI_parent_message_id DATA_OpenAI_datail | |
#$(${CURL:-curl} -fsSL --request GET --url 'https://chat.openai.com/backend-api/models' --header "Authorization: Bearer ${DATA_OpenAI_accessToken:?}" --header 'Connection: close' --header 'Accept-Language: en-US,en;q=0.9' --header 'Host: chat.openai.com' --header 'Authority: chat.openai.com' --header 'Accept: text/event-stream' --header 'Origin: https://chat.openai.com' --header 'Referer: https://chat.openai.com/chat' --header 'sec-ch-ua: "Not?A_Brand";v="8", "Chromium";v="108", "Google Chrome";v="108"' --header 'sec-ch-ua-mobile: ?0' --header 'sec-ch-ua-platform: macOS' --header 'sec-fetch-dest: empty' --header 'sec-fetch-mode: cors' --header 'sec-fetch-site: same-origin' --header 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36' --header 'X-Openai-Assistant-App-Id": ' |${JQ-jq} '.models[0].slug' - 2>/dev/null |${SED:-sed} -e 's#^"##g' -e 's#"$##g') | |
DATA_OpenAI_model="text-davinci-002-render" | |
DATA_OpenAI_URL="https://chat.openai.com/backend-api/conversation" | |
# Goog luck... | |
DATA_OpenAI_messages_id="${DATA_OpenAI_messages_id_prev:-$(${CAT:-cat} /proc/sys/kernel/random/uuid)}" | |
DATA_OpenAI_parent_message_id="${DATA_OpenAI_parent_message_id_prev:-$(${CAT:-cat} /proc/sys/kernel/random/uuid)}" | |
# # action :? next variant | |
# # message.role : 'unknown', 'user', 'assistant', 'system', 'critic', 'tool' | |
# # message.content.content_type : 'text', 'code', 'tether_browsing_code', 'tether_browsing_display', 'tether_quote', 'error', 'stdout', 'stderr', 'image', 'execution_output', 'masked_code', 'masked_text', 'unit_test_result', 'system_error' | |
OUTPUT_OpenAI_response=$(${CURL:-curl} ${CURL_EXTRAo:-} --request POST \ | |
--url "${DATA_OpenAI_URL:?}" \ | |
--user-agent "${DATA_OpenAI_user_agent:?}" \ | |
--header "Cookie: ${DATA_OpenAI_Cookies:?}" \ | |
--header "Authorization: Bearer ${DATA_OpenAI_accessToken:?}" \ | |
--header 'Connection: close' \ | |
--header 'Accept-Language: en-US,en;q=0.9' \ | |
--header 'Host: chat.openai.com' \ | |
--header 'Authority: chat.openai.com' \ | |
--header 'Accept: text/event-stream' \ | |
--header 'Origin: https://chat.openai.com' \ | |
--header 'Referer: https://chat.openai.com/chat' \ | |
--header 'X-Openai-Assistant-App-Id": ' \ | |
--header 'Content-Type: application/json' \ | |
--data "{ | |
${DATA_OpenAI_conversation_id_prev:-}\"action\": \"variant\", | |
\"model\": \"${DATA_OpenAI_model:?}\", | |
\"messages\": [{ | |
\"id\": \"${DATA_OpenAI_messages_id:?}\", | |
\"role\": \"user\", | |
\"content\": {\"content_type\": \"text\", \"parts\": [\"${DATA_Text_ask:-}\"]} | |
}], | |
\"parent_message_id\": \"${DATA_OpenAI_parent_message_id:?}\" | |
}") | |
DATA_OpenAI_datail=$(${ECHO:-echo} "${OUTPUT_OpenAI_response:-}" |${JQ:-jq} '.detail.message' - 2>/dev/null |${TAIL:-tail} -n1 |${SED:-sed} -e 's#^"##g' -e 's#"$##g' -e 's#^[\\n]\+##g') | |
if [ -n "${DATA_OpenAI_datail}" ];then echo "${DATA_OpenAI_datail:-}";else | |
DATA_OpenAI_message_conversation_message_content_parts=$(${ECHO:-echo} "${OUTPUT_OpenAI_response:-}" |${SED:-sed} -e 's#^data: ##g' |${SED:-sed} -e 's#^\[DONE\]$##g' |${JQ:-jq} '.message.content.parts[]' - 2>/dev/null |${TAIL:-tail} -n1 |${SED:-sed} -e 's#^"##g' -e 's#"$##g' -e 's#^[\\n]\+##g') | |
DATA_OpenAI_conversation_id=$(${ECHO:-echo} "${OUTPUT_OpenAI_response:-}" |${SED:-sed} -e 's#^data: ##g' |${SED:-sed} -e 's#^\[DONE\]$##g' |${JQ:-jq} '.conversation_id' - 2>/dev/null |${TAIL:-tail} -n1 |${SED:-sed} -e 's#^"##g' -e 's#"$##g' -e 's#^[\\n]\+##g') | |
fi | |
} | |
function telegram_getUpdates(){ | |
unset OUTPUT_Telegram_getUpdates DATA_Telegram_getUpdates_ok | |
OUTPUT_Telegram_getUpdates=$(${CURL:-curl} ${CURL_EXTRAt:-} --request POST \ | |
--url "${DATA_Telegram_URL:?}/getUpdates" \ | |
--data allowed_updates=message) | |
DATA_Telegram_getUpdates_ok=$(${ECHO:-echo} "${OUTPUT_Telegram_getUpdates:-}" |${JQ:-jq} '.ok' - 2>/dev/null) | |
if [[ "${DATA_Telegram_getUpdates_ok:-}" == 'true' ]];then | |
${ECHO:-echo} -en "\e[K"'* Updated... '"\t\t\t\t\r" | |
fi | |
} | |
function telegram_deleteMessage(){ | |
unset OUTPUT_Telegram_deleteMessage DATA_Telegram_deleteMessage_ok | |
OUTPUT_Telegram_deleteMessage=$(${CURL:-curl} ${CURL_EXTRAt:-} --request POST \ | |
--url "${DATA_Telegram_URL:?}/deleteMessage" \ | |
--data message_id=${DATA_Telegram_message_message_id:-} \ | |
--data chat_id=${DATA_Telegram_message_chat_id:-}) | |
DATA_Telegram_deleteMessage_ok=$(${ECHO:-echo} "${OUTPUT_Telegram_deleteMessage:-}" |${JQ:-jq} '.ok' - 2>/dev/null) | |
if [[ "${DATA_Telegram_deleteMessage_ok:-}" == 'true' ]];then | |
${ECHO:-echo} -en "\e[K"'* Deleted... '"${DATA_Telegram_info:-}""\t\t\t\t\r" | |
fi | |
} | |
function telegram_sendMessage(){ | |
OUTPUT_Telegram_sendMessage=$(${CURL:-curl} ${CURL_EXTRAt:-} --request POST \ | |
--url "${DATA_Telegram_URL:?}/sendMessage" \ | |
--data chat_id=${DATA_Telegram_message_chat_id:-} \ | |
--data text="${DATA_Text_response:-}") | |
DATA_Telegram_sendMessage_ok=$(${ECHO:-echo} "${OUTPUT_Telegram_sendMessage:-}" |${JQ:-jq} '.ok' - 2>/dev/null) | |
if [[ "${DATA_Telegram_sendMessage_ok:-}" == 'true' ]];then | |
${ECHO:-echo} -en "\e[K"'* Answered...'"${DATA_Telegram_info:-}""\t\t\t\t\r" | |
fi | |
} | |
function sendAnswer(){ | |
${ECHO:-echo} -en "\e[K"'* Sending...'"${DATA_Telegram_info:-}""\t\t\t\t\r" | |
unset DATA_Text_response | |
if [[ -n "${DATA_OpenAI_message_conversation_message_content_parts}" && -n "${DATA_OpenAI_conversation_id}" ]];then | |
DATA_Text_response=$(${ECHO:-echo} "${DATA_OpenAI_message_conversation_message_content_parts:-}" |${SED:-sed} -e 's#\\n$##g') | |
telegram_sendMessage | |
if [[ "${DATA_Telegram_sendMessage_ok:-}" == 'true' ]];then | |
${ECHO:-echo} "DATA_OpenAI_conversation_id_prev=${DATA_OpenAI_conversation_id:-}" > "${DATA_Name_chatLogFile:?}" | |
#${ECHO:-echo} "DATA_OpenAI_messages_id_prev=${DATA_OpenAI_messages_id:-}" >> "${DATA_Name_chatLogFile:?}" | |
${ECHO:-echo} "DATA_OpenAI_parent_message_id_prev=${DATA_OpenAI_parent_message_id:-}" >> "${DATA_Name_chatLogFile:?}" | |
${SED:-sed} -i 's#^\(LAST_Telegram_message_message_id.\+\)#LAST_Telegram_message_message_id='${DATA_Telegram_message_message_id:?}'#g' ${0:?} | |
${ECHO:-echo} -en "\e[K"'* Done...'"${DATA_Telegram_info:-}""\t\t\t\t\r\n\r" | |
fi | |
else | |
${ECHO:-echo} -en "\e[K"'* Failed...'"${DATA_Telegram_info:-}""\t\t\t\t\r\n\r" | |
fi | |
} | |
function getAnswer(){ | |
${ECHO:-echo} -en "\e[K"'* Asking...'"${DATA_Telegram_info:-}""\t\t\t\t\r" | |
unset DATA_Content_chatLogFile DATA_OpenAI_conversation_id_prev DATA_OpenAI_messages_id_prev DATA_OpenAI_parent_message_id_prev | |
if [ -f "${DATA_Name_chatLogFile:-}" ];then | |
DATA_Content_chatLogFile=$(${CAT:-cat} "${DATA_Name_chatLogFile:?}") | |
if [ -n "${DATA_Content_chatLogFile}" ];then | |
source "${DATA_Name_chatLogFile:?}" | |
DATA_OpenAI_conversation_id_prev="\"conversation_id\": \"${DATA_OpenAI_conversation_id_prev:-}\", " | |
fi | |
fi | |
aiUse_OpenAI | |
} | |
function choiceMessage(){ | |
telegram_getUpdates | |
${ECHO:-echo} -en "\e[K"'* Choosing...'"${LAST_Telegram_message_message_id:-}+""\t\t\t\t\r" | |
unset INDX LIST_Telegram_message_message_id LENGTH_Telegram_message_message_id | |
LIST_Telegram_message_message_id=$(${ECHO:-echo} "${OUTPUT_Telegram_getUpdates:-}" |${JQ:-jq} '.result[].message.message_id' - 2>/dev/null) | |
LENGTH_Telegram_message_message_id=$(${ECHO:-echo} -e "${LIST_Telegram_message_message_id:-}" | ${WC:-wc} -l) | |
for INDX in $(${SEQ:-seq} 0 $((LENGTH_Telegram_message_message_id-1)));do | |
unset DATA_Telegram_message_message_id DATA_Telegram_message_chat_id DATA_Telegram_message_chat_username DATA_Telegram_message_text DATA_Telegram_message_date DATA_Telegram_info | |
DATA_Telegram_message_message_id=$(${ECHO:-echo} "${OUTPUT_Telegram_getUpdates:-}" |${JQ:-jq} ".result[${INDX:?}].message.message_id" - 2>/dev/null) | |
if [[ ${DATA_Telegram_message_message_id:-} -gt ${LAST_Telegram_message_message_id:-} ]];then | |
DATA_Telegram_message_chat_id=$(${ECHO:-echo} "${OUTPUT_Telegram_getUpdates:-}" |${JQ:-jq} ".result[${INDX:-}].message.chat.id" - 2>/dev/null) | |
DATA_Telegram_message_chat_username=$(${ECHO:-echo} "${OUTPUT_Telegram_getUpdates:-}" |${JQ:-jq} ".result[${INDX:-}].message.chat.username" - 2>/dev/null |${SED:-sed} -e 's#"##g') | |
DATA_Telegram_message_text=$(${ECHO:-echo} "${OUTPUT_Telegram_getUpdates:-}" |${JQ:-jq} ".result[${INDX:-}].message.text" - 2>/dev/null) | |
DATA_Telegram_message_date=$(${ECHO:-echo} "${OUTPUT_Telegram_getUpdates:-}" |${JQ:-jq} ".result[${INDX:-}].message.date" - 2>/dev/null) | |
DATA_Telegram_info="${DATA_Telegram_message_message_id:-}:${DATA_Telegram_message_chat_id:-}:${DATA_Telegram_message_chat_username:-}" | |
workMessage | |
fi | |
${SLEEP:-sleep} 0.1s | |
done | |
} | |
function workMessage(){ | |
if [[ -n ${DATA_Telegram_message_message_id} \ | |
&& -n ${DATA_Telegram_message_chat_id} \ | |
&& -n ${DATA_Telegram_message_chat_username} \ | |
&& -n "${DATA_Telegram_message_text}" \ | |
&& -n ${DATA_Telegram_message_date} ]];then | |
${ECHO:-echo} -en "\e[K"'* Trying...'"${DATA_Telegram_info:-}""\t\t\t\t\r" | |
unset DATA_Date_epochNow DATA_Name_chatLogFile DATA_Date_chatLogFile DATA_Text_ask | |
DATA_Date_epochNow=$(${DATE:-date} +%s) | |
if [[ $((DATA_Date_epochNow-DATA_Telegram_message_date)) -ge 86400 ]];then | |
telegram_deleteMessage | |
else | |
DATA_Name_chatLogFile="${DATA_Telegram_message_chat_id:?}.chatBLog" | |
if [ -f "${DATA_Name_chatLogFile:-}" ];then | |
DATA_Date_chatLogFile=$(${STAT:-stat} -c %Y "${DATA_Name_chatLogFile:?}") | |
if [[ $((DATA_Date_epochNow-DATA_Date_chatLogFile)) -ge 43200 ]];then | |
${RM:-rm} "${DATA_Name_chatLogFile:?}" | |
fi | |
fi | |
DATA_Text_ask=$(${ECHO:-echo} "${DATA_Telegram_message_text:-}" |${SED:-sed} -e ':a;N;$!ba;s/\n/\\n/g' |${SED:-sed} -e 's#^"##g' -e 's#"$##g' -e 's#^[\\n]\+##g') | |
case "${DATA_Text_ask,,}x" in | |
'x'|'/helpx') | |
DATA_Text_ask="¡¡Necesito tu ayuda!!" | |
getAnswer ; sendAnswer | |
;; | |
'/startx') | |
if [ -f "${DATA_Name_chatLogFile:-}" ];then | |
${RM:-rm} "${DATA_Name_chatLogFile:?}" | |
fi | |
DATA_Text_ask="¡Hola, soy un humano y mi apodo es \\\"${DATA_Telegram_message_chat_username:-}\\\"!" | |
getAnswer ; sendAnswer | |
;; | |
'/stopx'|'/restartx') | |
if [ -f "${DATA_Name_chatLogFile:-}" ];then | |
${RM:-rm} "${DATA_Name_chatLogFile:?}" | |
fi | |
;; | |
*) | |
getAnswer ; sendAnswer | |
;; | |
esac | |
fi | |
fi | |
} | |
choiceMessage | |
${SLEEP:-sleep} 0.1s | |
################# | |
# # https://chat.openai.com/ | |
# # https://beta.openai.com/account/api-keys | |
# # https://beta.openai.com/docs/api-reference/models | |
# # https://beta.openai.com/docs/api-reference/completions/create | |
# # https://beta.openai.com/docs/models/gpt-3 | |
# # https://core.telegram.org/bots | |
# # https://telegram.me/BotFather | |
# # https://core.telegram.org/bots/api | |
# # https://nlpcloud.com/home/token | |
# # https://docs.nlpcloud.com/#code-generation | |
# #HttpOnly_.chat.openai.com\tTRUE\t/\tTRUE\t${DATA_OpenAI_Cookie_cf_clearance_epoch_expires}\tcf_clearance\t${DATA_OpenAI_Cookie_cf_clearance:?} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -x | |
# Run As: | |
# while :;do echo -en "$(bash TelegramBotAI.sh)\t\t\t\t\r";done | |
DATA_Telegram_Token='' | |
DATA_AI_OpenAI_Key='' | |
#DATA_AI_NLPCloud_Token='' | |
################# | |
LAST_Telegram_message_message_id=0 | |
DATA_Telegram_URL="https://api.telegram.org/bot${DATA_Telegram_Token:?}" | |
function aiUse_OpenAI(){ | |
unset OUTPUT_AI_OpenAI_response DATA_AI_response | |
DATA_AI_OpenAI_model="text-davinci-003" | |
DATA_AI_OpenAI_max_tokens=50 # 4000 | |
DATA_AI_OpenAI_URL="https://api.openai.com/v1/completions" | |
OUTPUT_AI_OpenAI_response=$(${CURL:-curl} -fsSL --request POST \ | |
--url "${DATA_AI_OpenAI_URL:?}" \ | |
--header "Content-Type: application/json" \ | |
--data "{ | |
\"stop\":[\"${DATA_Telegram_message_chat_username:-You}:\"], | |
\"echo\":false, | |
\"n\":1, | |
\"max_tokens\":${DATA_AI_OpenAI_max_tokens:-50}, | |
\"frequency_penalty\":0.5, | |
\"presence_penalty\":0.0, | |
\"top_p\":1.0, | |
\"temperature\":0.5, | |
\"best_of\":1, | |
\"logprobs\":null, | |
\"stream\":false, | |
\"model\":\"${DATA_AI_OpenAI_model:?}\", | |
\"prompt\":\"${DATA_Text_ask:-}\" | |
}" --header "Authorization: Bearer ${DATA_AI_OpenAI_Key:?}") | |
DATA_AI_response=$(echo "${OUTPUT_AI_OpenAI_response:-}" |${JQ:-jq} '.choices[0].text' - 2>/dev/null |${SED:-sed} -e 's#^"##g' -e 's#"$##g' -e 's#^[\\n]\+##g') | |
} | |
function aiUse_NLPCloud(){ | |
unset OUTPUT_AI_NLPCloud_response DATA_AI_response | |
# es_core_news_lg gpt-j gpt-neox-20b fast-gpt-j | |
DATA_AI_NLPCloud_model="gpt-j" | |
DATA_AI_NLPCloud_max_length=50 # 1024 | |
DATA_AI_NLPCloud_URL="https://api.nlpcloud.io/v1/gpu/${DATA_AI_NLPCloud_model:?}/generation" | |
OUTPUT_AI_NLPCloud_response=$(${CURL:-curl} -fsSL --request POST \ | |
--url "${DATA_AI_NLPCloud_URL:?}" \ | |
--header "Content-Type: application/json" \ | |
--data "{ | |
\"top_p\":1.0, | |
\"temperature\":0.5, | |
\"end_sequence\":\"###\", | |
\"remove_end_sequence\":true, | |
\"length_no_input\":true, | |
\"remove_input\":true, | |
\"min_length\":1, | |
\"max_length\":${DATA_AI_NLPCloud_max_length:-50}, | |
\"text\":\"${DATA_Text_ask:-}\" | |
}" --header "Authorization: Token ${DATA_AI_NLPCloud_Token:?}") | |
DATA_AI_response=$(echo "${OUTPUT_AI_NLPCloud_response:-}" |${JQ:-jq} '.generated_text' - 2>/dev/null |${SED:-sed} -e 's#^"##g' -e 's#"$##g' -e 's#^[\\n]\+##g') | |
} | |
function telegram_getUpdates(){ | |
echo '* Updating...' | |
unset OUTPUT_Telegram_getUpdates | |
OUTPUT_Telegram_getUpdates=$(${CURL:-curl} -fsSL --request POST \ | |
--url "${DATA_Telegram_URL:?}/getUpdates" \ | |
--data allowed_updates=message) | |
} | |
function telegram_deleteMessage(){ | |
unset OUTPUT_Telegram_deleteMessage DATA_Telegram_deleteMessage_ok | |
OUTPUT_Telegram_deleteMessage=$(${CURL:-curl} -fsSL --request POST \ | |
--url "${DATA_Telegram_URL:?}/deleteMessage" \ | |
--data message_id=${DATA_Telegram_message_message_id:-} \ | |
--data chat_id=${DATA_Telegram_message_chat_id:-}) | |
DATA_Telegram_deleteMessage_ok=$(echo "${OUTPUT_Telegram_deleteMessage:-}" |${JQ:-jq} '.ok' - 2>/dev/null) | |
if [[ "${DATA_Telegram_deleteMessage_ok:-}" == 'true' ]];then | |
echo '* Deleted... '"${DATA_Telegram_info:-}" | |
fi | |
} | |
function telegram_sendMessage(){ | |
OUTPUT_Telegram_sendMessage=$(${CURL:-curl} -fsSL --request POST \ | |
--url "${DATA_Telegram_URL:?}/sendMessage" \ | |
--data chat_id=${DATA_Telegram_message_chat_id:-} \ | |
--data text="${DATA_Text_response:-}") | |
DATA_Telegram_sendMessage_ok=$(echo "${OUTPUT_Telegram_sendMessage:-}" |${JQ:-jq} '.ok' - 2>/dev/null) | |
if [[ "${DATA_Telegram_sendMessage_ok:-}" == 'true' ]];then | |
echo '* Answered...'"${DATA_Telegram_info:-}" | |
fi | |
} | |
function sendAnswer(){ | |
echo '* Sending...'"${DATA_Telegram_info:-}" | |
unset DATA_Text_response | |
if [[ -n "${DATA_AI_response}" ]];then | |
#_ | |
DATA_Text_response=$(echo "${DATA_AI_response:-}" |${SED:-sed} -e 's#Victoria: ##g' |${SED:-sed} -e 's#\\n$##g') | |
#DATA_Text_response=$(echo "${DATA_AI_response:-}" |${SED:-sed} -e 's#Victor: ##g' |${SED:-sed} -e 's#\\n$##g') | |
telegram_sendMessage | |
if [[ "${DATA_Telegram_sendMessage_ok:-}" == 'true' ]];then | |
#_ | |
echo "${DATA_Text_ask:-}${DATA_AI_response:-}" > "${DATA_Name_chatLogFile:?}" | |
#echo "${DATA_Text_ask:-}${DATA_Text_response:-}\n###" > "${DATA_Name_chatLogFile:?}" | |
${SED:-sed} -i 's#^\(LAST_Telegram_message_message_id.\+\)#LAST_Telegram_message_message_id='${DATA_Telegram_message_message_id:?}'#g' ${0:?} | |
echo '* Done...'"${DATA_Telegram_info:-}" | |
fi | |
else | |
echo '* Failed...'"${DATA_Telegram_info:-}" | |
fi | |
} | |
function getAnswer(){ | |
echo '* Asking...'"${DATA_Telegram_info:-}" | |
unset DATA_Content_chatLogFile DATA_Text_ask | |
DATA_Content_chatLogFile=$(cat "${DATA_Name_chatLogFile:?}") | |
#_ | |
DATA_Text_ask=$(echo "${DATA_Content_chatLogFile:-}\n${DATA_Telegram_message_chat_username:-You}: ${DATA_Telegram_message_text:-}\nVictoria:" |${SED:-sed} -e ':a;N;$!ba;s/\n/\\n/g' |${SED:-sed} -e 's#"##g') | |
#DATA_Text_ask=$(echo "${DATA_Content_chatLogFile:-}\n${DATA_Telegram_message_chat_username:-You}: ${DATA_Telegram_message_text:-}\nVictor:" |${SED:-sed} -e ':a;N;$!ba;s/\n/\\n/g' |${SED:-sed} -e 's#"##g') | |
#_ | |
aiUse_OpenAI | |
#aiUse_NLPCloud | |
} | |
function choiceMessage(){ | |
telegram_getUpdates | |
echo '* Choosing...'"${LAST_Telegram_message_message_id:-}+" | |
unset INDX LIST_Telegram_message_message_id LENGTH_Telegram_message_message_id | |
unset DATA_Telegram_message_message_id DATA_Telegram_message_chat_id DATA_Telegram_message_chat_username DATA_Telegram_message_text DATA_Telegram_message_date DATA_Telegram_info | |
LIST_Telegram_message_message_id=$(echo "${OUTPUT_Telegram_getUpdates:-}" |${JQ:-jq} '.result[].message.message_id' - 2>/dev/null) | |
LENGTH_Telegram_message_message_id=$(echo -e "${LIST_Telegram_message_message_id:-}" | wc -l) | |
for INDX in $(seq 0 $((LENGTH_Telegram_message_message_id-1)));do | |
DATA_Telegram_message_message_id=$(echo "${OUTPUT_Telegram_getUpdates:-}" |${JQ:-jq} ".result[${INDX:--1}].message.message_id" - 2>/dev/null) | |
if [[ ${DATA_Telegram_message_message_id:-} -gt ${LAST_Telegram_message_message_id:-} && ${LAST_Telegram_message_message_id:-} -ge 0 ]];then | |
DATA_Telegram_message_chat_id=$(echo "${OUTPUT_Telegram_getUpdates:-}" |${JQ:-jq} ".result[${INDX:--1}].message.chat.id" - 2>/dev/null) | |
DATA_Telegram_message_chat_username=$(echo "${OUTPUT_Telegram_getUpdates:-}" |${JQ:-jq} ".result[${INDX:--1}].message.chat.username" - 2>/dev/null |${SED:-sed} -e 's#"##g') | |
DATA_Telegram_message_text=$(echo "${OUTPUT_Telegram_getUpdates:-}" |${JQ:-jq} ".result[${INDX:--1}].message.text" - 2>/dev/null) | |
DATA_Telegram_message_date=$(echo "${OUTPUT_Telegram_getUpdates:-}" |${JQ:-jq} ".result[${INDX:--1}].message.date" - 2>/dev/null) | |
DATA_Telegram_info="${DATA_Telegram_message_message_id:-}:${DATA_Telegram_message_chat_id:-}:${DATA_Telegram_message_chat_username:-}" | |
workMessage | |
fi | |
done | |
} | |
function workMessage(){ | |
if [[ -n ${DATA_Telegram_message_message_id} \ | |
&& -n ${DATA_Telegram_message_chat_id} \ | |
&& -n ${DATA_Telegram_message_chat_username} \ | |
&& -n ${DATA_Telegram_message_text} \ | |
&& -n ${DATA_Telegram_message_date} ]];then | |
echo '* Trying...'"${DATA_Telegram_info:-}" | |
unset DATA_Date_epochNow DATA_Name_chatLogFile DATA_Text_begin DATA_Date_chatLogFile | |
DATA_Date_epochNow=$(date +%s) | |
if [[ $((DATA_Date_epochNow-DATA_Telegram_message_date)) -ge 86400 ]];then | |
telegram_deleteMessage | |
else | |
DATA_Name_chatLogFile="${DATA_Telegram_message_chat_id:?}.chatLog" | |
#_ | |
# The discussion will be limited to 50 characters. | |
DATA_Text_begin="This is a discussion between a human nicknamed ${DATA_Telegram_message_chat_username:-You} and a robot named Victoria.\nVictoria is very nice and empathetic.\n" | |
#DATA_Text_begin="This is a discussion between a human nicknamed ${DATA_Telegram_message_chat_username:-You} and a robot named Victor.\nVictor is very nice and empathetic.\n\n${DATA_Telegram_message_chat_username:-You}: ¡Hola Victor!\nVictor: Hola, ¿Cómo estás?\n###\n${DATA_Telegram_message_chat_username:-You}: Bien, gracias por preguntar. ¿Y tú?\nVictor: Bien también. ¿Podemos dejar la conversación para otro momento?\n###\n${DATA_Telegram_message_chat_username:-You}: Ok. Nos vemos.\nVictor: Lo siento, nos vemos luego.\n###" | |
if [ -f "${DATA_Name_chatLogFile:-}" ];then | |
DATA_Date_chatLogFile=$(stat -c %Y "${DATA_Name_chatLogFile:?}") | |
if [[ $((DATA_Date_epochNow-DATA_Date_chatLogFile)) -ge 43200 ]];then | |
echo "${DATA_Text_begin:-}" > "${DATA_Name_chatLogFile:?}" | |
fi | |
else | |
echo "${DATA_Text_begin:-}" > "${DATA_Name_chatLogFile:?}" | |
fi | |
case "${DATA_Telegram_message_text,,}x" in | |
'"/help"x'|'""x'|'x'|'/helpx') | |
true | |
;; | |
'"/start"x'|'\startx') | |
echo "${DATA_Text_begin:-}" > "${DATA_Name_chatLogFile:?}" | |
DATA_Telegram_message_text='¡Hola!' | |
getAnswer | |
sendAnswer | |
;; | |
'"/stop"x'|'/stopx') | |
echo 'Deleting Messages by chat_id!!' | |
rm "${DATA_Name_chatLogFile:?}" | |
;; | |
*) | |
getAnswer | |
sendAnswer | |
;; | |
esac | |
fi | |
fi | |
echo '* theEnd.' | |
} | |
choiceMessage | |
################# | |
# # https://beta.openai.com/account/api-keys | |
# # https://beta.openai.com/docs/api-reference/models | |
# # https://beta.openai.com/docs/api-reference/completions/create | |
# # https://beta.openai.com/docs/models/gpt-3 | |
#curl "https://api.openai.com/v1/models" -H "Authorization: Bearer ${OPENAI_API_KEY:?}" |jq '.data[].id' | |
# # https://core.telegram.org/bots | |
# # https://telegram.me/BotFather | |
# # https://core.telegram.org/bots/api | |
# # https://nlpcloud.com/home/token | |
# # https://docs.nlpcloud.com/#code-generation | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment