functions in bash added and colors (#19)

* functions in bash added and colors

* Update bashbot.sh

* Update bashbot.sh

* Update bashbot.sh

* Update bashbot.sh

* Update bashbot.sh
This commit is contained in:
jarriztg 2016-06-05 05:41:49 -05:00 committed by Daniil Gentili
parent b2c5793032
commit d169b7f96f
1 changed files with 42 additions and 8 deletions

View File

@ -16,12 +16,23 @@ if [ ! -f "JSON.sh/JSON.sh" ]; then
echo "JSON.sh has been downloaded. Proceeding."
fi
if [ ! -f "token" ]; then
clear
echo -e '\e[0;31mTOKEN MISSING.\e[0m'
echo "PLEASE WRITE YOUR TOKEN HERE"
read token
echo "$token" >> token
fi
source commands.sh source
URL='https://api.telegram.org/bot'$TOKEN
SCRIPT="$0"
MSG_URL=$URL'/sendMessage'
LEAVE_URL=$URL'/leaveChat'
KICK_URL=$URL'/kickChatMember'
UNBAN_URL=$URL'/unbanChatMember'
PHO_URL=$URL'/sendPhoto'
AUDIO_URL=$URL'/sendAudio'
DOCUMENT_URL=$URL'/sendDocument'
@ -99,13 +110,25 @@ send_text() {
}
send_markdown_message() {
res=$(curl -s "$MSG_URL" -d "chat_id=$1" -d "text=$2" -d "parse_mode=markdown")
res=$(curl -s "$MSG_URL" -d "chat_id=$1" -d "text=$2" -d "parse_mode=markdown" -d "disable_web_page_preview=true")
}
send_html_message() {
res=$(curl -s "$MSG_URL" -F "chat_id=$1" -F "text=$2" -F "parse_mode=html")
}
kick_chat_member() {
res=$(curl -s "$KICK_URL" -F "chat_id=$1" -F "user_id=$2")
}
unban_chat_member() {
res=$(curl -s "$UNBAN_URL" -F "chat_id=$1" -F "user_id=$2")
}
leave_chat() {
res=$(curl -s "$LEAVE_URL" -F "chat_id=$1")
}
answer_inline_query() {
case $2 in
"article")
@ -263,7 +286,7 @@ startproc() {
killproc
mkfifo /tmp/$copname
TMUX= tmux new-session -d -s $copname "$* &>/tmp/$copname; echo imprettydarnsuredatdisisdaendofdacmd>/tmp/$copname"
TMUX= tmux new-session -d -s sendprocess_$copname "bash $SCRIPT outproc ${USER[ID]} $copname"
TMUX= tmux new-session -d -s sendprocess_$copname "bash $SCRIPT outproc ${CHAT[ID]} $copname"
}
killproc() {
@ -278,9 +301,12 @@ inproc() {
process_client() {
# Message
MESSAGE=$(echo "$res" | egrep '\["result",0,"message","text"\]' | cut -f 2 | cut -d '"' -f 2)
# Chat
CHAT[ID]=$(echo "$res" | egrep '\["result",0,"message","chat","id"\]' | cut -f 2)
# User
USER[ID]=$(echo "$res" | egrep '\["result",0,"message","chat","id"\]' | cut -f 2)
USER[ID]=$(echo "$res" | egrep '\["result",0,"message","from","id"\]' | cut -f 2)
USER[FIRST_NAME]=$(echo "$res" | egrep '\["result",0,"message","chat","first_name"\]' | cut -f 2 | cut -d '"' -f 2)
USER[LAST_NAME]=$(echo "$res" | egrep '\["result",0,"message","chat","last_name"\]' | cut -f 2 | cut -d '"' -f 2)
USER[USERNAME]=$(echo "$res" | egrep '\["result",0,"message","chat","username"\]' | cut -f 2 | cut -d '"' -f 2)
@ -313,11 +339,11 @@ process_client() {
NAME="$(echo ${URLS[*]} | sed 's/.*\///g')"
# Tmux
copname="$ME"_"${USER[ID]}"
copname="$ME"_"${CHAT[ID]}"
source commands.sh
tmpcount="COUNT${USER[ID]}"
tmpcount="COUNT${CHAT[ID]}"
cat count | grep -q "$tmpcount" || echo "$tmpcount">>count
# To get user count execute bash bashbot.sh count
}
@ -357,20 +383,28 @@ case "$1" in
for f in $(cat count);do send_message ${f//COUNT} "$*"; $sleep;done
;;
"start")
clear
tmux kill-session -t $ME&>/dev/null
tmux new-session -d -s $ME "bash $SCRIPT startbot" && echo "Bot started successfully. Tmux session name is $ME" || echo "An error occurred while starting the bot."
tmux new-session -d -s $ME "bash $SCRIPT startbot" && echo -e '\e[0;32mBot started successfully.\e[0m'
echo "Tmux session name $ME" || echo -e '\e[0;31mAn error occurred while starting the bot. \e[0m'
send_markdown_message "${CHAT[ID]}" "*Bot started*"
;;
"kill")
clear
tmux kill-session -t $ME &>/dev/null
echo "Bot was killed successfully. "
send_markdown_message "${CHAT[ID]}" "*Bot stopped*"
echo -e '\e[0;32mOK. Bot stopped successfully.\e[0m'
;;
"help")
clear
less README.md
;;
"attach")
tmux attach -t $ME
;;
*)
echo "Available arguments: outproc, count, broadcast, start, kill, help, attach"
echo -e '\e[0;31mBAD REQUEST\e[0m'
echo -e '\e[0;31mAvailable arguments: outproc, count, broadcast, start, kill, help, attach\e[0m'
;;
esac