hptoad/examples/chat/answer

52 lines
1.4 KiB
Plaintext
Raw Permalink Normal View History

2015-08-13 14:52:59 +00:00
#!/bin/bash
2016-08-19 18:08:03 +00:00
# Simple chat bot
# with sending images to an external programme.
2015-08-13 14:52:59 +00:00
random() {
2016-08-19 18:08:03 +00:00
if (( "$1" < 1 )); then
echo '1'
else
shuf -n1 -i "1-$1"
fi
2015-08-13 14:52:59 +00:00
}
cd "$(dirname "$0")"
2016-08-19 18:08:03 +00:00
printf "%s" "$1: "
from=$(printf "%s\n" "$1" | tr -d '$`|<>')
2015-08-13 14:52:59 +00:00
# Second argument shows whether user is an admin (useless).
shift 2
string=$(echo "$@" | tr -d '$`')
if [[ ${#string} -gt 750 ]]; then
echo "tl;dr"
exit 0
fi
if [[ "$string" == *http*://* ]]; then
amount=$(sed -e '/https*:\/\//!d' ./frs.txt | wc -l)
# Makes it a bit more human (time to find a link?).
sleep 1
sed -e '/https*:\/\//!d' ./frs.txt | sed -n "$(random "$amount")p"
2016-08-19 18:08:03 +00:00
img="$(printf "%s\n" "$string" | sed -ne 's|.*\(https*://[^ \"()<>]*\).*|\1|g;1p')"
ns="$(printf "%s\n" "$string" | sed -e "s|$img||g")"
cl="$(wget --spider -S "$img" 2>&1)"
2016-08-19 18:08:03 +00:00
if [[ $(printf "%s\n" "$cl" | sed -e '/Content-Type/!d;/image\//!d') ]]; then
2015-08-13 14:52:59 +00:00
#echo 'posting'
2016-09-08 20:00:13 +00:00
printf "%s\n" "$img $from" >> ./pictures.txt
2015-08-13 14:52:59 +00:00
fi
else
2016-08-19 18:08:03 +00:00
# Exclude last two entries from the amount.
amount="$(($(sed -e '/https*:\/\//d' ./frs.txt | wc -l) - 2))"
2015-08-13 14:52:59 +00:00
answer="$(sed -e '/https*:\/\//d' ./frs.txt | sed -ne "s/;;\\\n/\n/g;$(random "$amount")p")"
# Makes it a bit more human.
2016-08-19 18:08:03 +00:00
sleep "$(echo "${#answer} * 0.15" | bc -l)"
printf "%s\n" "$answer"
2015-08-13 14:52:59 +00:00
fi
if [[ ${#string} -lt 7 ]]; then
exit 0
fi
2016-08-19 18:08:03 +00:00
printf "%s\n" "$string" | sed -e '{:q;N;s/\n/;;\\n/g;t q}' >> ./frs.txt
2015-08-13 14:52:59 +00:00
exit 0