#!/bin/bash
echo "🌐 Termux Smart Bot (type 'exit' to quit)"
echo
while true; do
read -p "You: " query
if [[ "$query" == "exit" ]]; then
echo "Bye 👋"
break
fi
# Encode spaces for URL
q=$(echo "$query" | sed 's/ /+/g')
# Fetch first snippet from DuckDuckGo
answer=$(curl -s "https://api.duckduckgo.com/?q=$q&format=json&no_redirect=1" \
| jq -r '.RelatedTopics[0].Text')
# Fallback if empty, too short, or just a category
if [[ -z "$answer" ]] || [[ "$answer" == "null" ]] || [[ ${#answer} -lt 20 ]] || [[ "$answer" == *Category* ]]; then
answer="Bot: Sorry, I couldn't find a detailed answer. Try a different phrasing."
else
answer="Bot: $answer"
fi
echo -e "$answer\n"
done