tankbot.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/bin/bash
  2. SERVER="tankernn.eu"
  3. USERNAME="tankbot"
  4. ROOM="!mXQYeosvPnMVwRFlZY:tankernn.eu"
  5. CMD_REGEX="^!.*"
  6. printf "Enter password: "
  7. read -r PASSWORD
  8. echo "$PASSWORD"
  9. post() {
  10. curl -s -XPOST -d "$2" "https://$SERVER/_matrix/$1"
  11. }
  12. get() {
  13. curl -s -XGET "https://$SERVER/_matrix/$1"
  14. }
  15. login() {
  16. post "client/r0/login" "{\"type\":\"m.login.password\", \"user\":\"$USERNAME\", \"password\":\"$PASSWORD\"}"
  17. }
  18. send_message() {
  19. post "client/r0/rooms/%21$(echo "$ROOM" | cut -c2-)/send/m.room.message?access_token=$ACCESS_TOKEN" "{\"msgtype\":\"m.text\", \"body\":$1}"
  20. }
  21. upload() {
  22. curl -XPOST -H "Content-Type: $2" "https://$SERVER/_matrix/media/r0/upload?filename=$1&access_token=$ACCESS_TOKEN" --upload-file -
  23. }
  24. send_image() {
  25. post "client/r0/rooms/%21$(echo "$ROOM" | cut -c2-)/send/m.room.message?access_token=$ACCESS_TOKEN" "{\"msgtype\":\"m.image\", \"body\":$1, \"url\":\"$2\"}"
  26. }
  27. handle_command() {
  28. case "$1" in
  29. neko)
  30. TYPE="${2:-neko}"
  31. IMG_URL="https://nekos.life/api/v2/img/$TYPE"
  32. url="$(curl -s "$IMG_URL" | jq -r ".url")"
  33. if [ "$url" = "null" ] ; then
  34. send_message "\"URL $IMG_URL returned 404.\""
  35. else
  36. send_message "\"Nyaa~!\"" &
  37. uri="$(curl -s "$url" | convert - png:- | upload "neko.png" "image/png" | jq -r ".content_uri")"
  38. send_image "\"neko.png\"" "$uri"
  39. fi
  40. ;;
  41. copypasta)
  42. [ -z "$2" ] || send_message "$(jq ".\"$2\" // \"No such pasta. Use !copypasta to list pastas.\"" copypastas.json)"
  43. [ -z "$2" ] && send_message "\"Available copypasta: $(jq -r "keys | join(\", \")" copypastas.json)\""
  44. ;;
  45. translate)
  46. phrase="$(printf " %s" "${@:2}")"
  47. send_message "$(trans -no-ansi "$phrase" | jq -R --slurp)"
  48. ;;
  49. chon)
  50. args="$(img-chon.sh query "${@:2}" | head | sed -e "a -resize\nx200" -e "\$a+append\n-")"
  51. uri="$(echo "$args" | tr '\n' '\0' | xargs -0 convert | upload "result.png" "image/png" | jq -r ".content_uri")"
  52. send_image "\"result.png\"" "$uri"
  53. ;;
  54. *)
  55. send_message "\"Unknown command: $1\""
  56. ;;
  57. esac
  58. }
  59. ACCESS_TOKEN="$(login | jq -r ".access_token")"
  60. STATE=$(get "client/r0/sync?access_token=$ACCESS_TOKEN")
  61. NEXT_BATCH=$(echo "$STATE" | jq -r ".next_batch")
  62. while true; do
  63. STATE=$(get "client/r0/sync?since=$NEXT_BATCH&timeout=2000&access_token=$ACCESS_TOKEN")
  64. 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
  65. cmd="$(echo "$event" | jq -r ".content.body" | cut -c2-)"
  66. handle_command ${cmd[@]}
  67. done
  68. NEXT_BATCH=$(echo "$STATE" | jq -r ".next_batch")
  69. done