|
@@ -0,0 +1,84 @@
|
|
|
+#!/bin/bash
|
|
|
+
|
|
|
+SERVER="tankernn.eu"
|
|
|
+USERNAME="tankbot"
|
|
|
+ROOM="!mXQYeosvPnMVwRFlZY:tankernn.eu"
|
|
|
+CMD_REGEX="^!.*"
|
|
|
+
|
|
|
+printf "Enter password: "
|
|
|
+read -r PASSWORD
|
|
|
+echo "$PASSWORD"
|
|
|
+
|
|
|
+post() {
|
|
|
+ curl -s -XPOST -d "$2" "https://$SERVER/_matrix/$1"
|
|
|
+}
|
|
|
+
|
|
|
+get() {
|
|
|
+ curl -s -XGET "https://$SERVER/_matrix/$1"
|
|
|
+}
|
|
|
+
|
|
|
+login() {
|
|
|
+ post "client/r0/login" "{\"type\":\"m.login.password\", \"user\":\"$USERNAME\", \"password\":\"$PASSWORD\"}"
|
|
|
+}
|
|
|
+
|
|
|
+send_message() {
|
|
|
+ post "client/r0/rooms/%21$(echo "$ROOM" | cut -c2-)/send/m.room.message?access_token=$ACCESS_TOKEN" "{\"msgtype\":\"m.text\", \"body\":$1}"
|
|
|
+}
|
|
|
+
|
|
|
+upload() {
|
|
|
+ curl -XPOST -H "Content-Type: $2" "https://$SERVER/_matrix/media/r0/upload?filename=$1&access_token=$ACCESS_TOKEN" --upload-file -
|
|
|
+}
|
|
|
+
|
|
|
+send_image() {
|
|
|
+ post "client/r0/rooms/%21$(echo "$ROOM" | cut -c2-)/send/m.room.message?access_token=$ACCESS_TOKEN" "{\"msgtype\":\"m.image\", \"body\":$1, \"url\":\"$2\"}"
|
|
|
+}
|
|
|
+
|
|
|
+handle_command() {
|
|
|
+ case "$1" in
|
|
|
+ neko)
|
|
|
+ TYPE="${2:-neko}"
|
|
|
+ IMG_URL="https://nekos.life/api/v2/img/$TYPE"
|
|
|
+ url="$(curl -s "$IMG_URL" | jq -r ".url")"
|
|
|
+ if [ "$url" = "null" ] ; then
|
|
|
+ send_message "\"URL $IMG_URL returned 404.\""
|
|
|
+ else
|
|
|
+ send_message "\"Nyaa~!\"" &
|
|
|
+ uri="$(curl -s "$url" | convert - png:- | upload "neko.png" "image/png" | jq -r ".content_uri")"
|
|
|
+ send_image "\"neko.png\"" "$uri"
|
|
|
+ fi
|
|
|
+ ;;
|
|
|
+ copypasta)
|
|
|
+ [ -z "$2" ] || send_message "$(jq ".\"$2\" // \"No such pasta. Use !copypasta to list pastas.\"" copypastas.json)"
|
|
|
+ [ -z "$2" ] && send_message "\"Available copypasta: $(jq -r "keys | join(\", \")" copypastas.json)\""
|
|
|
+ ;;
|
|
|
+ translate)
|
|
|
+ phrase="$(printf " %s" "${@:2}")"
|
|
|
+ send_message "$(trans -no-ansi "$phrase" | jq -R --slurp)"
|
|
|
+ ;;
|
|
|
+ chon)
|
|
|
+ args="$(img-chon.sh query "${@:2}" | head | sed -e "a -resize\nx200" -e "\$a+append\n-")"
|
|
|
+ uri="$(echo "$args" | tr '\n' '\0' | xargs -0 convert | upload "result.png" "image/png" | jq -r ".content_uri")"
|
|
|
+ send_image "\"result.png\"" "$uri"
|
|
|
+ ;;
|
|
|
+ *)
|
|
|
+ send_message "\"Unknown command: $1\""
|
|
|
+ ;;
|
|
|
+ esac
|
|
|
+}
|
|
|
+
|
|
|
+ACCESS_TOKEN="$(login | jq -r ".access_token")"
|
|
|
+
|
|
|
+STATE=$(get "client/r0/sync?access_token=$ACCESS_TOKEN")
|
|
|
+NEXT_BATCH=$(echo "$STATE" | jq -r ".next_batch")
|
|
|
+
|
|
|
+while true; do
|
|
|
+ STATE=$(get "client/r0/sync?since=$NEXT_BATCH&timeout=2000&access_token=$ACCESS_TOKEN")
|
|
|
+
|
|
|
+ echo "$STATE" | jq -c ".rooms.join.\"$ROOM\".timeline.events // [] | map(select(.content.msgtype == \"m.text\" and (.content.body | test(\"$CMD_REGEX\"))))[]" | while read -r event; do
|
|
|
+ cmd="$(echo "$event" | jq -r ".content.body" | cut -c2-)"
|
|
|
+ handle_command ${cmd[@]}
|
|
|
+ done
|
|
|
+
|
|
|
+
|
|
|
+ NEXT_BATCH=$(echo "$STATE" | jq -r ".next_batch")
|
|
|
+done
|