137 lines
3.1 KiB
Bash
Executable File
137 lines
3.1 KiB
Bash
Executable File
#!/bin/bash
|
||
|
||
# Hard requirement for lighttpd
|
||
printf "Content-Type: text/plain\n\n"
|
||
|
||
# Initialize basic variables
|
||
. /etc/konata.conf
|
||
input=$(cat /dev/stdin)
|
||
bot="konatasanbot"
|
||
|
||
# Parse incoming messages
|
||
cmd=$(jq -r '.message.text' <<< $input)
|
||
args=$(cut -d" " -f2- <<< $cmd)
|
||
chat=$(jq -r '.message.chat.id' <<< $input)
|
||
user=$(jq -r '.message.from.username' <<< $input)
|
||
|
||
# Functions for outgoing messages
|
||
sendmsg(){
|
||
# Escape any special characters first, see https://0x0.st/NO7J.
|
||
fmt=$(sed 's/[][~!@#$%^&*()-_=+{}\|;:",<.>/?'"'"']/\\&/g' <<< $1 | sed 's/\t//g')
|
||
sendraw "$fmt"
|
||
}
|
||
sendraw(){
|
||
curl -X POST "https://api.telegram.org/bot$token/sendMessage" \
|
||
-d "chat_id=$chat" -d "parse_mode=markdownv2" \
|
||
-d "disable_web_page_preview=true" -d "text=$1"
|
||
}
|
||
sendfile(){
|
||
curl -sF document=@"$1" "https://api.telegram.org/bot$token/sendDocument?chat_id=$chat"
|
||
}
|
||
|
||
# Public commands
|
||
help(){
|
||
sendmsg "Hiya! I'm @ghnou's personal bot, Konata.
|
||
Currently I have the following features:
|
||
- /help - Show this help message.
|
||
- /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.
|
||
Admin only commands:
|
||
- /stat - Uptime, memory, storage stats.
|
||
- /term - Execute a command on the system.
|
||
|
||
And a lot more to come!"
|
||
}
|
||
|
||
alive(){
|
||
sendmsg "Alive and well!"
|
||
}
|
||
|
||
cv(){
|
||
stats=$(curl -sL "corona.lmao.ninja/v2/countries/$args")
|
||
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
|
||
}
|
||
|
||
id(){
|
||
sendmsg "Chat ID: $chat"
|
||
}
|
||
|
||
mp3(){
|
||
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/*
|
||
}
|
||
|
||
# Administrative
|
||
stat(){
|
||
admin
|
||
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."
|
||
}
|
||
|
||
term(){
|
||
admin
|
||
sendmsg "\`$(echo $args | bash - 2>&1)\`"
|
||
}
|
||
|
||
fail(){
|
||
sendmsg "Sorry, you are not allowed to use this command!"
|
||
}
|
||
|
||
admin(){
|
||
case $user in
|
||
ghnou) continue ;;
|
||
*) fail ;;
|
||
esac
|
||
}
|
||
|
||
# Notes and such
|
||
anyone(){
|
||
sendraw "Quite possibly\. [Why do you ask?](https://dontasktoask.com)"
|
||
}
|
||
|
||
# Miscellaneous
|
||
tag(){
|
||
sendmsg "Please stand by, he'll get to your message soon™️..."
|
||
}
|
||
|
||
hiya(){
|
||
sendmsg "Hiya! :3"
|
||
}
|
||
|
||
# Match incoming messages
|
||
case "$cmd" in
|
||
# Public commands
|
||
/help | /help@$bot) help ;;
|
||
/start | /start@$bot) help ;;
|
||
/alive | /alive@$bot) alive ;;
|
||
/cv* | /cv@$bot*) cv ;;
|
||
/corona* | /cv@$bot*) cv ;;
|
||
/id | /id@$bot) id ;;
|
||
/mp3* | /mp3@$bot*) mp3 ;;
|
||
# Administrative
|
||
/stat | /stat@$bot) stat ;;
|
||
/term* | /term@$bot*) term ;;
|
||
# Notes and such
|
||
!anyone) anyone ;;
|
||
# Miscellaneous
|
||
*@ghnou*) tag ;;
|
||
*@konatasanbot*) hiya ;;
|
||
esac
|