konata/konata.sh

137 lines
3.1 KiB
Bash
Raw Normal View History

2020-12-28 03:34:15 +01:00
#!/bin/bash
2020-12-31 21:23:34 +01:00
# Hard requirement for lighttpd
2020-12-28 14:42:08 +01:00
printf "Content-Type: text/plain\n\n"
2020-12-25 20:57:07 +01:00
2020-12-31 21:23:34 +01:00
# Initialize basic variables
2020-12-28 14:42:08 +01:00
. /etc/konata.conf
2020-12-25 20:57:07 +01:00
input=$(cat /dev/stdin)
2020-12-31 18:14:41 +01:00
bot="konatasanbot"
2020-12-25 20:57:07 +01:00
2020-12-31 21:23:34 +01:00
# Parse incoming messages
2020-12-26 13:48:27 +01:00
cmd=$(jq -r '.message.text' <<< $input)
args=$(cut -d" " -f2- <<< $cmd)
2020-12-25 20:57:07 +01:00
chat=$(jq -r '.message.chat.id' <<< $input)
2020-12-26 01:08:36 +01:00
user=$(jq -r '.message.from.username' <<< $input)
2020-12-25 20:57:07 +01:00
2020-12-31 21:23:34 +01:00
# Functions for outgoing messages
2020-12-25 20:57:07 +01:00
sendmsg(){
# Escape any special characters first, see https://0x0.st/NO7J.
2020-12-27 13:17:31 +01:00
fmt=$(sed 's/[][~!@#$%^&*()-_=+{}\|;:",<.>/?'"'"']/\\&/g' <<< $1 | sed 's/\t//g')
2020-12-31 17:53:29 +01:00
sendraw "$fmt"
}
sendraw(){
2020-12-26 01:08:36 +01:00
curl -X POST "https://api.telegram.org/bot$token/sendMessage" \
2020-12-31 17:53:29 +01:00
-d "chat_id=$chat" -d "parse_mode=markdownv2" \
-d "disable_web_page_preview=true" -d "text=$1"
2020-12-25 20:57:07 +01:00
}
sendfile(){
curl -sF document=@"$1" "https://api.telegram.org/bot$token/sendDocument?chat_id=$chat"
}
2020-12-25 20:57:07 +01:00
2020-12-31 21:23:34 +01:00
# Public commands
2020-12-27 13:17:31 +01:00
help(){
sendmsg "Hiya! I'm @ghnou's personal bot, Konata.
Currently I have the following features:
- /help - Show this help message.
2020-12-27 13:17:31 +01:00
- /alive - Check whether I'm running.
- /cv [country] - Check COVID-19 stats.
- /id - Get the current chat's ID.
- /mp3 [link] - Download an mp3 file from YouTube.
2020-12-27 13:17:31 +01:00
Admin only commands:
- /stat - Uptime, memory, storage stats.
- /term - Execute a command on the system.
And a lot more to come!"
}
2020-12-25 20:57:07 +01:00
alive(){
2020-12-26 01:08:36 +01:00
sendmsg "Alive and well!"
}
2020-12-31 21:23:34 +01:00
cv(){
stats=$(curl -sL "corona.lmao.ninja/v2/countries/$args")
2020-12-31 21:23:34 +01:00
country=$(jq -r '.country' <<< $stats)
cases=$(jq -r '.todayCases' <<< $stats)
if [ "$(jq -r '.message' <<< $stats)" != "null" ]
then
sendmsg "This country doesn't have any stats!"
else
sendmsg "Today's COVID-19 cases for $country: $cases. Stay safe!"
fi
2020-12-25 20:57:07 +01:00
}
2020-12-31 21:23:34 +01:00
id(){
sendmsg "Chat ID: $chat"
}
mp3(){
2021-01-01 00:32:33 +01:00
admin
tmp="/tmp/mp3"
[ -d $tmp ] && mkdir $tmp
sendmsg "Downloading, this might take a while..."
youtube-dl --add-metadata --metadata-from-title "%(artist)s - %(title)s" -x --audio-format mp3 -o "$tmp/%(title)s.%(ext)s" "$args"
sendfile "$tmp/$(ls $tmp)"
rm $tmp/*
}
2020-12-31 21:23:34 +01:00
# Administrative
2020-12-25 20:57:07 +01:00
stat(){
admin
2020-12-25 20:57:07 +01:00
total=$(grep MemTotal /proc/meminfo | awk '{print $2}')
avail=$(grep MemAvailable /proc/meminfo | awk '{print $2}')
used=$(bc <<< "$total-$avail")
sendmsg "$(uptime | cut -c2-)
I am using ${used}K of memory and \
$(df -h / | tail -n1 | awk '{print $3}') of storage."
2020-12-26 01:08:36 +01:00
}
2020-12-27 13:17:31 +01:00
term(){
admin
sendmsg "\`$(echo $args | bash - 2>&1)\`"
}
2020-12-31 21:23:34 +01:00
fail(){
sendmsg "Sorry, you are not allowed to use this command!"
2020-12-26 01:08:36 +01:00
}
2020-12-31 21:23:34 +01:00
admin(){
case $user in
ghnou) continue ;;
*) fail ;;
esac
2020-12-25 20:57:07 +01:00
}
2020-12-31 21:23:34 +01:00
# Notes and such
2020-12-31 17:53:29 +01:00
anyone(){
sendraw "Quite possibly\. [Why do you ask?](https://dontasktoask.com)"
}
2020-12-31 21:23:34 +01:00
# Miscellaneous
2020-12-27 13:26:26 +01:00
tag(){
sendmsg "Please stand by, he'll get to your message soon™..."
}
2020-12-31 21:23:34 +01:00
hiya(){
sendmsg "Hiya! :3"
}
2020-12-31 21:23:34 +01:00
# Match incoming messages
2020-12-25 20:57:07 +01:00
case "$cmd" in
2020-12-31 21:23:34 +01:00
# Public commands
2020-12-31 18:14:41 +01:00
/help | /help@$bot) help ;;
/start | /start@$bot) help ;;
2021-01-01 00:06:39 +01:00
/alive | /alive@$bot) alive ;;
/cv* | /cv@$bot*) cv ;;
2021-01-01 00:06:39 +01:00
/id | /id@$bot) id ;;
/mp3* | /mp3@$bot*) mp3 ;;
2020-12-31 21:23:34 +01:00
# Administrative
2021-01-01 00:06:39 +01:00
/stat | /stat@$bot) stat ;;
/term* | /term@$bot*) term ;;
2020-12-31 21:23:34 +01:00
# Notes and such
2020-12-31 17:53:29 +01:00
!anyone) anyone ;;
2020-12-31 21:23:34 +01:00
# Miscellaneous
2020-12-27 13:26:26 +01:00
*@ghnou*) tag ;;
2020-12-27 13:17:31 +01:00
*@konatasanbot*) hiya ;;
2020-12-25 20:57:07 +01:00
esac