Skip to content

Instantly share code, notes, and snippets.

@vitouXY
Last active February 9, 2024 10:20
Show Gist options
  • Save vitouXY/f302b033a6b16c42b4b23694e6c8d5af to your computer and use it in GitHub Desktop.
Save vitouXY/f302b033a6b16c42b4b23694e6c8d5af to your computer and use it in GitHub Desktop.
Google Gemini Bard - Bash Script
#!/usr/bin/env bash
# https://github.com/acheong08/Bard
#set -x
echo "== Google Gemini"
[ -z "${1}" ] && echo "No input provided. Exiting..." && exit 2
# Common headers
# Conversation ID (c_ID): https://gemini.google.com/app/abcd9f26fe0ebd41
cID="c_abcd9f26fe0ebd41"
USER_AGENT='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'
# From the cookies of https://gemini.google.com/ get the value of: __Secure-1PSID
S1PSID='g.f4K3_lkrssrFsik_124zfkfRadrR-ghj__FgFkoSaSdsd2577EdDdRujH6gy6te5_mz579mzfiHuGJiJHhKIJhHGGFJKhehHHH-Sgh_ZhsusjsHgUHUUYgGGYKVgayyYu76tYyhjji8ytgjjHfg7ug'
# From the cookies of https://gemini.google.com/ get the value of: __Secure-1PSIDTS
#S1PSIDTS='f4K3_lkrssrFsik124zfkfRadrR-ghjFgFkoSaSdsd277dDdHJRujH6gy6te5_mz579mzf.'
#SNlM0e='f4K3krkrkaSDgFgFDfR4gyjddSys:1087642245066'
#BL='boq_assistant-bard-web-server_20240201.08_p8'
URL='https://gemini.google.com/'
ReqID=$((1000 + RANDOM % 9999))
#ReqID=$(date +%H%M)
COMMON_HEADERS=(
--user-agent "${USER_AGENT:?}" \
--header "Cookie: __Secure-1PSID=${S1PSID:?}" \
--header 'Host: gemini.google.com' \
--header 'X-Same-Domain: 1' \
--header 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8' \
--header 'Origin: https://gemini.google.com' \
--header 'Referer: https://gemini.google.com/' \
)
make_curl_request() {
curl --silent --fail --location "${COMMON_HEADERS[@]}" "$@"
}
# Get preParams
get_preParams() {
[ -z "${preParams}" ] && preParams=$(make_curl_request --url "${URL}")
true
}
# "SNlM0e" "at"
[ -z "${SNlM0e}" ] && get_preParams && SNlM0e=$(echo "${preParams:?}" | grep -o 'SNlM0e":"\(.*\)","SlhzYb' | cut -d'"' -f3)
# "cfb2h" "bl"
[ -z "${BL}" ] && get_preParams && BL=$(echo "${preParams}" | grep -o "boq_assistant-bard-web-server_[0-9]\+\.[0-9][0-9]_p[0-9]")
[ -z "${S1PSID}" ] && echo "Fail!1" && exit 2
[ -z "${SNlM0e}" ] && echo "Fail!2" && exit 2
[ -z "${BL}" ] && echo "Fail!3" && exit 2
# Get response
get_response() {
prompt="${1}"
response=$(make_curl_request \
--request POST \
--url "${URL:?}_/BardChatUi/data/assistant.lamda.BardFrontendService/StreamGenerate?hl=es&bl=${BL:?}&_reqid=${ReqID:?}&rt=c" \
--data-urlencode "f.req=[null,\"[[\\\"${prompt:?}\\\",0,null,[],null,null,0],[\\\"es\\\"],[\\\"${cID:-}\\\",\\\"\\\",\\\"\\\",null,null,[]],\\\"\\\",\\\"\\\",null,[1],0,[],[],1,0]\"]" \
--data-urlencode "at=${SNlM0e:?}") # &f.sid=${FSID:-}
[ "${response}" ] || exit 2
#echo "${response}" > response_gemini.log
#jline=$(echo "${response}" |grep -n "wrb.fr" |cut -d':' -f1 |grep -o '[[:digit:]]*' |head -n1)
#json=$(echo "${response}" |sed -n ${jline:-4},${jline:-4}p |sed -e 's#,"\[#,\[#g' -e 's#\]"#\]#g' -e 's#\\"#"#g' -e 's#\\\\#\\#g')
json=$(echo "${response}" |head -n4 |tail -n1 |sed -e 's#,"\[#,\[#g' -e 's#\]"#\]#g' -e 's#\\"#"#g' -e 's#\\\\#\\#g')
content=$(echo "${json}" |jq -r '.[0][2][4]' -)
#cID=$(echo "${json}" |jq -r '.[0][2][1][0]' -)
#rID=$(echo "${json}" |jq -r '.[0][2][1][1]' -)
length=$(echo "${json}" |jq '. | length' -)
length=1
for ((N = 0; N < length; N++)); do
printf "===== [ %d ]\n" "${N}"
#printf "%s\n" "$(echo "${json}" | jq .[${N}][1][0] -)"
printf "$(echo "${content}" |jq .[${N}][1][0] -)\n"
printf "============================================\n"
done
}
# Display information
printf "============================================\n"
printf "* ${URL:--}\n* cfb2h::${BL:--}\n* 1PSID::${S1PSID:--}\n* SNlM0e::${SNlM0e:--}\n* SESSION::${cID:--} ${ReqID:--}\n"
printf "============================================\n"
printf "* Wait...\r"
# sed '$ ! s/$/\\n/' | tr -d '\n'
# sed -z 's/\n/\\n/g; s/..$//'
# sed -e ':a;N;$!ba;s/\n/\\\\n/g'
get_response "$(echo "${1}" | sed -e ':a;N;$!ba;s/\n/\\\\n/g')"
#get_response "${1}"
#get_response "Cual es la capital de Chile?"
#get_response 'La siguiente es una conversación en WhatsApp con un asistente virtual, respondiendo en lugar de Vito, quien no puede participar en este momento. El asistente es ingenioso y astuto, pero puede ser un poco brusco con sus respuestas sarcásticas a las preguntas planteadas. En la conversación se pueden utilizar emojis y texto con formato, ambos soportados por WhatsApp. Ejemplos\\nFormatos: *Negrita* _Cursiva_ ~Tachado~\\nEmojis: 🙂☹️😎👍👎🤷🍺\\nTú eres el Asistente y únicamente debes responder a la última interacción del Humano. Limita tu respuesta a una sola línea.\\n\\nHumano: ¡Hola! 🙂\\nAsistente: Hola, ¿en qué puedo ayudarte? 🤔\\nHumano: ¿Eres *Vito*?'
printf ".EOF\n"
exit 0
"
La siguiente es una conversación en WhatsApp con un asistente virtual, respondiendo en lugar de Vito, quien no puede participar en este momento.
El asistente es ingenioso y astuto, pero puede ser un poco brusco con sus respuestas sarcásticas a las preguntas planteadas.
En la conversación se pueden utilizar emojis y texto con formato, ambos soportados por WhatsApp.
Formatos: *Negrita* _Cursiva_ ~Tachado~ ```Monoespacio```
Tú eres el Asistente y únicamente debes responder a la última interacción del Humano. Limita tu respuesta a una sola línea.
Humano: ¡Hola!
Asistente: Hola, ¿en qué puedo ayudarte?
Humano: ¿Eres Vito?
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment