2016-01-02 20:35:15 +01:00
|
|
|
#!/bin/bash
|
2015-07-10 07:43:08 +02:00
|
|
|
# bashbot, the Telegram bot written in bash.
|
2016-01-05 17:01:28 +01:00
|
|
|
# Written by @topkecleon, Juan Potato (@awkward_potato), Lorenzo Santina (BigNerd95) and Daniil Gentili (danog)
|
2016-01-05 17:54:34 +01:00
|
|
|
# https://github.com/topkecleon/telegram-bot-bash
|
|
|
|
|
|
|
|
# Depends on ./JSON.sh (http://github.com/dominictarr/./JSON.sh),
|
|
|
|
# which is MIT/Apache-licensed
|
|
|
|
# And on tmux (https://github.com/tmux/tmux),
|
|
|
|
# which is BSD-licensed
|
2015-07-10 07:43:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
|
2016-01-05 17:54:34 +01:00
|
|
|
TOKEN='tokenhere'
|
2015-07-10 07:43:08 +02:00
|
|
|
URL='https://api.telegram.org/bot'$TOKEN
|
2015-11-25 04:07:39 +01:00
|
|
|
MSG_URL=$URL'/sendMessage'
|
|
|
|
PHO_URL=$URL'/sendPhoto'
|
2016-01-05 18:26:27 +01:00
|
|
|
FILE_URL='https://api.telegram.org/file/bot'$TOKEN'/'
|
2015-07-10 07:43:08 +02:00
|
|
|
UPD_URL=$URL'/getUpdates?offset='
|
|
|
|
OFFSET=0
|
|
|
|
|
2015-12-26 02:47:10 +01:00
|
|
|
send_message() {
|
2016-01-05 17:01:28 +01:00
|
|
|
local chat="$1"
|
2016-01-05 17:54:34 +01:00
|
|
|
local text="$(echo "$2" | sed 's/ mykeyboardstartshere.*//g;s/ myimagelocationstartshere.*//g')"
|
|
|
|
local keyboard="$(echo "$2" | sed '/mykeyboardstartshere /!d;s/.*mykeyboardstartshere //g;s/ myimagelocationstartshere.*//g')"
|
|
|
|
local image="$(echo "$2" | sed '/myimagelocationstartshere /!d;s/.*myimagelocationstartshere //g;s/ mykeyboardstartshere.*//g;')"
|
|
|
|
if [ "$keyboard" != "" ]; then
|
2016-01-05 17:01:28 +01:00
|
|
|
send_keyboard "$chat" "$text" "$keyboard"
|
2016-01-05 17:54:34 +01:00
|
|
|
local sent=y
|
|
|
|
fi
|
|
|
|
if [ "$image" != "" ]; then
|
|
|
|
send_photo "$chat" "$image"
|
|
|
|
local sent=y
|
|
|
|
fi
|
|
|
|
if [ "$sent" != "y" ];then
|
|
|
|
res=$(curl -s "$MSG_URL" -F "chat_id=$chat" -F "text=$text")
|
2016-01-05 17:01:28 +01:00
|
|
|
fi
|
2016-01-03 02:14:07 +01:00
|
|
|
}
|
2016-01-05 17:01:28 +01:00
|
|
|
|
2016-01-03 02:14:07 +01:00
|
|
|
send_keyboard() {
|
2016-01-05 17:01:28 +01:00
|
|
|
local chat="$1"
|
|
|
|
local text="$2"
|
|
|
|
shift 2
|
2016-01-05 17:54:34 +01:00
|
|
|
local keyboard=init
|
|
|
|
for f in $*;do local keyboard="$keyboard, [\"$f\"]";done
|
|
|
|
local keyboard=${keyboard/init, /}
|
2016-01-05 17:01:28 +01:00
|
|
|
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 04:07:39 +01:00
|
|
|
}
|
|
|
|
|
2015-12-26 02:47:10 +01:00
|
|
|
send_photo() {
|
2016-01-05 17:01:28 +01:00
|
|
|
res=$(curl -s "$PHO_URL" -F "chat_id=$1" -F "photo=@$2")
|
2015-07-10 09:32:34 +02:00
|
|
|
}
|
|
|
|
|
2016-01-05 17:01:28 +01:00
|
|
|
startproc() {
|
|
|
|
local copname="$1"
|
|
|
|
local TARGET="$2"
|
|
|
|
mkdir -p "$copname"
|
|
|
|
mkfifo $copname/out
|
2016-01-05 17:54:34 +01:00
|
|
|
tmux new-session -d -n $copname "./question 2>&1>$copname/out"
|
2016-01-05 17:01:28 +01:00
|
|
|
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 21:51:45 +01:00
|
|
|
}
|
2016-01-03 02:14:07 +01:00
|
|
|
inproc() {
|
2016-01-05 17:01:28 +01:00
|
|
|
local copname="$1"
|
|
|
|
local copid="$2"
|
2016-01-05 18:26:27 +01:00
|
|
|
local MESSAGE="$3"
|
|
|
|
local PHOTO_ID="$4"
|
2016-01-05 17:01:28 +01:00
|
|
|
shift 2
|
2016-01-05 18:26:27 +01:00
|
|
|
tmux send-keys -t $copname "$MESSAGE
|
2016-01-05 17:01:28 +01:00
|
|
|
"
|
|
|
|
ps aux | grep -v grep | grep -q "$copid" || { rm -r $copname; };
|
2016-01-03 02:14:07 +01:00
|
|
|
}
|
|
|
|
|
2015-12-26 02:47:10 +01:00
|
|
|
process_client() {
|
|
|
|
local MESSAGE=$1
|
|
|
|
local TARGET=$2
|
2016-01-05 18:26:27 +01:00
|
|
|
local PHOTO_ID=$3
|
2015-12-26 02:47:10 +01:00
|
|
|
local msg=""
|
2016-01-05 17:01:28 +01:00
|
|
|
local copname="CO$TARGET"
|
|
|
|
local copidname="$copname/pid"
|
|
|
|
local copid="$(cat $copidname 2>/dev/null)"
|
|
|
|
if [ "$copid" = "" ]; then
|
2016-01-02 21:51:45 +01:00
|
|
|
case $MESSAGE in
|
2016-01-05 17:01:28 +01:00
|
|
|
'/question')
|
|
|
|
startproc "$copname" "$TARGET"&
|
|
|
|
;;
|
|
|
|
'/info')
|
|
|
|
send_message "$TARGET" "This is bashbot, the Telegram bot written entirely in bash."
|
|
|
|
;;
|
2016-01-05 17:05:26 +01:00
|
|
|
'/start')
|
|
|
|
send_message "$TARGET" "This is bashbot, the Telegram bot written entirely in bash.
|
|
|
|
Features background tasks and interactive chats.
|
|
|
|
Can serve as an interface for cli programs.
|
|
|
|
Currently can send messages, custom keyboards and photos.
|
|
|
|
|
2016-01-05 17:14:47 +01:00
|
|
|
Available commands:
|
|
|
|
/start: Start bot and get this message.
|
|
|
|
/info: Get shorter info message about this bot.
|
|
|
|
/question: Start interactive chat.
|
|
|
|
/cancel: Cancel any currently running interactive chats.
|
|
|
|
|
2016-01-05 17:05:26 +01:00
|
|
|
Written by @topkecleon, Juan Potato (@awkward_potato), Lorenzo Santina (BigNerd95) and Daniil Gentili (danog)
|
2016-01-05 17:54:34 +01:00
|
|
|
https://github.com/topkecleon/telegram-bot-bash
|
2016-01-05 17:05:26 +01:00
|
|
|
"
|
|
|
|
;;
|
2016-01-05 17:01:28 +01:00
|
|
|
*)
|
|
|
|
send_message "$TARGET" "$MESSAGE"
|
2016-01-02 21:51:45 +01:00
|
|
|
esac
|
2016-01-05 17:01:28 +01:00
|
|
|
else
|
2016-01-02 21:51:45 +01:00
|
|
|
case $MESSAGE in
|
2016-01-05 17:01:28 +01:00
|
|
|
'/cancel')
|
|
|
|
kill $copid
|
|
|
|
rm -r $copname
|
|
|
|
send_message "$TARGET" "Command canceled."
|
|
|
|
;;
|
2016-01-05 18:26:27 +01:00
|
|
|
*) inproc "$copname" "$copid" "$MESSAGE" "$PHOTO_ID";;
|
2016-01-02 21:51:45 +01:00
|
|
|
esac
|
2016-01-05 17:01:28 +01:00
|
|
|
fi
|
2015-12-26 02:47:10 +01:00
|
|
|
}
|
|
|
|
|
2015-07-10 07:43:08 +02:00
|
|
|
while true; do {
|
|
|
|
|
2016-01-05 17:01:28 +01:00
|
|
|
res=$(curl -s $UPD_URL$OFFSET)
|
2015-07-10 07:43:08 +02:00
|
|
|
|
2016-01-05 17:54:34 +01: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)
|
2016-01-05 18:26:27 +01:00
|
|
|
PHOTO_ID=$(echo $res | ./JSON.sh -s | egrep '\["result",0,"message","photo",.*,"file_id"\]' | cut -f 2 | cut -d '"' -f 2 | sed -n '$p')
|
2015-07-10 07:43:08 +02:00
|
|
|
|
|
|
|
OFFSET=$((OFFSET+1))
|
|
|
|
|
2015-07-10 09:32:34 +02:00
|
|
|
if [ $OFFSET != 1 ]; then
|
2016-01-05 18:26:27 +01:00
|
|
|
process_client "$MESSAGE" "$TARGET" "$PHOTO_ID"&
|
2015-07-10 09:32:34 +02:00
|
|
|
fi
|
2015-07-10 07:43:08 +02:00
|
|
|
|
2016-01-05 17:01:28 +01:00
|
|
|
}; done
|
|
|
|
|