94 lines
2.0 KiB
Bash
Executable File
94 lines
2.0 KiB
Bash
Executable File
#!/bin/bash
|
||
|
||
printf "Content-Type: text/plain\n\n"
|
||
|
||
. /etc/konata.conf
|
||
input=$(cat /dev/stdin)
|
||
|
||
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)
|
||
|
||
sendmsg(){
|
||
# Escape any special characters first, see https://0x0.st/NO7J.
|
||
fmt=$(sed 's/[][~!@#$%^&*()-_=+{}\|;:",<.>/?'"'"']/\\&/g' <<< $1 | sed 's/\t//g')
|
||
curl -X POST "https://api.telegram.org/bot$token/sendMessage" \
|
||
-d "chat_id=$chat" -d "parse_mode=markdownv2" -d "text=$fmt"
|
||
}
|
||
|
||
help(){
|
||
sendmsg "Hiya! I'm @ghnou's personal bot, Konata.
|
||
Currently I have the following features:
|
||
- /alive - Check whether I'm running.
|
||
- /cv [country] - Check COVID-19 stats.
|
||
- /id - Get the current chat's ID.
|
||
- /help - Show this help message.
|
||
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!"
|
||
}
|
||
|
||
hiya(){
|
||
sendmsg "Hiya! :3"
|
||
}
|
||
|
||
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)\`"
|
||
}
|
||
|
||
cv(){
|
||
stats=$(curl -sL corona.lmao.ninja/v2/countries/$args)
|
||
country=$(jq -r '.country' <<< $stats)
|
||
cases=$(jq -r '.todayCases' <<< $stats)
|
||
sendmsg "Today's COVID-19 cases for $country: $cases. Stay safe!"
|
||
}
|
||
|
||
id(){
|
||
sendmsg "Chat ID: $chat"
|
||
}
|
||
|
||
tag(){
|
||
sendmsg "Please stand by, he'll get to your message soon™️..."
|
||
}
|
||
|
||
fail(){
|
||
sendmsg "Sorry, you are not allowed to use this command!"
|
||
exit 1
|
||
}
|
||
|
||
admin(){
|
||
case $user in
|
||
ghnou) continue ;;
|
||
*) fail ;;
|
||
esac
|
||
}
|
||
|
||
case "$cmd" in
|
||
/start*|/help*) help ;;
|
||
/alive*) alive ;;
|
||
/stat*) stat ;;
|
||
/cv*|/corona*) cv ;;
|
||
/id*) id ;;
|
||
/term*) term ;;
|
||
*@ghnou*) tag ;;
|
||
*@konatasanbot*) hiya ;;
|
||
esac
|