2016-01-02 19:35:15 +00:00
|
|
|
#!/bin/bash
|
2015-07-10 05:43:08 +00:00
|
|
|
# bashbot, the Telegram bot written in bash.
|
2016-01-05 16:01:28 +00:00
|
|
|
# Written by @topkecleon, Juan Potato (@awkward_potato), Lorenzo Santina (BigNerd95) and Daniil Gentili (danog)
|
2015-07-10 05:43:08 +00:00
|
|
|
# http://github.com/topkecleon/bashbot
|
|
|
|
|
|
|
|
# Depends on JSON.sh (http://github.com/dominictarr/JSON.sh),
|
|
|
|
# which is MIT/Apache-licensed.
|
|
|
|
|
|
|
|
# This file is public domain in the USA and all free countries.
|
|
|
|
# If you're in Europe, and public domain does not exist, then haha.
|
|
|
|
|
2015-07-10 07:32:34 +00:00
|
|
|
TOKEN=''
|
2015-07-10 05:43:08 +00:00
|
|
|
URL='https://api.telegram.org/bot'$TOKEN
|
2015-11-25 03:07:39 +00:00
|
|
|
MSG_URL=$URL'/sendMessage'
|
|
|
|
PHO_URL=$URL'/sendPhoto'
|
2015-07-10 05:43:08 +00:00
|
|
|
UPD_URL=$URL'/getUpdates?offset='
|
|
|
|
OFFSET=0
|
|
|
|
|
2015-12-26 01:47:10 +00:00
|
|
|
send_message() {
|
2016-01-05 16:01:28 +00:00
|
|
|
local chat="$1"
|
|
|
|
local text="$(echo "$2" | sed 's/ mykeyboardstartshere.*//g')"
|
|
|
|
local keyboard="$(echo "$2" | sed '/mykeyboardstartshere /!d;s/.*mykeyboardstartshere //g')"
|
|
|
|
if [ "$keyboard" = "" ]; then
|
|
|
|
res=$(curl -s "$MSG_URL" -F "chat_id=$chat" -F "text=$text")
|
|
|
|
else
|
|
|
|
send_keyboard "$chat" "$text" "$keyboard"
|
|
|
|
fi
|
2016-01-03 01:14:07 +00:00
|
|
|
}
|
2016-01-05 16:01:28 +00:00
|
|
|
|
2016-01-03 01:14:07 +00:00
|
|
|
send_keyboard() {
|
2016-01-05 16:01:28 +00:00
|
|
|
local chat="$1"
|
|
|
|
local text="$2"
|
|
|
|
shift 2
|
|
|
|
keyboard=init
|
|
|
|
for f in $*;do keyboard="$keyboard, [\"$f\"]";done
|
|
|
|
keyboard=${keyboard/init, /}
|
|
|
|
res=$(curl -s "$MSG_URL" --header "content-type: multipart/form-data" -F "chat_id=$chat" -F "text=$text" -F "reply_markup={\"keyboard\": [$keyboard],\"one_time_keyboard\": true}")
|
2015-11-25 03:07:39 +00:00
|
|
|
}
|
|
|
|
|
2015-12-26 01:47:10 +00:00
|
|
|
send_photo() {
|
2016-01-05 16:01:28 +00:00
|
|
|
res=$(curl -s "$PHO_URL" -F "chat_id=$1" -F "photo=@$2")
|
2015-07-10 07:32:34 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 16:01:28 +00:00
|
|
|
startproc() {
|
|
|
|
local copname="$1"
|
|
|
|
local TARGET="$2"
|
|
|
|
mkdir -p "$copname"
|
|
|
|
mkfifo $copname/out
|
|
|
|
tmux new-session -d -n $copname "./question $TARGET 2>&1>$copname/out"
|
|
|
|
local pid=$(ps aux | sed '/tmux/!d;/'$copname'/!d;/sed/d;s/'$USER'\s*//g;s/\s.*//g')
|
|
|
|
echo $pid>$copname/pid
|
|
|
|
while ps aux | grep -v grep | grep -q $pid;do
|
|
|
|
read -t 10 line
|
|
|
|
[ "$line" != "" ] && send_message "$TARGET" "$line"
|
|
|
|
line=
|
|
|
|
done <$copname/out
|
2016-01-02 20:51:45 +00:00
|
|
|
}
|
2016-01-03 01:14:07 +00:00
|
|
|
inproc() {
|
2016-01-05 16:01:28 +00:00
|
|
|
local copname="$1"
|
|
|
|
local copid="$2"
|
|
|
|
shift 2
|
|
|
|
tmux send-keys -t $copname "$@
|
|
|
|
"
|
|
|
|
ps aux | grep -v grep | grep -q "$copid" || { rm -r $copname; };
|
2016-01-03 01:14:07 +00:00
|
|
|
}
|
|
|
|
|
2015-12-26 01:47:10 +00:00
|
|
|
process_client() {
|
|
|
|
local MESSAGE=$1
|
|
|
|
local TARGET=$2
|
|
|
|
local msg=""
|
2016-01-05 16:01:28 +00:00
|
|
|
local copname="CO$TARGET"
|
|
|
|
local copidname="$copname/pid"
|
|
|
|
local copid="$(cat $copidname 2>/dev/null)"
|
|
|
|
if [ "$copid" = "" ]; then
|
2016-01-02 20:51:45 +00:00
|
|
|
case $MESSAGE in
|
2016-01-05 16:01:28 +00:00
|
|
|
'/question')
|
|
|
|
startproc "$copname" "$TARGET"&
|
|
|
|
;;
|
|
|
|
'/info')
|
|
|
|
send_message "$TARGET" "This is bashbot, the Telegram bot written entirely in bash."
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
send_message "$TARGET" "$MESSAGE"
|
2016-01-02 20:51:45 +00:00
|
|
|
esac
|
2016-01-05 16:01:28 +00:00
|
|
|
else
|
2016-01-02 20:51:45 +00:00
|
|
|
case $MESSAGE in
|
2016-01-05 16:01:28 +00:00
|
|
|
'/cancel')
|
|
|
|
kill $copid
|
|
|
|
rm -r $copname
|
|
|
|
send_message "$TARGET" "Command canceled."
|
|
|
|
;;
|
|
|
|
*) inproc "$copname" "$copid" "$MESSAGE";;
|
2016-01-02 20:51:45 +00:00
|
|
|
esac
|
2016-01-05 16:01:28 +00:00
|
|
|
fi
|
2015-12-26 01:47:10 +00:00
|
|
|
}
|
|
|
|
|
2015-07-10 05:43:08 +00:00
|
|
|
while true; do {
|
|
|
|
|
2016-01-05 16:01:28 +00:00
|
|
|
res=$(curl -s $UPD_URL$OFFSET)
|
2015-07-10 05:43:08 +00:00
|
|
|
|
2016-01-05 16:01:28 +00:00
|
|
|
TARGET=$(echo $res | JSON.sh | egrep '\["result",0,"message","chat","id"\]' | cut -f 2)
|
|
|
|
OFFSET=$(echo $res | JSON.sh | egrep '\["result",0,"update_id"\]' | cut -f 2)
|
|
|
|
MESSAGE=$(echo $res | JSON.sh -s | egrep '\["result",0,"message","text"\]' | cut -f 2 | cut -d '"' -f 2)
|
2015-07-10 05:43:08 +00:00
|
|
|
|
|
|
|
OFFSET=$((OFFSET+1))
|
|
|
|
|
2015-07-10 07:32:34 +00:00
|
|
|
if [ $OFFSET != 1 ]; then
|
2016-01-02 19:46:34 +00:00
|
|
|
process_client "$MESSAGE" "$TARGET"&
|
2015-07-10 07:32:34 +00:00
|
|
|
fi
|
2015-07-10 05:43:08 +00:00
|
|
|
|
2016-01-05 16:01:28 +00:00
|
|
|
}; done
|
|
|
|
|