Add an example exec bot that handles basic commands

This commit is contained in:
James Mills 2022-03-24 02:07:42 +10:00
parent a5d70a7b7c
commit ffd21e172c
2 changed files with 48 additions and 1 deletions

View File

@ -5,7 +5,7 @@
# Setup:
#
# $ salty-chat -i ~/.config/salty/echobot.key -u echo@yourdomain.tld make-user
# $ salty-chat -i ~/.config/salty/echobot.key -u echo@mills.io read --post-hook ./echobot.sh
# $ salty-chat -i ~/.config/salty/echobot.key -u echo@yourdomain.tld read --post-hook ./echobot.sh
set -e

47
hooks/execbot.sh Executable file
View File

@ -0,0 +1,47 @@
#!/bin/sh
# A Salty IM Command Execution Bot written as a POSIX Shell script
# using salty-chat read' --post-hook mechanism.
#
# Supported commands: date time uptime whoami
#
# Setup:
#
# $ salty-chat -i ~/.config/salty/execbot.key -u exec@yourdomain.tld make-user
# $ salty-chat -i ~/.config/salty/execbot.key -u exec@yourdomain.tld read --post-hook ./execbot.sh
set -e
# XXX: Set this to the execbot's key
identity=
# XXX: Set this to the execbot's addr
user=
tmpfile="$(mktemp -t "echobot-XXXXXX")"
trap 'rm $tmpfile' EXIT
cat > "$tmpfile"
sender="$(head -n 1 < "$tmpfile" | awk '{ print $2 }')"
sender="$(echo "$sender" | sed 's/[)(]//g')"
message="$(head -n 1 < "$tmpfile" | awk '{ $1 = ""; $2 = ""; print $0; }')"
cmd="$(echo "$message" | awk '{$1=$1};1')"
cmds="time date uptime whoami"
case $cmd in
"time" | "date")
result="$(date)"
;;
"uptime")
result="$(uptime)"
;;
"whoami")
result="$(whoami)"
;;
*)
result="$(printf "Invalid command '%s' Available commands are: $cmds" "$cmd")"
;;
esac
echo "$result" | salty-chat -d -i "$identity" -u "$user" send "$sender"